PySerial 从 Arduino 读取线路的延迟
我正在使用带有基本“DigitalReadSerial”设置的 arduino uno,如下所述: http://arduino.cc /en/Tutorial/DigitalReadSerial
如果我使用 arduino IDE 附带的串行监视器,它会立即显示按下按钮后的任何更改。
这就是我在 pyserial 中想要的,0 或 1 取决于是否按下按钮。 (最终触发计时器)
只是为了测试它,我把它放在一起,不是最漂亮的,但它似乎读出了按钮状态,但有 20 秒的延迟。
import serial
ser = serial.Serial()
ser.setPort("COM2")
ser.baudrate = 9600
ser.open()
while 1==1:
ser.readline()
有人有什么想法吗?
I'm using an arduino uno with the basic "DigitalReadSerial" setup as outlined here: http://arduino.cc/en/Tutorial/DigitalReadSerial
If i use the serial monitor included with the arduino IDE, it immediately displays any changes from pressing the button.
This is what i want in pyserial, either a 0 or a 1 depending on whether the button is pressed. (to eventually trigger a timer)
just to test it, i threw this together, not the prettiest, but it seems to read out the pushbutton state, but there is a 20second delay.
import serial
ser = serial.Serial()
ser.setPort("COM2")
ser.baudrate = 9600
ser.open()
while 1==1:
ser.readline()
Does anyone have any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这似乎是一个缓存/同步问题,类似于影响常见文件系统中文件同步的问题。我的 arduino/pyserial 遇到了这个问题......直到现在?
来自 http://pyserial.sourceforge.net/pyserial_api.html,如果我将 3 齐平命令:我的程序中的 ser.flush()、ser.flushInput() 和 ser.flushOutput() ,它似乎按预期工作。
It seems to be a caching/sync problem, similar to those that affects the file sync in common filesystems. I have suffered that problem with my arduino/pyserial... until now?
From http://pyserial.sourceforge.net/pyserial_api.html, if I put the 3 flush commands: ser.flush(), ser.flushInput() and ser.flushOutput() in my program, it seems to work as expected.
您在 Arduino 代码中使用
Serial.print
或Serial.println
吗?如果是前者,它不会发出回车符,代码中的ser.readline()
将等待回车符。Are you using
Serial.print
orSerial.println
in your Arduino code? If the former, its not going to issue a carriage return and theser.readline()
in your code will be waiting for one.我刚刚遇到了同样的问题,我确信 PySerial 没有延迟。
延迟是由我的 PyQT 线程的延迟引起的。我通过 arduino 中的串行端口打印一行/0.1秒,但我以 0.5 秒的延迟读取 QThread 中的串行输出,这就是问题。随着时间的推移,延迟会增加。
我通过从我的项目中提取pyserial读取代码来验证这一点。请记住,读取频率不应低于写入频率。
从你的代码中,我假设你的Python环境如果不够快,无法及时从arduino接收数据。
尝试通过在两次打印之间插入一个小的延迟来减慢串行打印速度。
I just met the same problem and I'm sure there is no delay in PySerial.
The delay was caused by the delay in my PyQT Thread .I print through serial port in arduino with one line/0.1sec but I read the serial output in QThread with a 0.5sec delay,that's the problem.As time goes by,the delay will increase.
I verified that by extracting pyserial reading code from my project.Just remember,the read frequency should not be less than the write frequency.
From your code,I assume that your python enviroment if not fast enough to receive the data from arduino in time.
Try to slow down the serial print speed by insert a small delay between two print.
仅当有内容要读取时才开始读取行,否则它将在等待 eol 时阻塞,或者可能在读取串行缓冲区的过程中超时,从而截断字符串。这可以加快循环速度,并允许您使用较短的超时,非常适合循环多个端口。使用 pyserial3.0...
还可以尝试类似的方法,
它不会关心缓冲区是否包含少于指定的字节,我有时会这样做,因为它会读取/清除缓冲区中积累的额外数据。
Only commence a readline if there is something to read otherwise it will either block while waiting for eol, or it may time out half way through reading the serial buffer, truncating your string. This speeds up the loop, and allows you to use a short timeout, great for cycling through multiple ports. Using pyserial3.0...
Also try something like
Which will not care if the buffer contains less than the specified bytes, I sometimes do as it will read/flush out extra data that has builds up in the buffer.