diff --git a/custom_components/sigmesh_gateway/serial_reader.py b/custom_components/sigmesh_gateway/serial_reader.py index f67b86a..e9db749 100644 --- a/custom_components/sigmesh_gateway/serial_reader.py +++ b/custom_components/sigmesh_gateway/serial_reader.py @@ -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 命令."""