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

时间:2019-03-09 10:32:12

标签: python 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代码:

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 个答案:

答案 0 :(得分:0)

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

我还发现,问题仅与我的Arudino Uno克隆有关。当我使用原始的Arudino Uno时,串行连接速度不会降低至9600波特。