arduino 和 pyserial 之间的串行连接在重新打开后变慢

Serial connection between arduino and pyserial slows down after reopening

我需要你的帮助来解决我在 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 不会恢复串行连接。

有什么想法可以在重新打开连接时达到第一个连接的连接速度吗?

我正在使用 python 3.6 和 pyserial 3.4。 您需要更多信息吗?

提前致谢!

将串口波特率从9600提高到28800后,问题不再出现

我还发现,问题只出现在我的 Arudino Uno 克隆上。当我使用原始的 Arudino Uno 时,串行连接速度在波特率 9600 时不会降低。