From b33d83a2879066b14695d878996724500975a914 Mon Sep 17 00:00:00 2001 From: "COT001\\DEV" <871066422@qq.com> Date: Thu, 26 Mar 2026 11:22:24 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=82=E9=85=8D=E5=B0=8F=E6=89=B3=E6=89=8B?= =?UTF-8?q?=EF=BC=8C=E7=89=88=E6=9C=AC=E5=BE=85=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.json | 4 ++++ frontend/wrench_gui.py | 23 ++++++++++++++++------- wrench_controller.py | 30 +++++++++++++++++++----------- 3 files changed, 39 insertions(+), 18 deletions(-) diff --git a/config.json b/config.json index 4bb4478..7c7988d 100644 --- a/config.json +++ b/config.json @@ -24,6 +24,10 @@ "enabled": false, "description": "测试模式:失败也算成功,用于无钉子测试" }, + "torque_display": { + "decimal_mode": true, + "description": "扭矩显示模式:decimal_mode=true时显示值除以10(如350显示为35.0),false时原样显示" + }, "bolt_default_config": { "mode": 1, "torque_tolerance": 0.10, diff --git a/frontend/wrench_gui.py b/frontend/wrench_gui.py index e9620c6..df11ec0 100644 --- a/frontend/wrench_gui.py +++ b/frontend/wrench_gui.py @@ -100,6 +100,15 @@ class WrenchGUI: # 开始轮询 self.start_polling() + def format_torque(self, torque_value): + """格式化扭矩显示值""" + if torque_value is None or torque_value == "--": + return torque_value + decimal_mode = self.config.get('torque_display', {}).get('decimal_mode', False) + if decimal_mode: + return torque_value / 10 + return torque_value + def _create_widgets(self): """创建界面组件""" # 使用左右布局:左侧工单列表,右侧其他内容 @@ -714,7 +723,7 @@ class WrenchGUI: self.tree.insert("", tk.END, values=( bolt.get('bolt_id'), bolt.get('name'), - bolt.get('target_torque'), + self.format_torque(bolt.get('target_torque')), "待认领", "--", "--" @@ -953,7 +962,7 @@ class WrenchGUI: self.tree.insert("", tk.END, values=( bolt.get('bolt_id'), bolt.get('name'), - bolt.get('target_torque'), + self.format_torque(bolt.get('target_torque')), "待拧紧", "--", "--" @@ -1089,14 +1098,14 @@ class WrenchGUI: bolt_name = bolt.get('name') target_torque = bolt.get('target_torque') - self.log(f"开始拧紧: [{bolt_id}] {bolt_name}, 目标扭矩: {target_torque} Nm") + self.log(f"开始拧紧: [{bolt_id}] {bolt_name}, 目标扭矩: {self.format_torque(target_torque)} Nm") # 更新界面 self.root.after(0, lambda: self.update_current_bolt(bolt, "拧紧中...")) self.root.after(0, lambda: self.update_tree_item(index, "拧紧中", "running")) # 设定参数 - self.log(f"设定参数: 扭矩={target_torque}Nm, 模式=M{bolt.get('mode', 1)}") + self.log(f"设定参数: 扭矩={self.format_torque(target_torque)}Nm, 模式=M{bolt.get('mode', 1)}") self.wrench.set_torque_parameters( target_torque=target_torque, mode=bolt.get('mode', 1), @@ -1130,7 +1139,7 @@ class WrenchGUI: actual_angle = result.get("actual_angle", 0) timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") - self.log(f"✅ 作业 {bolt_id} 拧紧成功! 实际扭矩: {actual_torque} Nm", "SUCCESS") + self.log(f"✅ 作业 {bolt_id} 拧紧成功! 实际扭矩: {self.format_torque(actual_torque)} Nm", "SUCCESS") # 更新界面 self.root.after(0, lambda at=actual_torque, ts=timestamp: self.update_tree_item( @@ -1232,7 +1241,7 @@ class WrenchGUI: def update_current_bolt(self, bolt, status): """更新当前作业显示""" self.current_bolt_label.config(text=f"[{bolt.get('bolt_id')}] {bolt.get('name')}") - self.current_torque_label.config(text=f"目标扭矩: {bolt.get('target_torque')} Nm") + self.current_torque_label.config(text=f"目标扭矩: {self.format_torque(bolt.get('target_torque'))} Nm") self.current_status_label.config(text=f"状态: {status}") def update_tree_item(self, index, status, tag, actual_torque="--", timestamp="--"): @@ -1243,7 +1252,7 @@ class WrenchGUI: values = list(self.tree.item(item)['values']) values[3] = status if actual_torque != "--": - values[4] = actual_torque + values[4] = self.format_torque(actual_torque) if timestamp != "--": values[5] = timestamp self.tree.item(item, values=values, tags=(tag,)) diff --git a/wrench_controller.py b/wrench_controller.py index b96d1e8..efe515b 100644 --- a/wrench_controller.py +++ b/wrench_controller.py @@ -331,6 +331,13 @@ class WrenchController: if response: print(f"✅ 收到完整响应 ({len(response)}字节): {response.hex(' ').upper()}") + # 尝试解析实际扭矩(如果响应足够长) + if len(response) >= 31: + try: + actual_torque = struct.unpack('>H', response[29:31])[0] + print(f"⭐⭐⭐ 实际扭矩(位置29-30): {actual_torque} (原始值) ⭐⭐⭐") + except: + pass return response else: print("❌ 收到空响应(连接可能已关闭)") @@ -531,8 +538,8 @@ class WrenchController: :param response: 接收到的响应报文 :return: 解析后的结果字典 """ - if not response or len(response) < 44: - return {"error": "响应数据不完整"} + if not response or len(response) < 31: + return {"error": f"响应数据过短,仅{len(response) if response else 0}字节,至少需要31字节"} # 验证报文头和功能码 if response[0:2] != b'\xC5\xC5': @@ -598,21 +605,22 @@ class WrenchController: mode = "M1(扭矩模式)" if response[26] == 0x01 else "M2(角度模式)" - # 解析扭矩和角度值 - target_torque = struct.unpack('>H', response[27:29])[0] - actual_torque = struct.unpack('>H', response[29:31])[0] - target_angle = struct.unpack('>H', response[31:33])[0] - actual_angle = struct.unpack('>H', response[33:35])[0] - torque_max = struct.unpack('>H', response[35:37])[0] - torque_min = struct.unpack('>H', response[37:39])[0] - angle_max = struct.unpack('>H', response[39:41])[0] - angle_min = struct.unpack('>H', response[41:43])[0] + # 解析扭矩和角度值(安全解析,检查长度) + target_torque = struct.unpack('>H', response[27:29])[0] if len(response) >= 29 else 0 + actual_torque = struct.unpack('>H', response[29:31])[0] if len(response) >= 31 else 0 + target_angle = struct.unpack('>H', response[31:33])[0] if len(response) >= 33 else 0 + actual_angle = struct.unpack('>H', response[33:35])[0] if len(response) >= 35 else 0 + torque_max = struct.unpack('>H', response[35:37])[0] if len(response) >= 37 else 0 + torque_min = struct.unpack('>H', response[37:39])[0] if len(response) >= 39 else 0 + angle_max = struct.unpack('>H', response[39:41])[0] if len(response) >= 41 else 0 + angle_min = struct.unpack('>H', response[41:43])[0] if len(response) >= 43 else 0 # 详细打印扭矩和角度数据 print(f"\n{'='*70}") print(f"🔧 扭矩和角度数据解析:") print(f" [27-28] 目标扭矩: {target_torque} (原始值, HEX: {response[27:29].hex(' ').upper()})") print(f" [29-30] 实际扭矩: {actual_torque} (原始值, HEX: {response[29:31].hex(' ').upper()})") + print(f"\n⭐⭐⭐ 实际扭矩值: {actual_torque} ⭐⭐⭐") print(f" [31-32] 目标角度: {target_angle}° (HEX: {response[31:33].hex(' ').upper()})") print(f" [33-34] 实际角度: {actual_angle}° (HEX: {response[33:35].hex(' ').upper()})") print(f" [35-36] 扭矩上限: {torque_max} (HEX: {response[35:37].hex(' ').upper()})")