TorqueWrench/test_device_connection.py

86 lines
2.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
测试设备连接脚本
"""
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from wrench_controller import WrenchController
def test_connection(ip, port=7888, address_code=1):
"""测试设备连接"""
print(f"测试连接: {ip}:{port}, 地址码: {address_code}")
print("-" * 60)
device_config = {
"ip_address": ip,
"port": port,
"address_code": address_code
}
try:
wrench = WrenchController(device_config=device_config)
print("正在连接...")
if wrench.connect():
print("[OK] 连接成功!")
# 尝试查询SN码
print("\n正在查询SN码...")
sn = wrench.query_device_sn()
if sn:
print(f"[OK] SN码: {sn}")
else:
print("[WARN] 未获取到SN码")
wrench.disconnect()
print("\n[OK] 设备在线")
return True
else:
print("[FAIL] 连接失败")
return False
except Exception as e:
print(f"[FAIL] 连接异常: {e}")
import traceback
traceback.print_exc()
return False
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="测试扳手设备连接")
parser.add_argument("--ip", default="127.0.0.1", help="设备IP地址")
parser.add_argument("--port", type=int, default=7888, help="设备端口")
parser.add_argument("--address", type=int, default=1, help="设备地址码")
args = parser.parse_args()
print("=" * 60)
print("扳手设备连接测试")
print("=" * 60)
print(f"\n提示: 如果IP是127.0.0.1请确保wrench_simulator.py正在运行")
print("=" * 60)
print()
result = test_connection(args.ip, args.port, args.address)
print("\n" + "=" * 60)
if result:
print("测试结果: 设备在线")
else:
print("测试结果: 设备离线或无法连接")
print("\n可能的原因:")
print("1. wrench_simulator.py 未运行")
print("2. IP地址或端口不正确")
print("3. 防火墙阻止连接")
print("4. 设备地址码不匹配")
print("=" * 60)