From 209c485161bc83a5a6bfd25ab1c57cf950bd8115 Mon Sep 17 00:00:00 2001 From: pandx Date: Fri, 14 Nov 2025 11:49:24 +0800 Subject: [PATCH] add reconnect logic --- SamplingDialog.py | 4 +--- mainwindow.py | 26 ++++++++++++++++++++++---- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/SamplingDialog.py b/SamplingDialog.py index 9b2871d..cdf2c53 100644 --- a/SamplingDialog.py +++ b/SamplingDialog.py @@ -557,8 +557,6 @@ class CalibrationDialog(QDialog): self.socket.write(bytes( [0xAA, 0x55, 0xAA, 0x10, 0x01, 0x00])) # 发送数据 self.socket.waitForReadyRead() - - except Exception as e: self.status_bar.showMessage(f"状态: 错误 - {str(e)}") @@ -742,7 +740,7 @@ class CalibrationDialog(QDialog): def process_wave_packet(self, wave_data): data = wave_data # 接收所有数据 data = np.frombuffer(data, dtype=np.int32) # 根据实际数据格式转换 - LSB_32BIT = (2.8 / (2 ** 31)) * ((300 + 100) / 100) * 1000 + LSB_32BIT = (2.8 / (2 ** 31)) * ((300 + 100) / 100) / 2 # * 1000 # LSB_32BIT = (2.8 / (2 ** 31)) * ((750 + 249) / 249) * 1000 / 10.2 # LSB_32BIT = (2.8 / (2 ** 31)) * ((750 + 287) / 287) * 1000 / 10.2 self.scaled_data = data * LSB_32BIT diff --git a/mainwindow.py b/mainwindow.py index 2840ab8..7a7e3ec 100644 --- a/mainwindow.py +++ b/mainwindow.py @@ -151,6 +151,9 @@ class SocketClientApp(QMainWindow): self.partial_data = QByteArray() self.scaled_data = '' + self.sw_version = 2.4 # 软件版本号 + self.try_connect_times = 0 + self.sampling_rate = 0 self.buffer = QByteArray() self.reconnect_timer = QTimer(self) @@ -201,6 +204,7 @@ class SocketClientApp(QMainWindow): self.get_sn_button.setEnabled(False) self.iepe_power_switch.setEnabled(False) self.ip_input.setText("192.168.0.253") + # self.ip_input.setText("192.168.5.136") self.port_input.setText("12345") # 预留绘图区域 @@ -331,7 +335,7 @@ class SocketClientApp(QMainWindow): help_menu.addAction(version_action) # 设置状态栏右下角显示版本号 - self.status_bar.showMessage('版本号:2.2', 20000) + self.status_bar.showMessage(f'版本号:{self.sw_version}', 20000) def dnm_version_info_cb(self): msg = QMessageBox() @@ -340,9 +344,9 @@ class SocketClientApp(QMainWindow): msg.setWindowTitle("软件版本信息") # 设置消息框的内容 - msg.setText("软件版本号:2.3\n" + msg.setText(f"软件版本号:{self.sw_version}\n" "支持的分布式节点版本号:2.2以上\n" - "更新日期:2025-11-13") + "更新日期:2025-11-14") # 设置按钮 msg.setStandardButtons(QMessageBox.Ok) @@ -397,9 +401,11 @@ class SocketClientApp(QMainWindow): def connect_to_server(self): # self.process_wave_packet('') if self.connect_button.text() == "断开": + self.try_connect_times = 10 self.connect_button.setText("连接") self.socket.disconnectFromHost() elif self.connect_button.text() == "连接": + self.try_connect_times = 0 ip = self.ip_input.text() port = int(self.port_input.text()) self.socket.abort() @@ -427,6 +433,18 @@ class SocketClientApp(QMainWindow): def on_socket_disconnected(self): print("on_socket_disconnected") + if self.try_connect_times < 10: + print(f"try connect times: {self.try_connect_times + 1}") + self.try_connect_times += 1 + ip = self.ip_input.text() + port = int(self.port_input.text()) + self.socket.abort() + self.socket.connectToHost(ip, port) + # 设置 TCP Keep-Alive + self.socket.setSocketOption(QAbstractSocket.KeepAliveOption, True) + return + + print("disconnect") self.connect_button.setText("连接") self.status_bar.showMessage("状态: 连接断开") self.get_data_button.setEnabled(False) @@ -625,7 +643,7 @@ class SocketClientApp(QMainWindow): for i in range(min(5, len(data))): # 确保不超过数据长度 print(f"{data[i]:.3f}", end=" ") print() # 换行 - LSB_32BIT = (2.8 / (2 ** 31)) * ((300 + 100) / 100) * 1000 / 10.2 + LSB_32BIT = (2.8 / (2 ** 31)) * ((300 + 100) / 100) * 1000 / 10.2 / 2 # LSB_32BIT = (2.8 / (2 ** 31)) * ((750 + 287) / 287) * 1000 / 10.2 # LSB_32BIT = (2.8 / (2 ** 31)) * ((750 + 287) / 287) * 1000 self.scaled_data = data * LSB_32BIT