将tcp协议修改为opcua协议

This commit is contained in:
2025-11-18 11:22:04 +08:00
parent cfaf1d5d92
commit bd5d933540
8 changed files with 325 additions and 381 deletions

View File

@ -1,6 +1,6 @@
# coding: utf-8
from typing import List
from PySide6.QtCore import Qt, Signal, QEasingCurve, QUrl, QSize, QTimer, QEvent
from PySide6.QtCore import Qt, Signal, QEasingCurve, QUrl, QSize, QTimer, QEvent, QThread
from PySide6.QtGui import QIcon, QDesktopServices, QColor, QPalette, QBrush, QImage
from PySide6.QtWidgets import (QApplication, QHBoxLayout, QVBoxLayout,
QFrame, QWidget, QSpacerItem, QSizePolicy, QMainWindow, QPushButton)
@ -8,7 +8,7 @@ from PySide6.QtWidgets import (QApplication, QHBoxLayout, QVBoxLayout,
from system_nav_bar import SystemNavBar
from bottom_control_widget import BottomControlWidget
from weight_dialog import WeightDetailsDialog
from tcp_client import TcpClient
from opcua_client import OpcuaClient
class MainWindow(QWidget):
@ -17,7 +17,7 @@ class MainWindow(QWidget):
self.initWindow()
self.createSubWidgets() # 创建子部件
self.setupLayout() # 设置布局
self.tcp_client = TcpClient(self) # 创建 TCP 客户端
self.init_opc_client() # 初始化OPC客户端
self.bind_signals() # 绑定信号槽
def initWindow(self):
@ -92,21 +92,32 @@ class MainWindow(QWidget):
self.close()
super().keyPressEvent(event)
# -------------------
# OPC UA 客户端初始化
# -------------------
def init_opc_client(self):
"""初始化 OPC UA 客户端并启动线程避免阻塞UI"""
self.Opcua_client = OpcuaClient(self) # 创建 OPC UA 客户端实例
# 创建独立线程运行 OPC UA 客户端避免阻塞UI线程
self.opc_thread = QThread()
self.Opcua_client.moveToThread(self.opc_thread)
self.opc_thread.started.connect(self.Opcua_client.start_connect) # 线程启动后触发客户端连接
self.opc_thread.start() # 启动线程
# ---------------
# TCP更新重量信息
# OPC UA 信号绑定与数据更新
# ---------------
def bind_signals(self):
"""绑定TCP信号到弹窗更新函数"""
self.tcp_client.weight_updated.connect(self.update_weight_dialogs) # 绑定更新重量信息的信号槽
self.Opcua_client.weight_updated.connect(self.update_weight_dialogs) # 绑定更新重量信息的信号槽
# 连接状态信号:更新状态图标
self.tcp_client.connection_status_changed.connect(self.update_connection_status)
self.Opcua_client.connection_status_changed.connect(self.update_connection_status)
# 上料斗重量信息更新失败信号:更新状态图标
self.tcp_client.upper_weight_error.connect(self.upper_weight_status) # 重量信息更新失败信号
self.Opcua_client.upper_weight_error.connect(self.upper_weight_status) # 重量信息更新失败信号
# 下料斗重量信息更新失败信号:更新状态图标
self.tcp_client.down_weight_error.connect(self.down_weight_status) # 重量信息更新失败信号
self.Opcua_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()
print(f"客户端启动,自动连接服务端{self.Opcua_client.opc_server_url}")
def down_weight_status(self, is_error):
"""发送上料斗错误信息,更新连接状态"""
@ -131,6 +142,14 @@ class MainWindow(QWidget):
down_weight = data.get("hopper_down_weight", 0)
self.weight_dialog2.update_weight(down_weight)
def closeEvent(self, event):
"""窗口关闭时,停止 OPC UA 客户端和线程(避免资源泄露)"""
if hasattr(self, 'Opcua_client'):
self.Opcua_client.stop() # 停止客户端(断开连接、停止定时器)
if hasattr(self, 'opc_thread') and self.opc_thread.isRunning():
self.opc_thread.quit() # 退出线程
self.opc_thread.wait(2000) # 等待线程退出最多2秒
event.accept()
if __name__ == "__main__":
import sys