From 7299157dbafc4c4092c6bf7d7c9fc36f3707664c Mon Sep 17 00:00:00 2001 From: pandx Date: Wed, 5 Nov 2025 17:06:19 +0800 Subject: [PATCH] support serial num config and get --- SamplingDialog.py | 70 +++++++++++++++++++++++++++++++++++++++++++++++ mainwindow.py | 45 +++++++++++++++++++++++++++--- 2 files changed, 111 insertions(+), 4 deletions(-) diff --git a/SamplingDialog.py b/SamplingDialog.py index f2eb61e..0d7bb77 100644 --- a/SamplingDialog.py +++ b/SamplingDialog.py @@ -98,6 +98,76 @@ class Eigenvalue: self.offset2 = offset2 +class SNConfigDialog(QDialog): + def __init__(self, socket: QTcpSocket, parent=None): + super().__init__(parent) + self.setWindowTitle("配置序列号") + self.socket = socket + self.setFixedWidth(300) + self.yymmdd = QLineEdit() + self.sn = QLineEdit() + self.save_btn = QPushButton("保存") + self.cancel_btn = QPushButton("取消") + + ymd = QHBoxLayout() + ymd.addWidget(QLabel("年月日(ex:20251105)")) + ymd.addWidget(self.yymmdd) + + serial_num = QHBoxLayout() + serial_num.addWidget(QLabel("序列号(ex:12)")) + serial_num.addWidget(self.sn) + + btn_line = QHBoxLayout() + btn_line.addWidget(self.save_btn) + btn_line.addWidget(self.cancel_btn) + + main_layout = QVBoxLayout() + main_layout.addLayout(ymd) + main_layout.addLayout(serial_num) + main_layout.addLayout(btn_line) + self.setLayout(main_layout) + + self.save_btn.clicked.connect(self.send_sn_config) + self.cancel_btn.clicked.connect(self.reject) + + def send_sn_config(self): + try: + ymd_int = int(self.yymmdd.text()) + sn_int = int(self.sn.text()) + except ValueError: + QMessageBox.warning(self, "输入错误", "请输入合法的十进制数") + return + + packet = bytes([ + 0xAA, 0x55, 0xAA, + 0x13, # cmd + 0x01, # version + 0x00 # result_code (发送时为0) + ]) + struct.pack('2I', ymd_int, sn_int) + + self.socket.write(packet) + if not self.socket.waitForBytesWritten(1000): + QMessageBox.critical(self, "错误", "发送失败") + return + + if not self.socket.waitForReadyRead(3000): + QMessageBox.critical(self, "超时", "未收到回应") + return + + data = bytes(self.socket.readAll()) + pkg = PackageHead.parse(data) + + if not pkg: + QMessageBox.critical(self, "错误", "返回数据格式无效") + return + + if pkg['result_code'] == 0x00: + QMessageBox.information(self, "成功", "配置成功") + self.accept() + else: + QMessageBox.warning(self, "失败", f"错误码: {pkg['result_code']}") + + class MacConfigDialog(QDialog): def __init__(self, socket: QTcpSocket, parent=None): super().__init__(parent) diff --git a/mainwindow.py b/mainwindow.py index e034abe..17c4f88 100644 --- a/mainwindow.py +++ b/mainwindow.py @@ -182,6 +182,8 @@ class SocketClientApp(QMainWindow): self.get_version_button = QPushButton("版本") self.upgrade_button_sampling = QPushButton("采样率/时间") self.save_button_csv = QPushButton("存储到csv") + self.set_sn_button = QPushButton("设置序列号") + self.get_sn_button = QPushButton("获取序列号") self.get_data_button.setEnabled(False) # 初始状态不可点击 self.get_data_button2.setEnabled(False) # 初始状态不可点击 self.upgrade_button.setEnabled(False) # 初始状态不可点击 @@ -193,7 +195,9 @@ class SocketClientApp(QMainWindow): self.ipv4_config_button.setEnabled(False) self.calibration_config_button.setEnabled(False) self.get_version_button.setEnabled(False) - self.ip_input.setText("192.168.0.191") + self.set_sn_button.setEnabled(False) + self.get_sn_button.setEnabled(False) + self.ip_input.setText("192.168.0.253") self.port_input.setText("12345") # 预留绘图区域 @@ -241,6 +245,8 @@ class SocketClientApp(QMainWindow): button_layout.addWidget(self.calibration_config_button) button_layout.addWidget(self.get_version_button) button_layout.addWidget(self.save_button_csv) + button_layout.addWidget(self.set_sn_button) + button_layout.addWidget(self.get_sn_button) main_layout.addLayout(button_layout) # 添加分割线 @@ -299,6 +305,8 @@ class SocketClientApp(QMainWindow): self.calibration_config_button.clicked.connect(self.calibration_dialog) self.get_version_button.clicked.connect(self.on_button_get_version) self.batch_process_button.clicked.connect(self.on_button_batch) + self.get_sn_button.clicked.connect(self.on_button_get_sn) + self.set_sn_button.clicked.connect(self.on_button_set_sn) # 设置布局策略,确保控件大小随窗口调整 self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) @@ -370,6 +378,8 @@ class SocketClientApp(QMainWindow): self.ipv4_config_button.setEnabled(True) self.calibration_config_button.setEnabled(True) self.get_version_button.setEnabled(True) + self.set_sn_button.setEnabled(True) + self.get_sn_button.setEnabled(True) self.connect_button.setText("断开") def on_socket_disconnected(self): @@ -387,6 +397,8 @@ class SocketClientApp(QMainWindow): self.ipv4_config_button.setEnabled(False) self.calibration_config_button.setEnabled(False) self.get_version_button.setEnabled(False) + self.set_sn_button.setEnabled(False) + self.get_sn_button.setEnabled(False) # self.current_reconnect_attempts = 0 # self.reconnect_timer.start(self.reconnect_interval) @@ -522,9 +534,17 @@ class SocketClientApp(QMainWindow): body_size = struct.calcsize(body_format) body_data = recv_data[HEADER_SIZE:HEADER_SIZE + body_size] unpacked_data = struct.unpack(body_format, body_data) - print(f"version{unpacked_data}") + print(f"mac info {unpacked_data}") QMessageBox.information(self, "MAC地址", f"50-29-4D-20-{format(unpacked_data[0], '02x')}-{format(unpacked_data[1], '02x')}") + elif cmd == 0x14: + body_format = '< I I' + body_size = struct.calcsize(body_format) + body_data = recv_data[HEADER_SIZE:HEADER_SIZE + body_size] + unpacked_data = struct.unpack(body_format, body_data) + print(f"sn: {unpacked_data}") + QMessageBox.information(self, "序列号为: ", + f"年月日: {unpacked_data[0]}, 序号: {unpacked_data[1]}") def receive_data(self, length: int): while len(self.buffer) < length: @@ -613,7 +633,7 @@ class SocketClientApp(QMainWindow): 0x01, 0x00, 0x00, 0x01 ]) + rate_bytes + time_bytes - + print("get channel 1 wave") self.socket.write(packet) print(packet) self.status_bar.showMessage(f"状态: 正在获取通道1数据,采样率: {self.rate_code}, 时间: {time_value}s") @@ -634,7 +654,7 @@ class SocketClientApp(QMainWindow): 0x01, 0x00, 0x00, 0x02 ]) + rate_bytes + time_bytes - + print("get channel 2 wave") self.socket.write(packet) self.status_bar.showMessage(f"状态: 正在获取通道2数据,采样率: {self.rate_code}, 时间: {time_value}s") self.socket.waitForReadyRead() @@ -787,6 +807,13 @@ class SocketClientApp(QMainWindow): dialog.exec_() self.socket.readyRead.connect(self.on_ready_read) + def on_button_set_sn(self): + self.recv_state = '' + self.socket.readyRead.disconnect(self.on_ready_read) + dialog = SNConfigDialog(self.socket, self) + dialog.exec_() + self.socket.readyRead.connect(self.on_ready_read) + def calibration_dialog(self): self.recv_state = '' self.socket.readyRead.disconnect(self.on_ready_read) @@ -814,6 +841,16 @@ class SocketClientApp(QMainWindow): except Exception as e: self.status_bar.showMessage(f"状态: 错误 - {str(e)}") + def on_button_get_sn(self): + try: + self.recv_state = '' + self.status_bar.showMessage("状态: 正在获取数据...", 3000) + self.socket.write(bytes( + [0xAA, 0x55, 0xAA, 0x14, 0x01, 0x00])) # 发送数据 + self.socket.waitForReadyRead() + except Exception as e: + self.status_bar.showMessage(f"状态: 错误 - {str(e)}") + if __name__ == '__main__': app = QApplication(sys.argv)