1

我正在尝试通过 USB 从 Arduino beta Metro M4 express(使用 circuitpython 编程)发送命令和接收数据。circuitpython的版本是4.0.1,Python版本是3.7.3。我可以通过 USB 发送数据,但在接收数据时遇到问题。命令'supervisor.runtime.serial_bytes_available'读取通过usb从PC发送的命令,但我正在接收使用python shell中的'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 shell中接收到的预期和结果如下: 预期结果是:received1: True, received2: what 接收结果:cmd= b'what' received1: hat received2:
cmd= b'what' received1: hat received2:
cmd = b'what' received1: hat received2:
cmd= b'what' received1: hat received2:
cmd= b'what' received1: hat received2:
cmd= b'what'

4

0 回答 0