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 = [
python串口示波器(将串口数据接收与绘图分开)
于 2024-10-02 14:59:52 首次发布