From 93778ef8618c2ccc3d9306fa0fb9e08ca9548fda Mon Sep 17 00:00:00 2001 From: impressionyang Date: Thu, 16 Apr 2026 17:14:22 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=94=B9=E8=BF=9B=E4=B8=B2=E5=8F=A3?= =?UTF-8?q?=E5=86=99=E5=85=A5=E9=94=99=E8=AF=AF=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 移除 write_command 中的 try-catch,让错误向上抛出 2. 在 write 方法中添加更详细的错误日志 3. 直接调用 _serial.write() 而不是先检查 is_connected --- custom_components/sigmesh_gateway/serial_reader.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) 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 命令."""