2.15.23
This commit is contained in:
+37
-1
@@ -69,7 +69,7 @@ class ATClient:
|
||||
# 同上:避免在 _reader_loop 持锁期间二次 acquire
|
||||
self._http_events.append(ev)
|
||||
|
||||
def send(self, cmd: str, expect: str = "OK", timeout_ms: int = 2000):
|
||||
def send(self, cmd: str, expect: str = "OK", timeout_ms: int = 2000, abort_event=None):
|
||||
"""
|
||||
发送 AT 命令并等待 expect(子串匹配)。
|
||||
注意:expect=">" 用于等待 prompt。
|
||||
@@ -90,6 +90,9 @@ class ATClient:
|
||||
|
||||
t0 = time.ticks_ms()
|
||||
while abs(time.ticks_diff(time.ticks_ms(), t0)) < timeout_ms:
|
||||
if abort_event is not None and abort_event.is_set():
|
||||
self._waiting = False
|
||||
break
|
||||
if (not self._waiting) or (self._expect in self._resp):
|
||||
self._waiting = False
|
||||
break
|
||||
@@ -102,6 +105,39 @@ class ATClient:
|
||||
except:
|
||||
return str(self._resp)
|
||||
|
||||
def send_raw_and_wait(self, data: bytes, expect: str = "OK", timeout_ms: int = 1000,
|
||||
suffix: bytes = b""):
|
||||
"""Register the response waiter before writing raw UART data."""
|
||||
expect_b = expect.encode() if isinstance(expect, str) else expect
|
||||
with self._cmd_lock:
|
||||
with self._q_lock:
|
||||
self._waiting = True
|
||||
self._expect = expect_b
|
||||
self._resp = b""
|
||||
|
||||
total = 0
|
||||
while total < len(data):
|
||||
n = self.uart.write(data[total:])
|
||||
if not n or n < 0:
|
||||
time.sleep_ms(1)
|
||||
continue
|
||||
total += n
|
||||
if suffix:
|
||||
self.uart.write(suffix)
|
||||
|
||||
t0 = time.ticks_ms()
|
||||
while abs(time.ticks_diff(time.ticks_ms(), t0)) < timeout_ms:
|
||||
if (not self._waiting) or (self._expect in self._resp):
|
||||
self._waiting = False
|
||||
break
|
||||
time.sleep_ms(5)
|
||||
|
||||
self._waiting = False
|
||||
try:
|
||||
return self._resp.decode(errors="ignore")
|
||||
except:
|
||||
return str(self._resp)
|
||||
|
||||
def _find_urc_tag(self, tag: bytes):
|
||||
"""
|
||||
只在"真正的 URC 边界"查找 tag,避免误命中 HTTP payload 内容。
|
||||
|
||||
Reference in New Issue
Block a user