Pyserial prints empty b ' ' from a USB serial port

我的梦境 提交于 2021-01-28 06:33:38

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!