TorqueWrench/test_wrench_status.py

129 lines
4.4 KiB
Python
Raw Normal View History

2026-03-18 17:13:20 +08:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
扳手状态测试Demo - 打印所有成功和失败状态
"""
from wrench_controller import WrenchController
import time
def main():
print("="*60)
print("扳手状态测试Demo")
print("="*60)
# 创建控制器
wrench = WrenchController()
# 连接扳手
if not wrench.connect():
print("❌ 连接失败,退出")
return
try:
# 启用远程控制
print("\n1. 启用远程控制...")
wrench.enable_remote_control(True)
time.sleep(1)
# 设置扭矩参数
print("\n2. 设置扭矩参数...")
target_torque = 50 # 50 Nm
wrench.set_torque_parameters(
target_torque=target_torque,
mode=1, # M1模式
torque_tolerance=0.10 # ±10%
)
# 启动扳手
print("\n3. 启动扳手...")
wrench.start_wrench(direction=1)
# 等待结果(可能需要多次接收)
print("\n4. 等待扳手执行结果...")
print("="*60)
result = None
max_attempts = 10
for attempt in range(max_attempts):
print(f"\n尝试接收 {attempt + 1}/{max_attempts}...")
response = wrench._receive_response(timeout=5.0)
if response:
print(f"收到响应: {response.hex(' ').upper()}")
# 检查功能码
if len(response) >= 4:
func_code = response[3]
print(f"功能码: 0x{func_code:02X}")
if func_code == 0x06:
print("→ 这是应答帧(0x06),继续等待结果帧...")
continue
elif func_code == 0x15:
print("→ 这是结果帧(0x15),开始解析...")
result = wrench.parse_result(response)
break
elif func_code == 0x12:
print("→ 这是实时数据帧(0x12),继续等待结果帧...")
continue
else:
print(f"→ 未知功能码,继续等待...")
continue
else:
print("未收到响应,继续等待...")
if result:
wrench.print_result(result)
# 详细打印结果
if result:
print("\n" + "="*60)
print("📊 详细结果分析")
print("="*60)
if "error" in result:
print(f"❌ 错误: {result['error']}")
else:
# 打印所有字段
print(f"✅ 成功标志: {result.get('success')}")
print(f"📝 状态描述: {result.get('status')}")
print(f"🔢 状态码: {result.get('status_code')}")
print(f"👤 员工号: {result.get('employee_id')}")
print(f"🔩 螺栓号: {result.get('bolt_no')}")
print(f"⏰ 时间戳: {result.get('timestamp')}")
print(f"⚙️ 模式: {result.get('mode')}")
print(f"🎯 目标扭矩: {result.get('target_torque')} Nm")
print(f"📈 实际扭矩: {result.get('actual_torque')} Nm")
print(f"📐 目标角度: {result.get('target_angle')}°")
print(f"📐 实际角度: {result.get('actual_angle')}°")
print(f"📊 扭矩范围: {result.get('torque_range')}")
print(f"📊 角度范围: {result.get('angle_range')}")
print("\n" + "="*60)
if result.get('success'):
print("✅✅✅ 最终判断: 成功 ✅✅✅")
else:
print("❌❌❌ 最终判断: 失败 ❌❌❌")
print("="*60)
else:
print("❌ 未收到结果")
except KeyboardInterrupt:
print("\n\n⚠️ 用户中断")
except Exception as e:
print(f"\n❌ 发生错误: {e}")
import traceback
traceback.print_exc()
finally:
# 停止远程控制
print("\n5. 停止远程控制...")
wrench.enable_remote_control(False)
# 断开连接
wrench.disconnect()
print("\n测试完成")
if __name__ == "__main__":
main()