From 5b7a0bb439d8713043d443127b0d0185ba5d2252 Mon Sep 17 00:00:00 2001 From: zhu-mengmeng <15588200382@163.com> Date: Tue, 1 Jul 2025 16:04:18 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E6=96=B0=E5=A2=9E=E4=B8=8A=E6=96=99?= =?UTF-8?q?=E4=B8=8B=E6=96=99=E6=8C=89=E9=92=AE=E8=A7=A6=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/jtDB.db | Bin 139264 -> 139264 bytes from pymodbus.py | 15 +++-- utils/modbus_monitor.py | 6 +- widgets/main_window.py | 124 +++++++++++++++++++++++++++++++++++++++- 4 files changed, 131 insertions(+), 14 deletions(-) diff --git a/db/jtDB.db b/db/jtDB.db index 16e38a2cff4733550efd72ab5a4f681d160b68f6..b642773a13640c8440afe5a42b0af8723408d3ba 100644 GIT binary patch delta 1349 zcmZwHO=}ZT6b9gXC+TD+nl>@9W^C=}s?>)2J;_~Ii{MhhlC8KYbR!h%rl6gIRIsbc zEc_WNT@*tX{WJan*CL)fx4}tz)(`JF!6gW4^z_G@KTm?0_5Ahr zr*JcARCJJ^hn}*;TArp1mI=!Z*wVBW#QH#w5aNE~B0_K-w`^gW zwABME+^&aX%KpD5ZLD^&WNamZq@ylMOanbYNcz+~N=!lOeukLvu?|YY-J^{V+{4j5 zl$Zc&A(Xi$fi-WNf?GM7Lx~T7W)Xs3I%*)4db(R8xWE5KjJG=_8xvI9m9dUeu`EVN z!3P1U2qEdI29!-SA2z}9T`WXm!pCM%Dr!|l2;PfpTm>Pxeda8Zd4)OpGLk}nq4G!|N5fIi6 JxAGnW4gr}?EPVg~ diff --git a/from pymodbus.py b/from pymodbus.py index e61cb9e..9d11579 100644 --- a/from pymodbus.py +++ b/from pymodbus.py @@ -2,20 +2,19 @@ from pymodbus.client import ModbusTcpClient import time client = ModbusTcpClient('localhost', port=5020) client.connect() -client.write_registers(address=11, values=[2247]) -client.write_registers(address=3, values=[0]) - -# client.write_registers(address=20, values=[0]) -# time.sleep(2) -# client.write_registers(address=20, values=[1]) +# client.write_registers(address=11, values=[2247]) +# client.write_registers(address=3, values=[0]) +time.sleep(2) +client.write_registers(address=0, values=[0]) +client.write_registers(address=4, values=[0]) # client.write_registers(address=30, values=[25]) # client.write_registers(address=5, values=[16]) # 贴标完成 # client.write_registers(address=24, values=[1])s -client.write_registers(address=13, values=[1]) +# client.write_registers(address=13, values=[1]) -result = client.read_holding_registers(address=11, count=1) +result = client.read_holding_registers(address=0, count=1) print(result.registers[0],"123===") client.close() \ No newline at end of file diff --git a/utils/modbus_monitor.py b/utils/modbus_monitor.py index ea6b549..8a7570a 100644 --- a/utils/modbus_monitor.py +++ b/utils/modbus_monitor.py @@ -48,12 +48,12 @@ class ModbusMonitor(QObject): register_error = Signal(int, str) monitor_status_changed = Signal(bool, str) - def __init__(self, polling_interval=2.0, max_errors=3, retry_interval=5.0): + def __init__(self, polling_interval=1.0, max_errors=3, retry_interval=5.0): """ 初始化Modbus监控器 Args: - polling_interval: 轮询间隔(秒),默认2.0秒,比原来的1.0秒增加了一倍 + polling_interval: 轮询间隔(秒),默认1.0秒,比原来的1.0秒增加了一倍 max_errors: 最大错误次数,超过此次数将暂停特定寄存器的监控 retry_interval: 重试间隔(秒) """ @@ -77,7 +77,7 @@ class ModbusMonitor(QObject): def _initialize_registers(self): """初始化要监控的寄存器列表""" # 默认监控的寄存器地址 - register_addresses = [5, 6, 11, 13, 20, 21, 22, 23, 24, 25, 30] + register_addresses = [0, 4, 5, 6, 11, 13, 20, 21, 22, 23, 24, 25, 30] for address in register_addresses: self.registers[address] = RegisterValue(address) diff --git a/widgets/main_window.py b/widgets/main_window.py index 681fe57..f20766d 100644 --- a/widgets/main_window.py +++ b/widgets/main_window.py @@ -211,6 +211,12 @@ class MainWindow(MainWindowUI): # 恢复开始按钮原始样式 self.restore_start_button_style() + + # 恢复上料和下料按钮原始样式 + if hasattr(self, 'restore_input_button_style'): + self.restore_input_button_style() + if hasattr(self, 'restore_output_button_style'): + self.restore_output_button_style() def get_axios_num(self,tray_id): """获取托盘号对应的轴号""" @@ -645,8 +651,9 @@ class MainWindow(MainWindowUI): if success3: logging.info(f"停止下料操作:当前层数 {self._current_unload_num}/{self._total_unload_num}") QMessageBox.information(self, "操作提示", "已停止下料操作") - # 恢复按钮原始样式 + # 恢复开始按钮和下料按钮原始样式 self.restore_start_button_style() + self.restore_output_button_style() else: QMessageBox.warning(self, "错误", "停止下料操作失败") else: @@ -657,8 +664,9 @@ class MainWindow(MainWindowUI): self._is_loading_active = False # 标记上料任务已停止 logging.info("停止上料操作") QMessageBox.information(self, "操作提示", "已停止上料操作") - # 恢复按钮原始样式 + # 恢复开始按钮和上料按钮原始样式 self.restore_start_button_style() + self.restore_input_button_style() else: QMessageBox.warning(self, "错误", "停止上料操作失败") @@ -2127,13 +2135,117 @@ class MainWindow(MainWindowUI): # 在这里可以添加错误处理逻辑 pass + # 上料按钮样式控制方法 + def restore_input_button_style(self): + """恢复上料按钮的原始样式""" + try: + button_style = """ + QPushButton { + padding: 8px 16px; + font-weight: bold; + border-radius: 4px; + border: 1px solid #2196f3; + } + QPushButton:hover { + background-color: #bbdefb; + } + """ + self.input_button.setStyleSheet(button_style) + logging.info("已恢复上料按钮原始样式") + except Exception as e: + logging.error(f"恢复上料按钮样式失败: {str(e)}") + + def fill_input_button_style(self): + """填充上料按钮样式 - 蓝色背景,白色字体""" + try: + button_style = """ + QPushButton { + padding: 8px 16px; + font-weight: bold; + border-radius: 4px; + background-color: #2196F3; + color: white; + border: 1px solid #2196F3; + } + QPushButton:hover { + background-color: #1e88e5; + color: white; + } + """ + self.input_button.setStyleSheet(button_style) + logging.info("已填充上料按钮样式") + except Exception as e: + logging.error(f"填充上料按钮样式失败: {str(e)}") + + # 下料按钮样式控制方法 + def restore_output_button_style(self): + """恢复下料按钮的原始样式""" + try: + button_style = """ + QPushButton { + padding: 8px 16px; + font-weight: bold; + border-radius: 4px; + border: 1px solid #ffc107; + } + QPushButton:hover { + background-color: #ffecb3; + } + """ + self.output_button.setStyleSheet(button_style) + logging.info("已恢复下料按钮原始样式") + except Exception as e: + logging.error(f"恢复下料按钮样式失败: {str(e)}") + + def fill_output_button_style(self): + """填充下料按钮样式 - 黄色背景,白色字体""" + try: + button_style = """ + QPushButton { + padding: 8px 16px; + font-weight: bold; + border-radius: 4px; + background-color: #FFC107; + color: white; + border: 1px solid #FFC107; + } + QPushButton:hover { + background-color: #ffb300; + color: white; + } + """ + self.output_button.setStyleSheet(button_style) + logging.info("已填充下料按钮样式") + except Exception as e: + logging.error(f"填充下料按钮样式失败: {str(e)}") + @Slot(int, int) def handle_register_change(self, address, value): """处理寄存器变化""" logging.info(f"[处理] 寄存器D{address}变化: {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}),填充下料按钮样式") + + # 当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 ,恢复下料按钮样式") + # 当D11寄存器变为0时,复位D10寄存器为0 - if address == 11 and value == 0: + elif address == 11 and value == 0: try: logging.info("检测到D11寄存器变为0,正在复位D10寄存器") modbus = ModbusUtils() @@ -2190,6 +2302,9 @@ class MainWindow(MainWindowUI): else: # 上料任务完成,清除状态显示 self.clear_operation_status("input") + # 上料任务完成,恢复上料按钮样式 + self.restore_input_button_style() + logging.info("上料任务完成,恢复上料按钮样式") QMessageBox.information(self, "上料操作", desc) except Exception as e: @@ -2275,6 +2390,9 @@ class MainWindow(MainWindowUI): self.clear_operation_status("output") self.unloading_level_label.setText("下料层数:--") self.unloading_position_label.setText("下料位置:--") + # 下料任务完成,恢复下料按钮样式 + self.restore_output_button_style() + logging.info("下料任务完成,恢复下料按钮样式") elif "请启动" in desc: QMessageBox.information(self, "下料层完成", desc)