From 2b3aaf887925c3d6b8cad31d9fba9bc2545caaef Mon Sep 17 00:00:00 2001 From: caoqianming Date: Mon, 18 May 2026 15:16:27 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=86=99=E6=8C=87=E4=BB=A4=E4=B9=9F?= =?UTF-8?q?=E7=9F=AD=E8=AF=BB0.3s=E6=8D=95=E8=8E=B7=E6=9C=BA=E5=99=A8?= =?UTF-8?q?=E7=9A=84ERR=E5=9B=9E=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 之前 expect_reply=False 直接关 socket, 机器若回 ERR 完全感知不到, 表现就是"接口200但实际没动作"。改为写完后短读最多一行, 拿到 ERR/'!' 开头直接报错; 超时则照旧视为成功放行。 Co-Authored-By: Claude Opus 4.7 (1M context) --- apps/cm/coder.py | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/apps/cm/coder.py b/apps/cm/coder.py index 095fa08f..360b63f7 100644 --- a/apps/cm/coder.py +++ b/apps/cm/coder.py @@ -27,15 +27,32 @@ class CoderClient: try: with socket.create_connection((self.ip, self.port), timeout=self.timeout) as s: s.sendall(frame) - if not expect_reply: - return b"" - buf = b"" - while CR not in buf: - chunk = s.recv(256) - if not chunk: - break - buf += chunk - return buf + if expect_reply: + buf = b"" + while CR not in buf: + chunk = s.recv(256) + if not chunk: + break + buf += chunk + return buf + # 写指令: 短暂等待, 捕获机器拒绝(ERR/!); 超时则视为成功 + s.settimeout(0.3) + try: + buf = b"" + while CR not in buf: + chunk = s.recv(256) + if not chunk: + break + buf += chunk + except socket.timeout: + buf = b"" + if buf: + text = buf.rstrip(b"\r\n").decode("latin-1", errors="replace") + if text.startswith("ERR") or text.startswith("!"): + raise CoderError(f"喷码机拒绝指令 {frame!r}: {text}") + return b"" + except CoderError: + raise except (socket.timeout, OSError) as e: raise CoderError(f"喷码通讯失败 {self.ip}:{self.port} - {e}")