Pyserial 从 USB 串行端口打印空 b ' '

Pyserial prints empty b ' ' from a USB serial port

当我 运行 这个 Python 3.5 脚本时,我似乎遇到了问题 用于USB串口控制设备:

import serial
import time

ser1 = serial.Serial('/dev/tty.usbserial', 115200, timeout=0.1)

def setupMode():
    ser1.write(b'$PF,200\r\n')
    ser1.write(b'$PO,20\r\n')

setupMode()


def startMeasurments():
    ser1.write(b'$GO\r\n')

startMeasurments()

def checkUnit():
    ser1.write(b'$US\r\n')

checkUnit()

while True:
    data = ser1.read(9999)
    print ('Got:', data)

time.sleep(0.1)
ser1.close()

我得到这些结果:

python maintest.py
Got: b''
Got: b''
Got: b''
Got: b''
Got: b''
Got: b''
Got: b''
Got: b''
Got: b''
Got: b''

打印数据的频率似乎是正确的,测试命令时:

ser1.write(xxxxx)

它触发设备并将必要的数据输出到制造商提供的软件,因此它工作正常 - 只是 python 输出似乎不起作用。 我该如何解决这个问题?

也许这会奏效:

while True:
    data = ''
    while ser1.inWaiting()>0:
        data += ser1.read(1)
    if data:
        print ('Got:', data)