import serial
import matplotlib.pyplot as plt
import numpy as np
import threading
import queue
import time# 创建队列用于线程间数据传递
data_queue = queue.Queue()# 配置串口
ser = serial.Serial('COM3', 115200)# 数据读取线程
def read_from_serial(ser, data_queue):while True:try:value = float(ser.readline().decode().strip())data_queue.put(value) # 将读取到的数据放入队列except Exception as e:print(f"Error reading serial: {e}")# 启动数据读取线程
read_thread = threading.Thread(target=read_from_serial, args=(ser, data_queue))
read_thread.daemon = True
read_thread.start()# 绘图设置
plt.ion()
fig, ax = plt.subplots()
line, = ax.plot([], [])
ax.set_ylim(0, 10) # 根据实际情况调整
ax.set_xlim(0, 100)# 数据缓冲区
data = [