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}")