问题
I seem to be having an problem, when I run this Python 3.5 script It's for USB serial controlled device:
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()
I get these results:
python maintest.py
Got: b''
Got: b''
Got: b''
Got: b''
Got: b''
Got: b''
Got: b''
Got: b''
Got: b''
Got: b''
the frequency of the printed data seems correct, and when tested the command:
ser1.write(xxxxx)
it triggers the device and outputs the necessary data to manufacturer provided software, so it's working fine- just the python output seems not to be working. How could i tackle this?
回答1:
Maybe this will work:
while True:
data = ''
while ser1.inWaiting()>0:
data += ser1.read(1)
if data:
print ('Got:', data)
来源:https://stackoverflow.com/questions/44053983/pyserial-prints-empty-b-from-a-usb-serial-port