使用 "PySerial" 在 Python 3.7.2 中读取串行数据的延迟不一致
Inconsistent delay in reading serial data in Python 3.7.2 using "PySerial"
我正在使用 pySerial 库将串行数据从 Arduino 的基本示例 "AnalogReadSerial" 读取到 Python 3.7.2。有大约 不稳定的延迟。 16 毫秒 和一个 异常峰值约为在每次记录中观察到 1.5 秒。 (Arduino 的打印时间约为 5 milliseconds/data 点)
使用 pySerial 时这种延迟是正常的还是我的代码有问题?
如果经常看到pySerial的延迟,你能推荐一个更好的库来读取串行数据吗?
尝试过的黑客攻击:
- 我尝试在 Arduino 代码中设置足够的延迟(延迟 5 毫秒)——类似的延迟结果。
- 此线程中的建议 -- PySerial delay in reading line from Arduino --incorporated "in_waiting"
- 考虑了 Arduino 的打印时间(约 5ms)
import serial
import time
serialport = serial.Serial('COM3',9600) #define my port
count =1
timedata = []
while count<=100: #run for 100 serial values
if serialport.in_waiting > 0: #buffer
count += 1
t1 = int(round(time.time()*1000)) #time before reading
reading = serialport.readline().decode() #read serial data and decode
t2 = int(round(time.time()*1000)) #time after reading
finalt = t2 - t1 #time taken to read
timedata.append(finalt) #store all time values in a list
print(timedata[-1]) #print time for reading every new value
我几乎每次都得到相似的结果。
在代码 运行 期间有一次 1.5 秒的峰值,否则不稳定的 16 毫秒延迟。
这是图表的图像:
感谢您的宝贵时间。
您可能 运行 遇到了 USB 转串口芯片的延迟问题,这很可能是 FTDI 设备。
https://projectgus.com/2011/10/notes-on-ftdi-latency-with-arduino/
On Linux & Windows, the default latency timer setting is 16ms. For example, say you send a 3 byte MIDI message from your Arduino at 115200bps. As serial data, it takes 0.3ms for the MIDI message to go from the Arduino’s microcontroller to the FTDI chip. However, the FTDI holds the message in its buffer for a further 15.8ms (16ms after the first byte arrived), before the latency timer expires and it sends a USB packet to the computer.
文章继续解释如何调整您的设置。
我正在使用 pySerial 库将串行数据从 Arduino 的基本示例 "AnalogReadSerial" 读取到 Python 3.7.2。有大约 不稳定的延迟。 16 毫秒 和一个 异常峰值约为在每次记录中观察到 1.5 秒。 (Arduino 的打印时间约为 5 milliseconds/data 点)
使用 pySerial 时这种延迟是正常的还是我的代码有问题?
如果经常看到pySerial的延迟,你能推荐一个更好的库来读取串行数据吗?
尝试过的黑客攻击:
- 我尝试在 Arduino 代码中设置足够的延迟(延迟 5 毫秒)——类似的延迟结果。
- 此线程中的建议 -- PySerial delay in reading line from Arduino --incorporated "in_waiting"
- 考虑了 Arduino 的打印时间(约 5ms)
import serial
import time
serialport = serial.Serial('COM3',9600) #define my port
count =1
timedata = []
while count<=100: #run for 100 serial values
if serialport.in_waiting > 0: #buffer
count += 1
t1 = int(round(time.time()*1000)) #time before reading
reading = serialport.readline().decode() #read serial data and decode
t2 = int(round(time.time()*1000)) #time after reading
finalt = t2 - t1 #time taken to read
timedata.append(finalt) #store all time values in a list
print(timedata[-1]) #print time for reading every new value
我几乎每次都得到相似的结果。 在代码 运行 期间有一次 1.5 秒的峰值,否则不稳定的 16 毫秒延迟。
这是图表的图像:
感谢您的宝贵时间。
您可能 运行 遇到了 USB 转串口芯片的延迟问题,这很可能是 FTDI 设备。
https://projectgus.com/2011/10/notes-on-ftdi-latency-with-arduino/
On Linux & Windows, the default latency timer setting is 16ms. For example, say you send a 3 byte MIDI message from your Arduino at 115200bps. As serial data, it takes 0.3ms for the MIDI message to go from the Arduino’s microcontroller to the FTDI chip. However, the FTDI holds the message in its buffer for a further 15.8ms (16ms after the first byte arrived), before the latency timer expires and it sends a USB packet to the computer.
文章继续解释如何调整您的设置。