import serial
import time

# 配置串口参数
com_port = 'COM14'  # 串口号
baud_rate = 9600    # 波特率，根据下位机配置调整
timeout = 1         # 超时时间

# 打开串口
ser = serial.Serial(com_port, baud_rate, timeout=timeout)

try:
    while True:
        start_time =  time.perf_counter()
        data_received = 0

        # 在1秒内持续接收数据
        while time.perf_counter() - start_time < 1:
            data = ser.read(ser.in_waiting or 1)
            data_received += len(data)

        # 计算并打印接收速率
        print(f"接收速率: {data_received} 字节/秒")

except KeyboardInterrupt:
    print("程序终止")

finally:
    ser.close()