简体   繁体   English

来自Arduino的PySerial读取线延迟

[英]PySerial delay in reading line from Arduino

I'm using an arduino uno with the basic "DigitalReadSerial" setup as outlined here: http://arduino.cc/en/Tutorial/DigitalReadSerial 我将arduino uno与基本的“ DigitalReadSerial”设置结合使用,如下所示: 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. 如果我使用arduino IDE随附的串行监视器,则它会立即显示从按按钮开始的任何更改。

This is what i want in pyserial, either a 0 or a 1 depending on whether the button is pressed. 这是我想要的pyserial,根据按钮是否按下而为0或1。 (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. 只是为了测试它,我把它放在一起,不是最漂亮的,但是似乎读出了按钮状态,但是有20秒的延迟。

    import serial
    ser = serial.Serial()
    ser.setPort("COM2")
    ser.baudrate = 9600
    ser.open()
    while 1==1:
        ser.readline()

Does anyone have any ideas? 有人有什么想法吗?

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? 我的arduino / pyserial遇到了这个问题...直到现在?

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. http://pyserial.sourceforge.net/pyserial_api.html中 ,如果我在程序中放入了3个刷新命令:ser.flush(),ser.flushInput()和ser.flushOutput(),它似乎可以正常工作。

I just met the same problem and I'm sure there is no delay in PySerial. 我只是遇到了同样的问题,而且我确信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. 延迟是由于我的PyQT线程的延迟引起的。我通过arduino的串行端口以1行/0.1秒的速度进行打印,但是我以0.5秒的延迟读取QThread的串行输出,这就是问题。随着时间的流逝,延迟将增加。

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 . 从您的代码中,我假设您的python环境不够快,无法及时从arduino接收数据

Try to slow down the serial print speed by insert a small delay between two print. 尝试通过在两次打印之间插入一个小的延迟来降低串行打印速度。

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. 仅在有需要读取的内容时才开始读取行,否则它将在等待eol时阻塞,或者可能在读取串行缓冲区的一半时超时,从而截断了字符串。 This speeds up the loop, and allows you to use a short timeout, great for cycling through multiple ports. 这样可以加快循环速度,并允许您使用较短的超时时间,非常适合在多个端口之间循环。 Using pyserial3.0... 使用pyserial3.0 ...

while 1:
    if ser.in_waiting > 0:
        data = ser.readline()
        print(data)

Also try something like 也尝试类似的东西

while 1:
    if ser.in_waiting > 0:
        data = ser.read(32)
        print(data)

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. 如果缓冲区包含的字节数少于指定的字节数,这将不在乎,我有时会这样做,因为它将读取/刷新缓冲区中累积的额外数据。

Are you using Serial.print or Serial.println in your Arduino code? 您在Arduino代码中使用的是Serial.print还是Serial.println If the former, its not going to issue a carriage return and the ser.readline() in your code will be waiting for one. 如果是前者,则不会发出回车,并且代码中的ser.readline()将等待一个。

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

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