141 lines
5.0 KiB
Python
141 lines
5.0 KiB
Python
|
|
import logging
|
||
|
|
import json
|
||
|
|
import os
|
||
|
|
from PySide6.QtWidgets import QWidget, QMessageBox
|
||
|
|
from PySide6.QtCore import Signal
|
||
|
|
from utils.config_loader import ConfigLoader
|
||
|
|
|
||
|
|
class PLCSettingsWidget(QWidget):
|
||
|
|
"""PLC设置控制器"""
|
||
|
|
|
||
|
|
# 定义信号
|
||
|
|
settings_changed = Signal() # 设置变更信号
|
||
|
|
|
||
|
|
def __init__(self, parent=None):
|
||
|
|
"""初始化控制器
|
||
|
|
|
||
|
|
Args:
|
||
|
|
parent: 父窗口
|
||
|
|
"""
|
||
|
|
super().__init__(parent)
|
||
|
|
self.parent = parent
|
||
|
|
self.config = ConfigLoader.get_instance()
|
||
|
|
|
||
|
|
# 获取UI控件引用
|
||
|
|
self.modbus_host_input = parent.modbus_host_input
|
||
|
|
self.modbus_port_input = parent.modbus_port_input
|
||
|
|
self.modbus_test_button = parent.modbus_test_button
|
||
|
|
self.plc_save_button = parent.plc_save_button
|
||
|
|
|
||
|
|
# 连接信号和槽
|
||
|
|
self.connect_signals()
|
||
|
|
|
||
|
|
# 加载配置
|
||
|
|
self.load_config()
|
||
|
|
|
||
|
|
def connect_signals(self):
|
||
|
|
"""连接信号和槽"""
|
||
|
|
self.modbus_test_button.clicked.connect(self.test_modbus_connection)
|
||
|
|
self.plc_save_button.clicked.connect(self.save_config)
|
||
|
|
|
||
|
|
def load_config(self):
|
||
|
|
"""加载配置"""
|
||
|
|
try:
|
||
|
|
# 加载Modbus配置
|
||
|
|
host = self.config.get_value("modbus.host")
|
||
|
|
port = self.config.get_value("modbus.port")
|
||
|
|
|
||
|
|
self.modbus_host_input.setText(host if host else "localhost")
|
||
|
|
self.modbus_port_input.setText(str(port) if port else "502")
|
||
|
|
|
||
|
|
logging.info("已加载PLC配置")
|
||
|
|
except Exception as e:
|
||
|
|
logging.error(f"加载PLC配置失败: {str(e)}")
|
||
|
|
QMessageBox.critical(self, "错误", f"加载PLC配置失败: {str(e)}")
|
||
|
|
|
||
|
|
def save_config(self):
|
||
|
|
"""保存配置"""
|
||
|
|
try:
|
||
|
|
# 获取输入值
|
||
|
|
host = self.modbus_host_input.text().strip()
|
||
|
|
port = self.modbus_port_input.text().strip()
|
||
|
|
|
||
|
|
# 验证输入
|
||
|
|
if not host:
|
||
|
|
QMessageBox.warning(self, "警告", "请输入Modbus主机地址")
|
||
|
|
return
|
||
|
|
|
||
|
|
if not port:
|
||
|
|
QMessageBox.warning(self, "警告", "请输入Modbus端口")
|
||
|
|
return
|
||
|
|
|
||
|
|
try:
|
||
|
|
port_int = int(port)
|
||
|
|
if port_int < 1 or port_int > 65535:
|
||
|
|
QMessageBox.warning(self, "警告", "端口号必须在1-65535之间")
|
||
|
|
return
|
||
|
|
except ValueError:
|
||
|
|
QMessageBox.warning(self, "警告", "端口号必须是整数")
|
||
|
|
return
|
||
|
|
|
||
|
|
# 更新配置
|
||
|
|
self.config.set_value("modbus.host", host)
|
||
|
|
self.config.set_value("modbus.port", port)
|
||
|
|
self.config.save_config()
|
||
|
|
|
||
|
|
# 发送信号
|
||
|
|
self.settings_changed.emit()
|
||
|
|
|
||
|
|
# 提示成功
|
||
|
|
QMessageBox.information(self, "成功", "PLC配置已保存")
|
||
|
|
logging.info(f"已保存PLC配置: host={host}, port={port}")
|
||
|
|
except Exception as e:
|
||
|
|
logging.error(f"保存PLC配置失败: {str(e)}")
|
||
|
|
QMessageBox.critical(self, "错误", f"保存PLC配置失败: {str(e)}")
|
||
|
|
|
||
|
|
def test_modbus_connection(self):
|
||
|
|
"""测试Modbus连接"""
|
||
|
|
try:
|
||
|
|
# 获取输入值
|
||
|
|
host = self.modbus_host_input.text().strip()
|
||
|
|
port = self.modbus_port_input.text().strip()
|
||
|
|
|
||
|
|
# 验证输入
|
||
|
|
if not host:
|
||
|
|
QMessageBox.warning(self, "警告", "请输入Modbus主机地址")
|
||
|
|
return
|
||
|
|
|
||
|
|
if not port:
|
||
|
|
QMessageBox.warning(self, "警告", "请输入Modbus端口")
|
||
|
|
return
|
||
|
|
|
||
|
|
try:
|
||
|
|
port_int = int(port)
|
||
|
|
except ValueError:
|
||
|
|
QMessageBox.warning(self, "警告", "端口号必须是整数")
|
||
|
|
return
|
||
|
|
|
||
|
|
# 导入ModbusUtils
|
||
|
|
from utils.modbus_utils import ModbusUtils
|
||
|
|
|
||
|
|
# 创建Modbus连接
|
||
|
|
modbus = ModbusUtils(host=host, port=port_int)
|
||
|
|
client = modbus.get_client()
|
||
|
|
|
||
|
|
if client:
|
||
|
|
QMessageBox.information(self, "成功", f"Modbus连接成功: {host}:{port}")
|
||
|
|
logging.info(f"Modbus连接测试成功: {host}:{port}")
|
||
|
|
else:
|
||
|
|
QMessageBox.warning(self, "警告", f"Modbus连接失败: {host}:{port}")
|
||
|
|
logging.warning(f"Modbus连接测试失败: {host}:{port}")
|
||
|
|
|
||
|
|
# 关闭连接
|
||
|
|
modbus.close_client(client)
|
||
|
|
except Exception as e:
|
||
|
|
logging.error(f"测试Modbus连接失败: {str(e)}")
|
||
|
|
QMessageBox.critical(self, "错误", f"测试Modbus连接失败: {str(e)}")
|
||
|
|
|
||
|
|
def on_settings_changed(self):
|
||
|
|
"""设置变更处理"""
|
||
|
|
# 重新加载配置
|
||
|
|
self.load_config()
|