2026-01-24 02:54:01 +08:00
|
|
|
|
#!/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)
|
|
|
|
|
|
|
2026-02-04 11:35:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
2026-02-26 16:39:29 +08:00
|
|
|
|
|
|
|
|
|
|
|