From 721483dfce6800545b8668f53efac6fd14207f22 Mon Sep 17 00:00:00 2001 From: impressionyang Date: Fri, 17 Apr 2026 14:06:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=B8=B2=E5=8F=A3=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E8=84=9A=E6=9C=AC=20test=5Fserial.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test_serial.py | 141 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 test_serial.py diff --git a/test_serial.py b/test_serial.py new file mode 100644 index 0000000..d297a74 --- /dev/null +++ b/test_serial.py @@ -0,0 +1,141 @@ +#!/usr/bin/env python3 +"""串口测试脚本 - 用于诊断 E104-BT12USP 网关通信问题.""" + +import serial +import serial.tools.list_ports +import time +import sys + + +def list_ports(): + """列出可用串口.""" + print("=== 可用串口列表 ===") + ports = serial.tools.list_ports.comports() + for p in ports: + print(f" {p.device}: {p.description} ({p.hwid})") + return ports + + +def test_hci_mode(device, baudrate=115200): + """测试 HCI 模式.""" + print(f"\n=== 测试 HCI 模式 ({device}, {baudrate}) ===") + try: + ser = serial.Serial(device, baudrate, timeout=2) + print(f"串口已打开") + + # 发送 HCI 版本查询 (E9 FF 02 00) + cmd = bytes([0xE9, 0xFF, 0x02, 0x00]) + print(f"发送 HCI 版本查询:{cmd.hex().upper()}") + ser.write(cmd) + + time.sleep(1) + if ser.in_waiting: + data = ser.read(ser.in_waiting) + print(f"收到响应 ({len(data)} 字节): {data.hex().upper()}") + + # 尝试解析 91 响应 + if data[0] == 0x91: + print("✓ 收到 HCI 响应格式 (91 头)") + return True + else: + print("✗ 无响应") + + ser.close() + return False + + except serial.SerialException as e: + print(f"✗ 串口错误:{e}") + return False + except Exception as e: + print(f"✗ 错误:{e}") + return False + + +def test_at_mode(device, baudrate=115200): + """测试 AT 命令模式.""" + print(f"\n=== 测试 AT 命令模式 ({device}, {baudrate}) ===") + try: + ser = serial.Serial(device, baudrate, timeout=2) + print(f"串口已打开") + + # 发送 AT 命令 + cmd = b"AT\r\n" + print(f"发送 AT 命令:{cmd!r}") + ser.write(cmd) + + time.sleep(1) + if ser.in_waiting: + data = ser.read(ser.in_waiting) + print(f"收到响应:{data!r}") + + if b"OK" in data or b"AT+" in data: + print("✓ 收到 AT 响应") + return True + else: + print("✗ 无响应") + + ser.close() + return False + + except serial.SerialException as e: + print(f"✗ 串口错误:{e}") + return False + except Exception as e: + print(f"✗ 错误:{e}") + return False + + +def monitor_raw(device, baudrate=115200, duration=10): + """监听原始串口数据.""" + print(f"\n=== 监听原始数据 ({device}, 持续{duration}秒) ===") + print("请在其他程序中触发网关发送数据...") + + try: + ser = serial.Serial(device, baudrate, timeout=0.1) + start = time.time() + + while time.time() - start < duration: + if ser.in_waiting: + data = ser.read(ser.in_waiting) + timestamp = time.strftime("%H:%M:%S") + print(f"[{timestamp}] 收到 {len(data)} 字节:{data.hex().upper()}") + time.sleep(0.01) + + ser.close() + print("监听结束") + + except Exception as e: + print(f"✗ 错误:{e}") + + +if __name__ == "__main__": + if len(sys.argv) > 1: + device = sys.argv[1] + else: + ports = list_ports() + if not ports: + print("未找到串口设备") + sys.exit(1) + device = ports[0].device + + baudrate = int(sys.argv[2]) if len(sys.argv) > 2 else 115200 + + print(f"\n使用设备:{device}, 波特率:{baudrate}") + + # 测试 HCI 模式 + hci_result = test_hci_mode(device, baudrate) + + # 测试 AT 模式 + at_result = test_at_mode(device, baudrate) + + # 结果总结 + print("\n=== 测试结果 ===") + print(f"HCI 模式:{'✓ 成功' if hci_result else '✗ 失败'}") + print(f"AT 模式:{'✓ 成功' if at_result else '✗ 失败'}") + + if not hci_result and not at_result: + print("\n建议:") + print("1. 检查串口权限:ls -l /dev/ttyUSB0") + print("2. 确认网关已供电") + print("3. 尝试其他波特率 (9600, 38400, 57600)") + print("4. 检查 USB 线是否完好")