feat: 更新Modbus寄存器写入逻辑,调整托盘层数和状态反馈,优化加载对话框与主窗口的交互

This commit is contained in:
zhu-mengmeng 2025-06-22 02:03:27 +08:00
parent 26e835e25a
commit f5c813e7a4
4 changed files with 97 additions and 30 deletions

Binary file not shown.

View File

@ -2,10 +2,10 @@ from pymodbus.client import ModbusTcpClient
client = ModbusTcpClient('localhost', port=5020) client = ModbusTcpClient('localhost', port=5020)
client.connect() client.connect()
client.write_registers(address=11, values=[2222]) client.write_registers(address=11, values=[2223])
client.write_registers(address=13, values=[1]) client.write_registers(address=13, values=[0])
# client.write_registers(address=6, values=[1]) client.write_registers(address=20, values=[1])
# client.write_registers(address=5, values=[16]) # client.write_registers(address=5, values=[16])
# 贴标完成 # 贴标完成
# client.write_registers(address=24, values=[1]) # client.write_registers(address=24, values=[1])

View File

@ -95,7 +95,8 @@ class LoadingDialog(LoadingDialogUI):
if(xpack_response.get("status",False)): if(xpack_response.get("status",False)):
xpack = xpack_response['xpack'] xpack = xpack_response['xpack']
self.tray_input.setText(xpack) self.tray_input.setText(xpack)
# 发送托盘号到主窗口 self.pallet_tier_value.setText('3')
# 发送托盘号到主窗
from widgets.main_window import MainWindow from widgets.main_window import MainWindow
main_window = self.parent main_window = self.parent
if main_window and isinstance(main_window, MainWindow): if main_window and isinstance(main_window, MainWindow):

View File

@ -27,7 +27,7 @@ from PySide6.QtWidgets import (
QTableWidget, QMenu, QComboBox, QFormLayout, QDialog, QVBoxLayout, QHBoxLayout, QPushButton, QTableWidget, QMenu, QComboBox, QFormLayout, QDialog, QVBoxLayout, QHBoxLayout, QPushButton,
QStatusBar, QSplitter, QFrame, QHeaderView QStatusBar, QSplitter, QFrame, QHeaderView
) )
from PySide6.QtCore import Qt, QTimer, Slot from PySide6.QtCore import Qt, QTimer, Slot, Signal
from PySide6.QtGui import QBrush, QColor from PySide6.QtGui import QBrush, QColor
import time import time
@ -47,6 +47,9 @@ from utils.serial_manager import SerialManager
class MainWindow(MainWindowUI): class MainWindow(MainWindowUI):
"""主窗口""" """主窗口"""
# 定义信号作为类变量
loading_feedback_signal = Signal(str, str) # 参数status_type, desc
def __init__(self, user_id=None, user_name=None, corp_name=None, corp_id=None): def __init__(self, user_id=None, user_name=None, corp_name=None, corp_id=None):
super().__init__(user_name) super().__init__(user_name)
@ -58,6 +61,13 @@ class MainWindow(MainWindowUI):
self._loading_data_in_progress = False # 数据加载状态标志,防止循环调用 self._loading_data_in_progress = False # 数据加载状态标志,防止循环调用
self._current_order_code = None # 存储当前订单号 self._current_order_code = None # 存储当前订单号
# 添加拆垛相关的属性
self._current_stow_num = 0 # 当前拆垛层数
self._loading_info = None # 存储上料对话框的信息
# 连接信号到槽
self.loading_feedback_signal.connect(self._handle_loading_feedback_ui)
# 称重相关变量 # 称重相关变量
self._current_weight = None # 当前称重值(千克) self._current_weight = None # 当前称重值(千克)
self._last_weight_time = None # 最后一次称重时间 self._last_weight_time = None # 最后一次称重时间
@ -377,6 +387,18 @@ class MainWindow(MainWindowUI):
from widgets.loading_dialog_widget import LoadingDialog from widgets.loading_dialog_widget import LoadingDialog
dialog = LoadingDialog(parent=self,user_id=self.user_id,user_name=self.user_name,corp_id=self.corp_id) dialog = LoadingDialog(parent=self,user_id=self.user_id,user_name=self.user_name,corp_id=self.corp_id)
# 如果已有上料信息,则填充到对话框
if self._loading_info and self._current_stow_num > 0:
dialog.order_input.setText(self._loading_info.get('order_code', ''))
dialog.tray_input.setText(self._loading_info.get('tray_code', ''))
dialog.axis_value.setText(self._loading_info.get('axis_value', '--'))
dialog.quantity_value.setText(self._loading_info.get('quantity_value', '--'))
dialog.weight_value.setText(self._loading_info.get('weight_value', '--'))
dialog.pallet_tier_value.setText(str(self._current_stow_num))
# 禁用输入框,防止修改
dialog.order_input.setEnabled(False)
dialog.tray_input.setEnabled(False)
# 连接订单号信号 # 连接订单号信号
dialog.order_code_signal.connect(self.handle_order_code_received) dialog.order_code_signal.connect(self.handle_order_code_received)
@ -390,19 +412,32 @@ class MainWindow(MainWindowUI):
if stow_num == "--" or not stow_num: if stow_num == "--" or not stow_num:
QMessageBox.warning(self, "错误", "未获取到托盘料信息,请重试") QMessageBox.warning(self, "错误", "未获取到托盘料信息,请重试")
return return
# 如果是新的上料操作没有存储的信息或层数为0
if not self._loading_info or self._current_stow_num == 0:
self._current_stow_num = int(stow_num)
# 保存上料信息
self._loading_info = {
'order_code': dialog.order_input.text(),
'tray_code': dialog.tray_input.text(),
'axis_value': dialog.axis_value.text(),
'quantity_value': dialog.quantity_value.text(),
'weight_value': dialog.weight_value.text(),
}
# 执行Modbus操作 # 执行Modbus操作
modbus = ModbusUtils() modbus = ModbusUtils()
client = modbus.get_client() client = modbus.get_client()
try: try:
# 上料 D2 寄存器写入 1, D0 寄存器写入拆垛层数 # 上料 D2 寄存器写入 1, D0 寄存器写入拆垛层数
success0 = modbus.write_register_until_success(client, 0, int(stow_num)) success0 = modbus.write_register_until_success(client, 0, self._current_stow_num)
success2 = modbus.write_register_until_success(client, 2, 1) success2 = modbus.write_register_until_success(client, 2, 1)
success3 = modbus.write_register_until_success(client, 3, 1) success3 = modbus.write_register_until_success(client, 3, 1)
# 上料 D2 寄存器写入 1, D0 寄存器写入拆垛层数 # 上料 D2 寄存器写入 1, D0 寄存器写入拆垛层数
if success0 and success2 and success3: if success0 and success2 and success3:
# 创建状态标签并显示在右上角 # 创建状态标签并显示在右上角
self.show_operation_status("拆垛层数", "input", stow_num) self.show_operation_status("拆垛层数", "input", str(self._current_stow_num))
else: else:
QMessageBox.information(self, "操作提示", "上料失败") QMessageBox.information(self, "操作提示", "上料失败")
except Exception as e: except Exception as e:
@ -482,15 +517,28 @@ class MainWindow(MainWindowUI):
def handle_start(self): def handle_start(self):
""" """
2,3 寄存器设置为 1 处理开始按钮点击事件
每次点击开始时将当前层数写入D0寄存器并将D2,D3寄存器设置为1
""" """
if self._current_stow_num <= 0:
QMessageBox.warning(self, "提示", "请先进行上料操作")
return
modbus = ModbusUtils() modbus = ModbusUtils()
client = modbus.get_client() client = modbus.get_client()
try: try:
# 写入当前层数到D0寄存器
success0 = modbus.write_register_until_success(client, 0, self._current_stow_num)
success2 = modbus.write_register_until_success(client, 2, 1) success2 = modbus.write_register_until_success(client, 2, 1)
success3 = modbus.write_register_until_success(client, 3, 1) success3 = modbus.write_register_until_success(client, 3, 1)
if success0 and success2 and success3:
logging.info(f"开始操作:当前层数 {self._current_stow_num}")
else:
QMessageBox.warning(self, "错误", "开始操作失败")
except Exception as e: except Exception as e:
logging.error(f"启动操作失败: {str(e)}") logging.error(f"开始操作失败: {str(e)}")
QMessageBox.critical(self, "错误", f"开始操作失败: {str(e)}")
finally: finally:
modbus.close_client(client) modbus.close_client(client)
@ -1851,8 +1899,26 @@ class MainWindow(MainWindowUI):
client = modbus.get_client() client = modbus.get_client()
modbus.write_register_until_success(client, 2, 0) modbus.write_register_until_success(client, 2, 0)
modbus.close_client(client) modbus.close_client(client)
self.show_operation_status("上料完成", "input", desc)
QMessageBox.information(self, "上料操作", f"上料操作已完成: {desc}") # 更新当前层数
if self._current_stow_num > 0:
self._current_stow_num -= 1
if self._current_stow_num == 0:
# 如果所有层都完成,清空上料信息
self._loading_info = None
logging.info("所有层拆垛完成,清空上料信息")
else:
logging.info(f"当前层拆垛完成,剩余层数: {self._current_stow_num}")
# 通过信号触发UI更新
self.loading_feedback_signal.emit("input", desc)
def _handle_loading_feedback_ui(self, status_type, desc):
"""在主线程中处理UI更新"""
try:
self.show_operation_status("上料完成", status_type, desc)
except Exception as e:
logging.error(f"处理UI更新失败: {str(e)}")
@Slot(int, str) @Slot(int, str)
def handle_unloading_feedback(self, status, desc): def handle_unloading_feedback(self, status, desc):