@@ -115,6 +115,8 @@ def _gen_ui(widgets: List[Dict], canvas_w: int, canvas_h: int, app_name: str = "
115115 ' FluentIcon as FIF, InfoBar, InfoBarPosition, isDarkTheme,' ,
116116 ')' ,
117117 '' ,
118+ 'from serial_handler import _list_all_ports' ,
119+ '' ,
118120 'try:' ,
119121 ' import serial' ,
120122 ' import serial.tools.list_ports' ,
@@ -612,13 +614,23 @@ def run(self):
612614 data = self._port.read(self._port.in_waiting)
613615 self.rawReceived.emit(data)
614616 self._buf += data
617+ self._buf = self._buf.replace(b"\\ \\ r\\ \\ n", b"\\ n").replace(b"\\ \\ n", b"\\ n")
618+ _found = False
615619 while b"\\ n" in self._buf:
616620 line, self._buf = self._buf.split(b"\\ n", 1)
617621 text = line.decode("utf-8", errors="replace").strip()
618622 if text:
619623 parsed = self._parse_line(text)
620624 if parsed:
621625 self.parsedReceived.emit(parsed)
626+ _found = True
627+ if not _found and self._buf:
628+ text = self._buf.decode("utf-8", errors="replace").strip()
629+ if text and ("=" in text or text.startswith("{{")):
630+ parsed = self._parse_line(text)
631+ if parsed:
632+ self._buf = b""
633+ self.parsedReceived.emit(parsed)
622634 else:
623635 self.msleep(10)
624636 except Exception as e:
@@ -1106,12 +1118,12 @@ def __init__(self, parent=None):
11061118 self._vlbs = []; main_h.addLayout(rp)
11071119 self._root.addLayout(main_h, 1)
11081120 tb = QHBoxLayout(); tb.setContentsMargins(4,2,4,0); tb.setSpacing(6)
1109- tb.addWidget(QLabel ("显示点数"))
1110- self._dsp = QLineEdit (); self._dsp.setText(str(self._disp_pts))
1121+ tb.addWidget(BodyLabel ("显示点数"))
1122+ self._dsp = LineEdit (); self._dsp.setText(str(self._disp_pts))
11111123 self._dsp.setFixedWidth(80); self._dsp.editingFinished.connect(self._on_dsp)
11121124 tb.addWidget(self._dsp); tb.addStretch()
1113- sb = QPushButton ("保存"); sb.setFixedWidth(56 ); sb.clicked.connect(self._save); tb.addWidget(sb)
1114- lb = QPushButton ("加载"); lb.setFixedWidth(56 ); lb.clicked.connect(self._load); tb.addWidget(lb)
1125+ sb = PushButton ("保存"); sb.setFixedWidth(64 ); sb.clicked.connect(self._save); tb.addWidget(sb)
1126+ lb = PushButton ("加载"); lb.setFixedWidth(64 ); lb.clicked.connect(self._load); tb.addWidget(lb)
11151127 self._root.addLayout(tb)
11161128 self._cb_row = QHBoxLayout(); self._cb_row.setContentsMargins(4,0,4,2)
11171129 self._root.addLayout(self._cb_row)
@@ -1139,8 +1151,8 @@ def configure(self, cfgs, disp_pts=200):
11391151 c = self._plot.plot(pen=pg.mkPen(color=clr, width=2), name=nm)
11401152 self._curves.append(c)
11411153 else: self._curves.append(None)
1142- cb = QCheckBox (nm); cb.setChecked(True)
1143- cb.setStyleSheet(f"QCheckBox {{ color: rgb{clr}; font-size: 11px; }}")
1154+ cb = CheckBox (nm); cb.setChecked(True)
1155+ cb.setStyleSheet(f"CheckBox {{ color: rgb{clr}; font-size: 11px; }}")
11441156 idx = i; cb.toggled.connect(lambda v, ci=idx: self._toggle(ci, v))
11451157 self._cbs.append(cb); self._cb_row.addWidget(cb)
11461158 vl = QLabel(f"<b style='color:rgb{clr}'>{nm}</b> --")
@@ -1454,7 +1466,13 @@ def __init__(self, parent=None):
14541466 super().__init__(parent)
14551467 self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
14561468 root = QVBoxLayout(self); root.setContentsMargins(16,12,16,12); root.setSpacing(8)
1457- root.addWidget(StrongBodyLabel("串口配置"))
1469+ title_row = QHBoxLayout()
1470+ title_row.addWidget(StrongBodyLabel("串口配置"))
1471+ title_row.addStretch()
1472+ self._status = CaptionLabel("未连接")
1473+ self._status.setStyleSheet("color: #888;")
1474+ title_row.addWidget(self._status)
1475+ root.addLayout(title_row)
14581476 g = QGridLayout(); g.setSpacing(6)
14591477 g.addWidget(BodyLabel("端口"), 0, 0)
14601478 self._port_combo = ComboBox(); g.addWidget(self._port_combo, 0, 1)
@@ -1474,21 +1492,40 @@ def __init__(self, parent=None):
14741492 self._handler = None; self._refresh_ports()
14751493 def set_serial_handler(self, h):
14761494 self._handler = h
1477- if h: h.connectionChanged.connect(self._on_conn_change)
1495+ if h:
1496+ h.connectionChanged.connect(self._on_conn_change)
1497+ h.error.connect(self._on_error)
1498+ h.connected.connect(lambda p: self._set_status(f"已连接: {p}", "#4CAF50"))
1499+ h.disconnected.connect(lambda: self._set_status("未连接", "#888"))
14781500 def _refresh_ports(self):
14791501 self._port_combo.clear()
14801502 for port, desc in _list_all_ports():
1481- self._port_combo.addItem(f"{port} - {desc}", port)
1503+ self._port_combo.addItem(f"{port} {desc}")
1504+ def _set_status(self, text, color):
1505+ self._status.setText(text); self._status.setStyleSheet(f"color: {color};")
1506+ def _on_error(self, msg):
1507+ self._set_status(f"错误: {msg}", "#F44336")
1508+ self._conn_btn.setEnabled(True)
14821509 def _do_connect(self):
1483- if not self._handler: return
1484- port = self._port_combo.currentData() or self._port_combo.currentText().split(" ")[0]
1510+ if not self._handler:
1511+ self._set_status("handler未绑定", "#F44336")
1512+ return
1513+ text = self._port_combo.currentText()
1514+ if not text or text == "无可用串口":
1515+ self._set_status("未选择端口", "#FF9800")
1516+ return
1517+ port = text.split()[0]
14851518 baud = int(self._baud_combo.currentText())
1519+ self._set_status("连接中...", "#FF9800")
1520+ self._conn_btn.setEnabled(False)
14861521 self._handler.connect(port, baud)
14871522 def _do_disconnect(self):
14881523 if self._handler: self._handler.disconnect()
14891524 def _on_conn_change(self, connected):
14901525 self._conn_btn.setEnabled(not connected)
14911526 self._disconn_btn.setEnabled(connected)
1527+ for w in (self._port_combo, self._baud_combo, self._refresh_btn):
1528+ w.setEnabled(not connected)
14921529'''
14931530
14941531
0 commit comments