本文介绍了pySerial 缓冲区不会刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 Windows 和 Linux 下使用 pySerial 时遇到串行 IO 问题.使用此代码,设备永远不会收到命令并且读取超时:
I'm having a problem with serial IO under both Windows and Linux using pySerial. With this code the device never receives the command and the read times out:
import serial
ser = serial.Serial('/dev/ttyUSB0',9600,timeout=5)
ser.write("get")
ser.flush()
print ser.read()
此代码第一次超时,但后续迭代成功:
This code times out the first time through, but subsequent iterations succeed:
import serial
ser = serial.Serial('/dev/ttyUSB0',9600,timeout=5)
while True:
ser.write("get")
ser.flush()
print ser.read()
谁能告诉我这是怎么回事?我尝试添加对 sync() 的调用,但它不会将串行对象作为参数.
Can anyone tell what's going on? I tried to add a call to sync() but it wouldn't take a serial object as it's argument.
谢谢,罗伯特
推荐答案
在写入和读取之间放置一些延迟例如
Put some delay in between write and reade.g.
import serial
ser = serial.Serial('/dev/ttyUSB0',9600,timeout=5)
ser.flushInput()
ser.flushOutput()
ser.write("get")
# sleep(1) for 100 millisecond delay
# 100ms dely
sleep(.1)
print ser.read()
这篇关于pySerial 缓冲区不会刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!