我设置了一个测试,将两个 uart-to-usb 适配器连接在一起。我以 100 Hz 左右的速率发送 14 字节数据包。不过,接收线程似乎只能在 60 Hz 左右运行。我已经使用逻辑分析仪验证了数据以正确的速率发送。
我一直在研究这个问题,下面有一个非常简化的测试代码,它只检查serial.in_waiting 的值更改。这是为了消除任何其他可能占用我时间的过程。
我的测试代码:
port1 = 'COM3'
port2 = 'COM4'
baud_rate = 115200
packet_size = 14 # Size of each packet in bytes
packet_interval = 0.01 # Interval between packets in seconds (10 ms)
number_of_packets = 100
# Generate test data to send in each packet
test_packet = b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'[:packet_size]
def sender():
with serial.Serial(port1, baud_rate, timeout=1) as ser:
start_time = 0
for _ in range(number_of_packets):
ser.write(test_packet)
ser.flush()
time_taken = time.perf_counter() - start_time
print(f"Sent {len(test_packet)} bytes in {time_taken:.6f} seconds. {1/time_taken:0.6f} Hz")
start_time = time.perf_counter()
# sleep_time = max(packet_interval-time_taken, 0)
time.sleep(packet_interval)
def receiver():
with serial.Serial(port2, baud_rate, timeout=1) as ser:
ser.reset_input_buffer()
received_data = b''
start_time = time.perf_counter()
prev_data_in_waiting = 0
while True:
data_in_waiting = ser.in_waiting
if(not data_in_waiting == prev_data_in_waiting):
print(f"data in queue {data_in_waiting}")
prev_data_in_waiting = data_in_waiting
dt = time.perf_counter()-start_time
print(f"freq : {1/dt}")
start_time = time.perf_counter()
time.sleep(0.001)
# Start sender and receiver in separate threads
start_time = time.perf_counter()
receiver_thread = threading.Thread(target=receiver)
sender_thread = threading.Thread(target=sender)
receiver_thread.start()
sender_thread.start()
receiver_thread.join()
sender_thread.join()
total_time = time.time() - start_time
print(f"Total time for data transmission: {total_time:.2f} seconds")
有人知道问题可能是什么吗?
我尝试以 100 HZ 的速率发送 14 字节数据
我预计以 100 Hz 的速率接收 14 字节的数据
我以大约 60 Hz 的速率接收数据
如果将延迟计时器降低至 1ms,FTDI 的速率可高达 1kHz。这可以从 Windows 中的设备管理器访问。