add reconnect logic

This commit is contained in:
pandx 2025-11-14 11:49:24 +08:00
parent b212882709
commit 209c485161
2 changed files with 23 additions and 7 deletions

View File

@ -557,8 +557,6 @@ class CalibrationDialog(QDialog):
self.socket.write(bytes( self.socket.write(bytes(
[0xAA, 0x55, 0xAA, 0x10, 0x01, 0x00])) # 发送数据 [0xAA, 0x55, 0xAA, 0x10, 0x01, 0x00])) # 发送数据
self.socket.waitForReadyRead() self.socket.waitForReadyRead()
except Exception as e: except Exception as e:
self.status_bar.showMessage(f"状态: 错误 - {str(e)}") self.status_bar.showMessage(f"状态: 错误 - {str(e)}")
@ -742,7 +740,7 @@ class CalibrationDialog(QDialog):
def process_wave_packet(self, wave_data): def process_wave_packet(self, wave_data):
data = wave_data # 接收所有数据 data = wave_data # 接收所有数据
data = np.frombuffer(data, dtype=np.int32) # 根据实际数据格式转换 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 + 249) / 249) * 1000 / 10.2
# LSB_32BIT = (2.8 / (2 ** 31)) * ((750 + 287) / 287) * 1000 / 10.2 # LSB_32BIT = (2.8 / (2 ** 31)) * ((750 + 287) / 287) * 1000 / 10.2
self.scaled_data = data * LSB_32BIT self.scaled_data = data * LSB_32BIT

View File

@ -151,6 +151,9 @@ class SocketClientApp(QMainWindow):
self.partial_data = QByteArray() self.partial_data = QByteArray()
self.scaled_data = '' self.scaled_data = ''
self.sw_version = 2.4 # 软件版本号
self.try_connect_times = 0
self.sampling_rate = 0 self.sampling_rate = 0
self.buffer = QByteArray() self.buffer = QByteArray()
self.reconnect_timer = QTimer(self) self.reconnect_timer = QTimer(self)
@ -201,6 +204,7 @@ class SocketClientApp(QMainWindow):
self.get_sn_button.setEnabled(False) self.get_sn_button.setEnabled(False)
self.iepe_power_switch.setEnabled(False) self.iepe_power_switch.setEnabled(False)
self.ip_input.setText("192.168.0.253") self.ip_input.setText("192.168.0.253")
# self.ip_input.setText("192.168.5.136")
self.port_input.setText("12345") self.port_input.setText("12345")
# 预留绘图区域 # 预留绘图区域
@ -331,7 +335,7 @@ class SocketClientApp(QMainWindow):
help_menu.addAction(version_action) 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): def dnm_version_info_cb(self):
msg = QMessageBox() msg = QMessageBox()
@ -340,9 +344,9 @@ class SocketClientApp(QMainWindow):
msg.setWindowTitle("软件版本信息") msg.setWindowTitle("软件版本信息")
# 设置消息框的内容 # 设置消息框的内容
msg.setText("软件版本号2.3\n" msg.setText(f"软件版本号:{self.sw_version}\n"
"支持的分布式节点版本号2.2以上\n" "支持的分布式节点版本号2.2以上\n"
"更新日期2025-11-13") "更新日期2025-11-14")
# 设置按钮 # 设置按钮
msg.setStandardButtons(QMessageBox.Ok) msg.setStandardButtons(QMessageBox.Ok)
@ -397,9 +401,11 @@ class SocketClientApp(QMainWindow):
def connect_to_server(self): def connect_to_server(self):
# self.process_wave_packet('') # self.process_wave_packet('')
if self.connect_button.text() == "断开": if self.connect_button.text() == "断开":
self.try_connect_times = 10
self.connect_button.setText("连接") self.connect_button.setText("连接")
self.socket.disconnectFromHost() self.socket.disconnectFromHost()
elif self.connect_button.text() == "连接": elif self.connect_button.text() == "连接":
self.try_connect_times = 0
ip = self.ip_input.text() ip = self.ip_input.text()
port = int(self.port_input.text()) port = int(self.port_input.text())
self.socket.abort() self.socket.abort()
@ -427,6 +433,18 @@ class SocketClientApp(QMainWindow):
def on_socket_disconnected(self): def on_socket_disconnected(self):
print("on_socket_disconnected") 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.connect_button.setText("连接")
self.status_bar.showMessage("状态: 连接断开") self.status_bar.showMessage("状态: 连接断开")
self.get_data_button.setEnabled(False) self.get_data_button.setEnabled(False)
@ -625,7 +643,7 @@ class SocketClientApp(QMainWindow):
for i in range(min(5, len(data))): # 确保不超过数据长度 for i in range(min(5, len(data))): # 确保不超过数据长度
print(f"{data[i]:.3f}", end=" ") print(f"{data[i]:.3f}", end=" ")
print() # 换行 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 / 10.2
# LSB_32BIT = (2.8 / (2 ** 31)) * ((750 + 287) / 287) * 1000 # LSB_32BIT = (2.8 / (2 ** 31)) * ((750 + 287) / 287) * 1000
self.scaled_data = data * LSB_32BIT self.scaled_data = data * LSB_32BIT