feat: 添加详细的串口调试日志
添加详细的日志输出用于调试: 1. 串口原始数据输入(字节数和 HEX) 2. HCI 命令发送详情(OPCODE、HEX、长度) 3. HCI 响应解析详情 4. 数据完整性检查日志 5. 超时时的详细提示信息 日志级别: - INFO: 关键操作和响应 - DEBUG: 详细数据流 - WARNING: 异常情况
This commit is contained in:
parent
a790d6247f
commit
c3e2785d39
@ -83,7 +83,10 @@ class HciGateway:
|
||||
响应数据或 None
|
||||
"""
|
||||
cmd = build_hci_command(opcode, payload)
|
||||
_LOGGER.debug("发送 HCI 命令:0x%02X, 数据:%s", opcode, cmd.hex().upper())
|
||||
_LOGGER.info("=== 发送 HCI 命令 ===")
|
||||
_LOGGER.info("OPCODE: 0x%02X", opcode)
|
||||
_LOGGER.info("CMD HEX: %s", cmd.hex().upper())
|
||||
_LOGGER.info("CMD LEN: %d 字节", len(cmd))
|
||||
|
||||
# 创建响应 future
|
||||
loop = asyncio.get_event_loop()
|
||||
@ -93,14 +96,19 @@ class HciGateway:
|
||||
try:
|
||||
# 发送命令
|
||||
await self.serial_reader.write(cmd)
|
||||
_LOGGER.info("命令已发送到串口")
|
||||
|
||||
# 等待响应
|
||||
response = await asyncio.wait_for(future, timeout)
|
||||
_LOGGER.debug("收到 HCI 响应:0x%02X, 数据:%s", opcode, response.hex().upper())
|
||||
_LOGGER.info("收到 HCI 响应:opcode=0x%02X, data=%s", opcode, response.hex().upper())
|
||||
return response
|
||||
|
||||
except asyncio.TimeoutError:
|
||||
_LOGGER.warning("HCI 命令超时:0x%02X", opcode)
|
||||
_LOGGER.warning("HCI 命令超时:0x%02X (超时 %d 秒)", opcode, timeout)
|
||||
_LOGGER.warning("未收到任何响应,请检查:")
|
||||
_LOGGER.warning("1. 网关是否已正确配置 (使用 danglo 工具)")
|
||||
_LOGGER.warning("2. 串口连接是否正常")
|
||||
_LOGGER.warning("3. 网关固件是否支持该命令")
|
||||
return None
|
||||
except Exception as e:
|
||||
_LOGGER.error("HCI 命令失败:0x%02X, 错误:%s", opcode, e)
|
||||
@ -115,7 +123,9 @@ class HciGateway:
|
||||
data: 原始串口数据
|
||||
"""
|
||||
self._buffer.extend(data)
|
||||
_LOGGER.debug("HCI 接收原始数据:%s", data.hex().upper())
|
||||
_LOGGER.debug("=== HCI 接收原始数据 (%d 字节) ===", len(data))
|
||||
_LOGGER.debug("HEX: %s", data.hex().upper())
|
||||
_LOGGER.debug("BUF: %s", bytes(self._buffer).hex().upper())
|
||||
|
||||
# 尝试解析数据包
|
||||
while len(self._buffer) >= 3:
|
||||
@ -129,11 +139,12 @@ class HciGateway:
|
||||
length = self._buffer[2]
|
||||
|
||||
if len(self._buffer) < 3 + length:
|
||||
_LOGGER.debug("数据不完整:需要 %d 字节,当前 %d 字节", 3 + length, len(self._buffer))
|
||||
break # 数据不完整,等待更多
|
||||
|
||||
payload = bytes(self._buffer[3 : 3 + length])
|
||||
|
||||
_LOGGER.debug("HCI 响应:opcode=0x%02X, payload=%s", opcode, payload.hex().upper())
|
||||
_LOGGER.info("HCI 响应:opcode=0x%02X, len=%d, payload=%s", opcode, length, payload.hex().upper())
|
||||
|
||||
# 唤醒等待的 future
|
||||
if opcode in self._response_futures:
|
||||
@ -151,18 +162,19 @@ class HciGateway:
|
||||
length = self._buffer[3]
|
||||
|
||||
if len(self._buffer) < 4 + length:
|
||||
_LOGGER.debug("事件数据不完整:需要 %d 字节,当前 %d 字节", 4 + length, len(self._buffer))
|
||||
break # 数据不完整
|
||||
|
||||
event_data = bytes(self._buffer[: 4 + length])
|
||||
self._buffer = self._buffer[4 + length :]
|
||||
|
||||
_LOGGER.debug("HCI 事件:opcode=0x%02X, data=%s", opcode, event_data.hex().upper())
|
||||
_LOGGER.info("HCI 事件:opcode=0x%02X, len=%d, data=%s", opcode, length, event_data.hex().upper())
|
||||
# 事件处理由上层负责
|
||||
self._handle_event(event_data)
|
||||
|
||||
else:
|
||||
# 未知数据,清空
|
||||
_LOGGER.warning("未知 HCI 数据头:%s", self._buffer[0:1].hex().upper())
|
||||
_LOGGER.warning("未知 HCI 数据头:0x%02X,清空缓冲区", self._buffer[0])
|
||||
self._buffer.clear()
|
||||
return
|
||||
|
||||
|
||||
@ -270,6 +270,8 @@ class SerialReader:
|
||||
try:
|
||||
if self._serial and self._serial.in_waiting:
|
||||
data = self._serial.read(self._serial.in_waiting)
|
||||
_LOGGER.debug("=== 串口原始输入 (%d 字节) ===", len(data))
|
||||
_LOGGER.debug("HEX: %s", data.hex().upper())
|
||||
|
||||
# 直接交给 HCI 网关处理
|
||||
if self.hci:
|
||||
|
||||
Loading…
Reference in New Issue
Block a user