pySerial缓冲区不会刷新
我在 Windows 和 Linux 下使用 pySerial 进行串行 IO 时遇到问题。使用此代码,设备永远不会接收命令并且读取超时:
import serial
ser = serial.Serial('/dev/ttyUSB0',9600,timeout=5)
ser.write("get")
ser.flush()
print ser.read()
此代码第一次超时,但后续迭代成功:
import serial
ser = serial.Serial('/dev/ttyUSB0',9600,timeout=5)
while True:
ser.write("get")
ser.flush()
print ser.read()
任何人都可以告诉发生了什么吗?我尝试添加对sync()的调用,但它不会将串行对象作为参数。
谢谢, 罗伯特
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()
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.
Thanks,
Robert
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在写入和读取之间设置一些延迟
例如
Put some delay in between write and read
e.g.
问题确实很老,但我觉得这可能是相关的补充。
某些设备(例如 Agilent E3631)依赖于 DTR。一些超便宜的适配器没有 DTR 线(或者 不要损坏它out),并且使用这些设备,此类设备可能永远不会以预期的方式运行(读取和写入之间的延迟变得非常长)。
如果您发现自己在使用此类设备,我的建议是购买带有 DTR 的适配器。
Question is really old, but I feel this might be relevant addition.
Some devices (such as Agilent E3631, for example) rely on DTR. Some ultra-cheap adapters do not have DTR line (or do not have it broken out), and using those, such devices may never behave in expected manner (delays between reads and writes get ridiculously long).
If you find yourself wrestling with such a device, my recommendation is to get an adapter with DTR.
这是因为 pyserial 在端口实际准备好之前就从打开端口返回。我注意到像flushInput()这样的东西实际上并没有清除输入缓冲区,例如,如果在open()之后立即调用。以下是演示代码:
我的解决方法是在打开后执行任何操作之前实现 100 毫秒的延迟。
This is because pyserial returns from opening the port before it is actually ready. I've noticed that things like flushInput() don't actually clear the input buffer, for example, if called immediately after the open(). Following is code to demonstrate:
My workaround has been to implement a 100ms delay after opening before doing anything.
抱歉,这对某些人来说已经过时且显而易见,但我没有看到这里提到这个选项。当flush没有对我的硬件做任何事情时,我最终调用了read_all()。
Sorry that this is old and obvious to some, but I didn't see this option mentioned here. I ended up calling a
read_all()
when flush wasn't doing anything with my hardware.