feat: 修复按钮监听

This commit is contained in:
zhu-mengmeng 2025-07-19 16:51:27 +08:00
parent 387fc11796
commit aa532ef4ec
7 changed files with 137 additions and 4106 deletions

Binary file not shown.

View File

@ -12,10 +12,14 @@ client.write_registers(address=11, values=[3922])
# client.write_registers(address=5, values=[16])
# 贴标完成
# client.write_registers(address=24, values=[1])
client.write_registers(address=13, values=[1])
client.write_registers(address=2, values=[0])
client.write_registers(address=3, values=[0])
result = client.read_holding_registers(address=13, count=1)
print(result.registers[0],"123===")
result1 = client.read_holding_registers(address=3, count=1)
print(result1.registers[0],"123===")
result2 = client.read_holding_registers(address=2, count=1)
print(result2.registers[0],"123===")
client.close()

1
test_register_monitor.py Normal file
View File

@ -0,0 +1 @@

View File

@ -77,7 +77,7 @@ class ModbusMonitor(QObject):
def _initialize_registers(self):
"""初始化要监控的寄存器列表"""
# 默认监控的寄存器地址
register_addresses = [0, 4, 5, 6, 11, 13, 20, 21, 22, 23, 24, 25, 30]
register_addresses = [0, 2, 3, 4, 5, 6, 11, 13, 20, 21, 22, 23, 24, 25, 30]
for address in register_addresses:
self.registers[address] = RegisterValue(address)

View File

@ -302,3 +302,16 @@ class EmergencyStopHandler:
if changed:
logging.info(f"急停状态变化: {desc} (值={value})")
return changed, desc
class RegisterChangeHandler(RegisterHandler):
"""通用寄存器变化处理器用于处理寄存器2和3等"""
def __init__(self, callback=None, address=None):
super().__init__()
self.callback = callback
self.address = address
def handle_change(self, value):
logging.info(f"寄存器D{self.address}变化: {value}")
if self.callback:
self.callback(self.address, value)

View File

@ -20,7 +20,8 @@ from utils.register_handlers import (
Error3Handler,
UnloadingLevelHandler,
UnloadingPositionHandler,
EmergencyStopHandler
EmergencyStopHandler,
RegisterChangeHandler
)
from utils.electricity_monitor import ElectricityHandler
# 导入PySide6
@ -762,7 +763,7 @@ class MainWindow(MainWindowUI):
logging.info(f"开始下料操作:当前层数 {self._current_unload_num}/{self._total_unload_num}")
QMessageBox.information(self, "操作提示", f"开始下料操作:当前第{self._current_unload_num}")
# 填充按钮样式
self.fill_start_button_style()
# self.fill_start_button_style()
else:
QMessageBox.warning(self, "错误", "开始下料操作失败")
else:
@ -775,7 +776,7 @@ class MainWindow(MainWindowUI):
self._is_loading_active = True # 标记上料任务已开始
logging.info(f"开始上料操作:当前层数 {self._current_stow_num}")
# 填充按钮样式
self.fill_start_button_style()
# self.fill_start_button_style()
else:
QMessageBox.warning(self, "错误", "开始上料操作失败")
@ -903,8 +904,25 @@ class MainWindow(MainWindowUI):
order_info[order_info_key] = label.toPlainText()
elif isinstance(label, QComboBox):
order_info[order_info_key] = label.currentText()
elif field_name == "炉号":
# 炉号字段是容器需要找到其中的QLineEdit
luno_container = label
if luno_container:
for child in luno_container.children():
if hasattr(child, 'text') and hasattr(child, 'setText'):
order_info[order_info_key] = child.text()
break
else:
# 如果没有找到子组件,设置为空字符串
order_info[order_info_key] = ""
else:
order_info[order_info_key] = ""
elif hasattr(label, 'text'):
# 其他有text方法的控件
order_info[order_info_key] = label.text()
else:
# 对于没有text方法的控件使用空字符串
order_info[order_info_key] = ""
except RuntimeError as e:
# 如果对象已被删除,记录错误并跳过
logging.warning(f"控件对象已被删除,字段: {field_name}, 错误: {str(e)}")
@ -1027,8 +1045,8 @@ class MainWindow(MainWindowUI):
# 选中新添加的行
self.process_table.selectRow(data_start_row)
# 限制最大行数
self.limit_table_rows(10) # 最多保留10行数据
# 移除行数限制,允许显示更多数据
# self.limit_table_rows(10) # 最多保留10行数据
# 将工程号和托盘号保存到数据库,确保能够正确关联
from dao.inspection_dao import InspectionDAO
@ -1780,6 +1798,10 @@ class MainWindow(MainWindowUI):
monitor.register_handler(4, UnloadingLevelHandler(self.handle_unloading_level))
monitor.register_handler(5, UnloadingPositionHandler(self.handle_unloading_position))
# 注册寄存器2和3的处理器开始按钮样式控制
monitor.register_handler(2, RegisterChangeHandler(self.handle_register_change, 2))
monitor.register_handler(3, RegisterChangeHandler(self.handle_register_change, 3))
# 注册电力消耗处理器并保存引用以便连接信号
self.electricity_handler = ElectricityHandler()
monitor.register_handler(30, self.electricity_handler)
@ -2126,70 +2148,73 @@ class MainWindow(MainWindowUI):
inspection_dao = InspectionDAO()
# 调用接口
gc_api = GcApi()
# 获取订单信息和其他信息,两者都已经是字典格式
info = {}
order_info = inspection_dao.get_order_info(self._current_order_code)
info.update(order_info)
# 获取包装号
# 构建info字典以数据库数据为基础前端数据作为补充
info = {}
# 1. 首先从数据库获取基础订单信息
order_info = inspection_dao.get_order_info(self._current_order_code)
if order_info:
info.update(order_info)
# 2. 从前端获取用户修改的数据,作为补充和更新
for field_name, field_key in self.FIELD_MAPPING.items():
if field_name in self.info_values and self.info_values[field_name] is not None:
try:
# 根据控件类型获取最新值
if isinstance(self.info_values[field_name], QTextEdit):
current_value = self.info_values[field_name].toPlainText().strip()
elif isinstance(self.info_values[field_name], QComboBox):
current_value = self.info_values[field_name].currentText().strip()
elif field_name == "炉号":
# 炉号字段是容器需要找到其中的QLineEdit
luno_container = self.info_values[field_name]
current_value = ""
if luno_container:
for child in luno_container.children():
if hasattr(child, 'text') and hasattr(child, 'setText'):
current_value = child.text().strip()
break
elif hasattr(self.info_values[field_name], 'text'):
current_value = self.info_values[field_name].text().strip()
else:
current_value = ""
# 如果前端有值则更新info字典覆盖数据库中的值
if current_value:
info[field_key] = current_value
logging.debug(f"从前端更新字段 '{field_name}': '{current_value}'")
# 如果前端没有值但数据库有值,保持数据库的值
elif field_key in info:
logging.debug(f"保持数据库字段 '{field_name}': '{info[field_key]}'")
except Exception as e:
logging.warning(f"获取前端字段 '{field_name}' 失败: {str(e)}")
continue
# 3. 添加其他必要的信息
info['xpack'] = self.tray_edit.currentText()
info['spack'] = self.tray_edit.currentText()
order_others_info = inspection_dao.get_order_others_info(gc_note, self._current_order_code, tray_id)
info.update(order_others_info)
info['data_corp'] = order_info['data_corp']
info['zh'] = axios_num
info['mzl'] = weight_kg
info['printsl'] = 1
info['pono'] = self._current_order_code
info["dycz"] = info.get("cz")
info['qd'] = self._current_gc_qd
# 添加库房信息
warehouse_combo = self.info_values.get("库房")
if warehouse_combo:
info['lib'] = warehouse_combo.currentText()
else:
info['lib'] = "成品库" # 默认值
# 添加炉号信息
luno_container = self.info_values.get("炉号")
if luno_container:
# 炉号字段是容器需要找到其中的QLineEdit
for child in luno_container.children():
if hasattr(child, 'text') and hasattr(child, 'setText'):
luno_value = child.text().strip()
if luno_value:
info['luno'] = luno_value
break
else:
info['luno'] = "" # 默认值
# 添加线材类型信息
wire_type_combo = self.info_values.get("线材类型")
if wire_type_combo:
wire_type_value = wire_type_combo.currentText().strip()
if wire_type_value and wire_type_value != "请选择":
info['xclx'] = wire_type_value
else:
info['xclx'] = "" # 默认值
else:
info['xclx'] = "" # 默认值
# 添加机台信息
machine_edit = self.info_values.get("机台")
if machine_edit:
machine_value = machine_edit.text().strip()
if machine_value:
info['jt'] = machine_value
else:
info['jt'] = "" # 默认值
else:
info['jt'] = "" # 默认值
# 添加sc_gch信息
# 4. 添加sc_gch信息
if hasattr(self, '_current_gc_sc_gch') and self._current_gc_sc_gch:
info['sc_gch'] = self._current_gc_sc_gch
else:
info['sc_gch'] = "" # 默认值
info['sc_gch'] = ""
# 5. 从数据库获取其他信息作为补充
order_others_info = inspection_dao.get_order_others_info(gc_note, self._current_order_code, tray_id)
if order_others_info:
info.update(order_others_info)
# 6. 确保data_corp字段存在
if 'data_corp' not in info and order_info and 'data_corp' in order_info:
info['data_corp'] = order_info['data_corp']
# 获取本机IP地址
import socket
try:
@ -2484,37 +2509,44 @@ class MainWindow(MainWindowUI):
"""处理寄存器变化"""
logging.info(f"[处理] 寄存器D{address}变化: {value}")
# 当D0寄存器有值时填充上料按钮样式
if address == 0 and value > 0:
self.fill_input_button_style()
logging.info(f"D0寄存器有值({value}),填充上料按钮样式")
# # 当D0寄存器有值时填充上料按钮样式
# if address == 0 and value > 0:
# self.fill_input_button_style()
# logging.info(f"D0寄存器有值({value}),填充上料按钮样式")
# 当D4寄存器有值时填充下料按钮样式
elif address == 4 and value > 0:
self.fill_output_button_style()
logging.info(f"D4寄存器有值({value}),填充下料按钮样式")
# # 当D4寄存器有值时填充下料按钮样式
# elif address == 4 and value > 0:
# self.fill_output_button_style()
# logging.info(f"D4寄存器有值({value}),填充下料按钮样式")
# 当D0寄存器为 0 时,恢复上料按钮样式
if address == 0 and value == 0:
# # 当D0寄存器为 0 时,恢复上料按钮样式
# if address == 0 and value == 0:
# self.restore_input_button_style()
# logging.info(f"D0寄存器为 0 ,恢复上料按钮样式")
# # 当D4寄存器为 0 时,恢复下料按钮样式
# elif address == 4 and value == 0:
# self.restore_output_button_style()
# logging.info(f"D4寄存器为 0 ,恢复下料按钮样式")
# D2寄存器控制上料按钮样式
if address == 2 and value == 0:
self.restore_input_button_style()
logging.info(f"D0寄存器为 0 ,恢复上料按钮样式")
# 当D4寄存器为 0 时,恢复下料按钮样式
elif address == 4 and value == 0:
self.restore_output_button_style()
logging.info(f"D4寄存器为 0 ,恢复下料按钮样式")
elif address == 2 and value == 0:
logging.info(f"D2寄存器为 0 ,恢复上料按钮样式")
elif address == 2 and value == 1:
self.fill_input_button_style()
self.fill_start_button_style()
logging.info(f"D2寄存器为 1 ,填充上料按钮样式")
elif address == 2 and value == 0 and address == 3 and value == 0:
self.restore_start_button_style()
logging.info(f"D2寄存器为 0 ,恢复开始按钮样式")
logging.info(f"D2寄存器为 0 D3寄存器为 0 ,恢复开始按钮样式")
# D3寄存器控制下料按钮样式
elif address == 3 and value == 0:
self.restore_start_button_style()
logging.info(f"D3寄存器为 0 ,恢复开始按钮样式")
elif address ==2 and value == 1:
self.fill_start_button_style()
logging.info(f"D2寄存器为 1 ,填充开始按钮样式")
self.restore_output_button_style()
logging.info(f"D3寄存器为 0 ,恢复下料按钮样式")
elif address == 3 and value == 1:
self.fill_output_button_style()
self.fill_start_button_style()
logging.info(f"D3寄存器为 1 ,填充开始按钮样式")
logging.info(f"D3寄存器为 1 ,填充下料按钮样式")
# 当D11寄存器变为0时复位D10寄存器为0
elif address == 11 and value == 0:
try:

File diff suppressed because it is too large Load Diff