diff --git a/config/config.json b/config/config.json index 476559d..68e270e 100644 --- a/config/config.json +++ b/config/config.json @@ -1,4 +1,4 @@ { - "hopper_up_weight": 1111, - "hopper_down_weight": 6524 + "hopper_up_weight": "666666", + "hopper_down_weight": "5444444" } diff --git a/main_window.py b/main_window.py index eae48b4..7e65f2a 100644 --- a/main_window.py +++ b/main_window.py @@ -98,16 +98,28 @@ class MainWindow(QWidget): def bind_signals(self): """绑定TCP信号到弹窗更新函数""" self.tcp_client.weight_updated.connect(self.update_weight_dialogs) # 绑定更新重量信息的信号槽 - # 连接状态信号:更新按钮状态和弹窗图标 + # 连接状态信号:更新状态图标 self.tcp_client.connection_status_changed.connect(self.update_connection_status) + # 上料斗重量信息更新失败信号:更新状态图标 + self.tcp_client.upper_weight_error.connect(self.upper_weight_status) # 重量信息更新失败信号 + # 下料斗重量信息更新失败信号:更新状态图标 + self.tcp_client.down_weight_error.connect(self.down_weight_status) # 重量信息更新失败信号 # 启动 TCP 连接 print(f"客户端启动,自动连接服务端{self.tcp_client.tcp_server_host}:{self.tcp_client.tcp_server_port}...") self.tcp_client._connect_to_server() + def down_weight_status(self, is_error): + """发送上料斗错误信息,更新连接状态""" + self.weight_dialog2._update_status_icon2(is_error) + + def upper_weight_status(self, is_error): + """发送下料斗错误信息,更新连接状态""" + self.weight_dialog1._update_status_icon2(is_error) + def update_connection_status(self, is_connected): - """更新连接状态""" - self.weight_dialog1._update_status_icon(is_connected) - self.weight_dialog2._update_status_icon(is_connected) + """根据服务端是否连接,更新连接状态""" + self.weight_dialog1._update_status_icon1(is_connected) + self.weight_dialog2._update_status_icon1(is_connected) def update_weight_dialogs(self, data): """更新上料斗、下料斗重量显示""" @@ -124,6 +136,6 @@ if __name__ == "__main__": import sys app = QApplication([]) window = MainWindow() - # window.show() # 显示主界面 - window.showFullScreen() # 全屏显示主界面 + window.show() # 显示主界面 + # window.showFullScreen() # 全屏显示主界面 sys.exit(app.exec()) diff --git a/tcp_client.py b/tcp_client.py index 1a534c5..558c239 100644 --- a/tcp_client.py +++ b/tcp_client.py @@ -22,6 +22,8 @@ class TcpClient(QObject): # 信号,用于向主窗口/重量弹窗发送最新重量数据 weight_updated = Signal(dict) # 重量更新信号 connection_status_changed = Signal(bool) # 连接状态更新信号 + upper_weight_error = Signal(bool) # 上料斗重量异常信号 + down_weight_error = Signal(bool) # 下料斗重量异常信号 def __init__(self, parent=None): super().__init__(parent) @@ -96,6 +98,9 @@ class TcpClient(QObject): # 发送连接状态信号 self.connection_status_changed.emit(True) + # 重量错误状态信号(连接成功后恢复正常图标) + self.upper_weight_error.emit(False) + self.down_weight_error.emit(False) # 连接成功后,向服务器发送“请求初始数据”指令 self._send_tcp_request("get_initial_data") @@ -110,7 +115,7 @@ class TcpClient(QObject): self.connection_status_changed.emit(False) # 发送重量清零信号 - self.weight_updated.emit({"hopper_up_weight": 0, "hopper_down_weight": 0}) + self.weight_updated.emit({"hopper_up_weight": 0, "hopper_down_weight": ""}) if not self.reconnect_timer.isActive(): # 未启动重连定时器时才启动 print(f"连接断开,将在{RECONNECT_INTERVAL / 1000}秒后启动重连...") @@ -126,7 +131,28 @@ class TcpClient(QObject): try: status_data = json.loads(tcp_data) self.latest_json_data = status_data - self.weight_updated.emit(status_data) + + # 错误检测 + up_weight = status_data.get("hopper_up_weight") + down_weight = status_data.get("hopper_down_weight") + + # 顺序不能变 + if up_weight == "error" and down_weight == "error": + self.weight_updated.emit({"hopper_up_weight": "", "hopper_down_weight": ""}) + self.upper_weight_error.emit(True) + self.down_weight_error.emit(True) + elif up_weight == "error": + self.weight_updated.emit({"hopper_up_weight": "", "hopper_down_weight": down_weight}) + self.upper_weight_error.emit(True) + elif down_weight == "error": + self.weight_updated.emit({"hopper_up_weight": up_weight, "hopper_down_weight": ""}) + self.down_weight_error.emit(True) + else: + # 防止打乱服务端连接时的状态图标变换 + self.weight_updated.emit(status_data) + self.upper_weight_error.emit(False) + self.down_weight_error.emit(False) + except json.JSONDecodeError as e: print(f"TCP数据解析失败(非JSON格式):{e}, 原始数据:{tcp_data}") except Exception as e: @@ -143,6 +169,9 @@ class TcpClient(QObject): # 发送连接断开信号 self.connection_status_changed.emit(False) + # 错误时重置重量和错误状态 + self.weight_updated.emit({"hopper_up_weight": "", "hopper_down_weight": ""}) + self.weight_error_signal.emit(False) # 首次连接失败时,启动重连定时器 if not self.reconnect_timer.isActive(): diff --git a/weight_dialog.py b/weight_dialog.py index 80f4ee3..29f5f56 100644 --- a/weight_dialog.py +++ b/weight_dialog.py @@ -105,7 +105,8 @@ class WeightDetailsDialog(QDialog): # 2、创建确认图标 self.check_label = QLabel() - self._update_status_icon(False) + self._update_status_icon1(False) + self._update_status_icon2(False) weight_layout.addWidget(self.check_label) weight_layout.setSpacing(50) @@ -197,7 +198,7 @@ class WeightDetailsDialog(QDialog): for i in range(min(len(self.digit_labels), 6)): self.digit_labels[i].setText(weight_str[i] if i < len(weight_str) else "0") - def _update_status_icon(self, is_connected): + def _update_status_icon1(self, is_connected): """根据连接状态切换图标""" if not self.check_label: return @@ -209,6 +210,18 @@ class WeightDetailsDialog(QDialog): else: print(f"错误:{icon_path} 加载失败,请检查路径!") + def _update_status_icon2(self, is_error): + """根据是否有错误信息,切换图标""" + if not self.check_label: + return + icon_path = self.default_icon_path if is_error else self.connected_icon_path + pixmap = QPixmap(icon_path) + if not pixmap.isNull(): + self.check_label.setPixmap(pixmap.scaled( + pixmap.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation)) + else: + print(f"错误:{icon_path} 加载失败,请检查路径!") + # -------------------- # 清空界面信息的通用方法 # --------------------