Pyserial 从 USB 串行端口打印空 b ' ' [英] Pyserial prints empty b ' ' from a USB serial port

查看:53
本文介绍了Pyserial 从 USB 串行端口打印空 b ' '的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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()

我得到了这些结果:

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)

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

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?

推荐答案

也许这会奏效:

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

这篇关于Pyserial 从 USB 串行端口打印空 b ' '的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆