重新打开后,arduino 和 pyserial 之间的串行连接变慢
Posted
技术标签:
【中文标题】重新打开后,arduino 和 pyserial 之间的串行连接变慢【英文标题】:Serial connection between arduino and pyserial slows down after reopening 【发布时间】:2019-03-09 10:32:11 【问题描述】:在使用 arduino 和 pyserial 之间的串行连接时,我偶然发现了一个问题,我需要您的帮助:
当我第一次使用串行连接时,它比我再次使用串行连接时快得多。
这是一个最简单的例子:
Arduino 代码:
void setup()
Serial.begin(9600);
Serial.println("Arduino ready"); // Print when Arduino ready
void loop()
// send data only when you receive data:
if(Serial.available() > 0)
Serial.read();
Serial.println(' ');
delay(1); // delay in between reads for stability
Python 代码:
import serial
import time
ser = serial.Serial('COM5',9600,timeout=1)
print(ser.readline()) # Wait until Arduino is ready
for i in range(1,10):
tic = time.time() # Start timer
ser.write(b' ')
ser.readline()
toc = time.time() # Log time
print("Serial write / read took %7.4f ms" % ((toc-tic)*1000))
ser.close()
第一次运行python代码:
b'Arduino ready\r\n'
Serial write / read took 5.9998 ms
Serial write / read took 6.0000 ms
Serial write / read took 7.0000 ms
Serial write / read took 5.9998 ms
Serial write / read took 6.0003 ms
Serial write / read took 5.9998 ms
Serial write / read took 6.0000 ms
Serial write / read took 7.0002 ms
Serial write / read took 5.9998 ms
再次运行python代码:
b'Arduino ready\r\n'
Serial write / read took 27.9999 ms
Serial write / read took 29.0003 ms
Serial write / read took 27.9999 ms
Serial write / read took 28.0001 ms
Serial write / read took 27.9999 ms
Serial write / read took 29.0000 ms
Serial write / read took 27.9999 ms
Serial write / read took 28.0001 ms
Serial write / read took 27.9999 ms
恢复串行连接速度的操作:
在 arduino 和 pc 之间拔下 / 插入 usb 电缆 重新编程arduino通过重置按钮重置 arduino 不会恢复串行连接。
任何想法如何在重新打开连接时达到第一个连接的连接速度?
我正在使用 python 3.6 和 pyserial 3.4。 您需要更多信息吗?
提前致谢!
【问题讨论】:
【参考方案1】:当我将串行波特率从 9600 增加到 28800 时,问题不再出现。
我还发现,这个问题只发生在我的 Arudino Uno 克隆上。当我使用原始的 Arudino Uno 时,串行连接速度在波特率为 9600 时不会降低。
【讨论】:
以上是关于重新打开后,arduino 和 pyserial 之间的串行连接变慢的主要内容,如果未能解决你的问题,请参考以下文章
在使用 pyserial 和 Arduino 时面临 TkInter 的问题
PySerial + Arduino MEGA 1 秒延迟/响应时间