QtWidgets.QApplication.processEvents()

 

QtWidgets.QApplication.processEvents()

 

import sys import serial from PyQt5 import QtWidgets import pyqtgraph as pg import numpy as np from threading import Thread, Event class RealTimePlot(QtWidgets.QWidget): def __init__(self): super().__init__() # 串口配置 self.ser = serial.Serial('COM10', 115200, timeout=1) self.data_buffer = np.zeros(500) self.ptr = 0 # 界面布局 self.setWindowTitle('ADC实时监测系统') self.layout = QtWidgets.QVBoxLayout() # 绘图区域 self.plot_widget = pg.PlotWidget() self.curve = self.plot_widget.plot(pen='y') self.plot_widget.setLabel('left', 'ADC值', '') self.plot_widget.setLabel('bottom', '时间', 's') self.plot_widget.setYRange(0, 4095) self.layout.addWidget(self.plot_widget) # 控制面板 control_panel = QtWidgets.QHBoxLayout() self.start_btn = QtWidgets.QPushButton('开始') self.stop_btn = QtWidgets.QPushButton('停止') self.save_btn = QtWidgets.QPushButton('保存数据') control_panel.addWidget(self.start_btn) control_panel.addWidget(self.stop_btn) control_panel.addWidget(self.save_btn) self.layout.addLayout(control_panel) self.setLayout(self.layout) # 事件绑定 self.start_btn.clicked.connect(self.start_acquisition) self.stop_btn.clicked.connect(self.stop_acquisition) self.save_btn.clicked.connect(self.save_data) # 线程控制 self.running = Event() self.thread = None def start_acquisition(self): if not self.running.is_set(): self.running.set() self.thread = Thread(target=self.data_reader) self.thread.start() self.start_btn.setEnabled(False) self.stop_btn.setEnabled(True) def stop_acquisition(self): self.running.clear() self.thread.join() self.start_btn.setEnabled(True) self.stop_btn.setEnabled(False) def data_reader(self): while self.running.is_set(): if self.ser.in_waiting: line = self.ser.readline().decode().strip() if line.startswith('ADC:'): try: value = int(line.split(':')[1]) self.data_buffer[:-1] = self.data_buffer[1:] self.data_buffer[-1] = value self.ptr += 1 QtWidgets.QApplication.processEvents() self.curve.setData(self.data_buffer) except ValueError: pass def save_data(self): filename, _ = QtWidgets.QFileDialog.getSaveFileName( self, "保存数据", "", "CSV文件 (*.csv)") if filename: np.savetxt(filename, self.data_buffer, delimiter=',', header='ADC Value', comments='') def closeEvent(self, event): self.running.clear() if self.thread is not None: self.thread.join() self.ser.close() event.accept() if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) window = RealTimePlot() window.show() sys.exit(app.exec_()) --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Cell In[1], line 95 93 if __name__ == '__main__': 94 app = QtWidgets.QApplication(sys.argv) ---> 95 window = RealTimePlot() 96 window.show() 97 sys.exit(app.exec_()) Cell In[1], line 13, in RealTimePlot.__init__(self) 10 super().__init__() 12 # 串口配置 ---> 13 self.ser = serial.Serial('COM10', 115200, timeout=1) 14 self.data_buffer = np.zeros(500) 15 self.ptr = 0 AttributeError: module 'serial' has no attribute 'Serial'
03-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Clark-dj

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值