コマンドを送信し、USB経由でArduinoベータMetro M4 Express(circuitpythonを使用してプログラム)からデータを受信しようとしています。circuitpython のバージョンは 4.0.1 で、Python のバージョンは 3.7.3 です。USB経由でデータを送信できましたが、データの受信中に問題が発生しました。コマンド「supervisor.runtime.serial_bytes_available」は、USB 経由で PC から送信されたコマンドを読み取りますが、Python シェルで「print」ステートメントを使用して印刷されたデータを受信しています。また、「data = sys.stdin.read(4)」コマンドを使用してコマンドを取得および出力できません。どんな助けでも大歓迎です。
私のpythonコードは次のとおりです。
import serial
import time
while(1):
ser = serial.Serial(port='COM9', baudrate=115200, timeout=0.01)
cmd = b'what'
print('cmd=', cmd)
ser.write(cmd)
ser.flushInput()
time.sleep(1)
x = ser.readline()
print("received1: ",x.decode("utf-8"))
y = ser.readline()
print("received2: ",y.decode("utf-8"))
ser.close()
Circuitpython コードは次のとおりです。
import supervisor
while(x):
if supervisor.runtime.serial_bytes_available:
print("True")
data = sys.stdin.read(4)
print("in code.py",data)
期待される結果と Python シェルで受信される結果は次のとおりです。 期待される結果は次のとおりです: received1: True、received2
:
what = b'what' received1: hat received2:
cmd= b'what' received1: hat received2:
cmd= b'what' received1: hat received2:
cmd= b'what'