PySerial 从 Arduino 读取线路的延迟

发布于 2024-12-09 08:47:25 字数 535 浏览 0 评论 0原文

我正在使用带有基本“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半边脸i 2024-12-16 08:47:25

这似乎是一个缓存/同步问题,类似于影响常见文件系统中文件同步的问题。我的 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.

南…巷孤猫 2024-12-16 08:47:25

您在 Arduino 代码中使用 Serial.printSerial.println 吗?如果是前者,它不会发出回车符,代码中的 ser.readline() 将等待回车符。

Are you using Serial.print or Serial.println in your Arduino code? If the former, its not going to issue a carriage return and the ser.readline() in your code will be waiting for one.

以歌曲疗慰 2024-12-16 08:47:25

我刚刚遇到了同样的问题,我确信 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.

捶死心动 2024-12-16 08:47:25

仅当有内容要读取时才开始读取行,否则它将在等待 eol 时阻塞,或者可能在读取串行缓冲区的过程中超时,从而截断字符串。这可以加快循环速度,并允许您使用较短的超时,非常适合循环多个端口。使用 pyserial3.0...

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

还可以尝试类似的方法,

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

它不会关心缓冲区是否包含少于指定的字节,我有时会这样做,因为它会读取/清除缓冲区中积累的额外数据。

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...

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文