增加温度解析

This commit is contained in:
2026-08-16 11:57:44 +08:00
parent 4c284c6e2f
commit a06b90bf77
+31
View File
@@ -56,6 +56,7 @@ class ReaderThread(QThread):
data_received = pyqtSignal(int, int) # (ch1, ch2) data_received = pyqtSignal(int, int) # (ch1, ch2)
error_occurred = pyqtSignal(str) error_occurred = pyqtSignal(str)
frame_raw = pyqtSignal(bytes) # 每收到一帧(不含帧头的4字节原始数据)就投递, 供UI侧决定是否作为温度帧
def __init__(self, port: str, baudrate: int = 115200): def __init__(self, port: str, baudrate: int = 115200):
super().__init__() super().__init__()
@@ -133,6 +134,7 @@ class ReaderThread(QThread):
del buf[:6] del buf[:6]
ch1 = data[0] | (data[1] << 8) ch1 = data[0] | (data[1] << 8)
ch2 = data[2] | (data[3] << 8) ch2 = data[2] | (data[3] << 8)
self.frame_raw.emit(data) # 先投递原始4字节, UI侧判断是否为温度帧
self.data_received.emit(ch1, ch2) self.data_received.emit(ch1, ch2)
@@ -158,6 +160,9 @@ class MainWindow(QMainWindow):
self.y1 = deque(maxlen=MAX_POINTS) self.y1 = deque(maxlen=MAX_POINTS)
self.y2 = deque(maxlen=MAX_POINTS) self.y2 = deque(maxlen=MAX_POINTS)
# 采集状态: True 表示下一个到达的帧视为"采集后第一帧", 用于识别温度帧
self._first_frame = False
self._build_ui() self._build_ui()
self._refresh_ports() self._refresh_ports()
@@ -201,6 +206,10 @@ class MainWindow(QMainWindow):
bar.addWidget(self.follow_chk) bar.addWidget(self.follow_chk)
bar.addStretch() bar.addStretch()
self.temp_lbl = QLabel("温度: -- ℃")
self.temp_lbl.setMinimumWidth(110)
bar.addWidget(self.temp_lbl)
self.status_lbl = QLabel("未连接") self.status_lbl = QLabel("未连接")
self.status_lbl.setMinimumWidth(120) self.status_lbl.setMinimumWidth(120)
bar.addWidget(self.status_lbl) bar.addWidget(self.status_lbl)
@@ -241,6 +250,7 @@ class MainWindow(QMainWindow):
def _connect(self, port: str): def _connect(self, port: str):
self.thread = ReaderThread(port) self.thread = ReaderThread(port)
self.thread.data_received.connect(self._on_data) self.thread.data_received.connect(self._on_data)
self.thread.frame_raw.connect(self._on_frame_raw)
self.thread.error_occurred.connect(self._on_reader_error) self.thread.error_occurred.connect(self._on_reader_error)
self.thread.start() self.thread.start()
self.connect_btn.setText("断开") self.connect_btn.setText("断开")
@@ -249,6 +259,7 @@ class MainWindow(QMainWindow):
self.port_combo.setEnabled(False) self.port_combo.setEnabled(False)
self.refresh_btn.setEnabled(False) self.refresh_btn.setEnabled(False)
self.status_lbl.setText(f"已连接 {port}") self.status_lbl.setText(f"已连接 {port}")
self.temp_lbl.setText("温度: -- ℃")
def _disconnect(self): def _disconnect(self):
self._stop_keepalive() self._stop_keepalive()
@@ -271,11 +282,13 @@ class MainWindow(QMainWindow):
def _on_start(self): def _on_start(self):
if self.thread is None: if self.thread is None:
return return
self._first_frame = True # 标记下一帧为"采集后第一帧", 用于识别温度帧
self._send_start_keepalive() # 立即发送一次启动命令 self._send_start_keepalive() # 立即发送一次启动命令
self.keepalive_timer.start(KEEPALIVE_INTERVAL_MS) # 周期保活 self.keepalive_timer.start(KEEPALIVE_INTERVAL_MS) # 周期保活
self.start_btn.setEnabled(False) self.start_btn.setEnabled(False)
self.stop_btn.setEnabled(True) self.stop_btn.setEnabled(True)
self.status_lbl.setText("采集中...") self.status_lbl.setText("采集中...")
self.temp_lbl.setText("温度: -- ℃")
def _on_stop(self): def _on_stop(self):
self._stop_keepalive() self._stop_keepalive()
@@ -294,8 +307,26 @@ class MainWindow(QMainWindow):
self.keepalive_timer.stop() self.keepalive_timer.stop()
# ---- 数据 / 绘图 ---- # ---- 数据 / 绘图 ----
@pyqtSlot(bytes)
def _on_frame_raw(self, raw: bytes):
"""原始4字节(不含帧头)回调。
采集后第一帧如满足 data[0]+data[2]==0xFF 则视作温度帧,不绘制。
"""
if not self._first_frame:
return
self._first_frame = False
if len(raw) == 4 and (raw[0] + raw[2]) & 0xFF == 0xFF:
# 满足温度帧校验, 记录温度并抑制后续绘制
t_raw = raw[0]
self.temp_lbl.setText(f"温度: {t_raw} ℃")
setattr(self, "_skip_next_data", True) # 跳过紧随其后的 data_received
@pyqtSlot(int, int) @pyqtSlot(int, int)
def _on_data(self, ch1: int, ch2: int): def _on_data(self, ch1: int, ch2: int):
# frame_raw 先于 data_received 发出; 若被标记为温度帧则跳过绘制
if getattr(self, "_skip_next_data", False):
self._skip_next_data = False
return
self.x.append(self.sample_index) self.x.append(self.sample_index)
self.y1.append(ch1) self.y1.append(ch1)
self.y2.append(ch2) self.y2.append(ch2)