简体   繁体   English

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

[英]Pyserial prints empty b ' ' from a USB serial port

I seem to be having an problem, when I run this Python 3.5 script It's for USB serial controlled device:我似乎遇到了问题,当我运行这个 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()

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.它触发设备并将必要的数据输出到制造商提供的软件,因此它工作正常 - 只是 python 输出似乎不起作用。 How could i tackle this?我怎么能解决这个问题?

Maybe this will work:也许这会奏效:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM