重新开放后,Arduino和Pyserial之间的串行连接减慢



我在使用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-code:

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 readyrn'
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 readyrn'
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-cable
  • 重编程arduino

通过重置按钮重置Arduino不会恢复串行连接。

重新打开连接时如何实现第一个连接的连接速度?

我使用的是Python 3.6和Pyserial 3.4。您需要进一步的信息吗?

预先感谢!

当我将串行波特率从9600增加到28800时,问题不再发生。

我还发现,这个问题仅在我的Arudino Uno克隆中发生。当我使用原始的Arudino Uno时,串行连接速度在Baud 9600时不会降低。

最新更新