fix: 改进串口写入错误日志

1. 移除 write_command 中的 try-catch,让错误向上抛出
2. 在 write 方法中添加更详细的错误日志
3. 直接调用 _serial.write() 而不是先检查 is_connected
This commit is contained in:
impressionyang 2026-04-16 17:14:22 +08:00
parent b4643fa408
commit 93778ef861

View File

@ -292,10 +292,18 @@ class SerialReader:
async def write(self, data: bytes) -> int:
"""写入数据到串口."""
if not self.is_connected:
_LOGGER.debug("write 方法is_connected=%s, _serial=%s", self.is_connected, self._serial)
if self._serial is None:
_LOGGER.error("串口对象未初始化")
raise RuntimeError("串口未连接")
return self._serial.write(data) # type: ignore[union-attr]
try:
result = self._serial.write(data)
_LOGGER.debug("串口写入成功:%d 字节", result)
return result
except Exception as e:
_LOGGER.error("串口写入失败:%s (is_open=%s)", e, self._serial.is_open)
raise
async def write_command(self, command: str) -> int:
"""写入 AT 命令."""