sdev 0.3.2__tar.gz → 0.3.4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {sdev-0.3.2/sdev.egg-info → sdev-0.3.4}/PKG-INFO +1 -1
- {sdev-0.3.2 → sdev-0.3.4}/pyproject.toml +1 -1
- {sdev-0.3.2 → sdev-0.3.4}/sdev/__init__.py +1 -1
- {sdev-0.3.2 → sdev-0.3.4}/sdev/core.py +41 -22
- {sdev-0.3.2 → sdev-0.3.4/sdev.egg-info}/PKG-INFO +1 -1
- {sdev-0.3.2 → sdev-0.3.4}/setup.py +1 -1
- {sdev-0.3.2 → sdev-0.3.4}/LICENSE +0 -0
- {sdev-0.3.2 → sdev-0.3.4}/MANIFEST.in +0 -0
- {sdev-0.3.2 → sdev-0.3.4}/README.md +0 -0
- {sdev-0.3.2 → sdev-0.3.4}/sdev/cli_wrapper.py +0 -0
- {sdev-0.3.2 → sdev-0.3.4}/sdev.egg-info/SOURCES.txt +0 -0
- {sdev-0.3.2 → sdev-0.3.4}/sdev.egg-info/dependency_links.txt +0 -0
- {sdev-0.3.2 → sdev-0.3.4}/sdev.egg-info/entry_points.txt +0 -0
- {sdev-0.3.2 → sdev-0.3.4}/sdev.egg-info/requires.txt +0 -0
- {sdev-0.3.2 → sdev-0.3.4}/sdev.egg-info/top_level.txt +0 -0
- {sdev-0.3.2 → sdev-0.3.4}/setup.cfg +0 -0
|
@@ -49,8 +49,8 @@ class SerialDevice:
|
|
|
49
49
|
self._display = True
|
|
50
50
|
self._check_alive = check_alive
|
|
51
51
|
|
|
52
|
-
self._reboot_timeout =
|
|
53
|
-
self._check_alive_timeout =
|
|
52
|
+
self._reboot_timeout = 60 # check_alive 时 wait_for_flag 等稳定提示符的最大秒数
|
|
53
|
+
self._check_alive_timeout = 1
|
|
54
54
|
self._scan_timeout = 0.1
|
|
55
55
|
|
|
56
56
|
def _serial_reader(self) -> None:
|
|
@@ -131,12 +131,20 @@ class SerialDevice:
|
|
|
131
131
|
def _echo_flag(self, line: str) -> None:
|
|
132
132
|
print(f"{_GREEN}{line}{_RESET}", end="", flush=True)
|
|
133
133
|
|
|
134
|
-
def scan(self, end_flag: str, timeout: Optional[float] = None, replace_with_acc=False):
|
|
134
|
+
def scan(self, end_flag: Optional[str], timeout: Optional[float] = None, replace_with_acc=False):
|
|
135
135
|
"""
|
|
136
136
|
从 _buffer 逐行 yield,直到出现 end_flag 或超时/None。
|
|
137
|
+
end_flag 为 None 时改为读到 timeout 就结束,此时 TimeoutError 视为正常结束(return)。
|
|
137
138
|
逻辑:用 acc_cache 保留最近几行,拼成 acc_line(无换行),避免设备折行把 flag 拆到两行导致 in 判不到;
|
|
138
139
|
命中时若 replace_with_acc 则 yield 整段 acc_line 加换行,否则逐行 yield cache 后 break。CTRL_C 按 _content_contains_ctrl_c 判。
|
|
139
140
|
"""
|
|
141
|
+
if end_flag is None:
|
|
142
|
+
try:
|
|
143
|
+
for line in self._scan(timeout):
|
|
144
|
+
yield line
|
|
145
|
+
except TimeoutError:
|
|
146
|
+
pass
|
|
147
|
+
return
|
|
140
148
|
acc_cache = []
|
|
141
149
|
for line in self._scan(timeout):
|
|
142
150
|
acc_cache.append(line)
|
|
@@ -152,21 +160,26 @@ class SerialDevice:
|
|
|
152
160
|
yield x
|
|
153
161
|
break
|
|
154
162
|
|
|
155
|
-
def scan_and_display(self, flag: str, flag_type: Literal["end_flag", "prompt"], timeout: Optional[float] = None, replace_with_acc=False):
|
|
156
|
-
"""对 scan(flag) 的每一行按 flag_type 回显(end_flag 绿、prompt 青、其余灰)并 yield
|
|
163
|
+
def scan_and_display(self, flag: Optional[str], flag_type: Optional[Literal["end_flag", "prompt"]] = None, timeout: Optional[float] = None, replace_with_acc=False):
|
|
164
|
+
"""对 scan(flag) 的每一行按 flag_type 回显(end_flag 绿、prompt 青、其余灰)并 yield 该行;flag/flag_type 为 None 时只灰显,flag 为 None 时超时视为正常结束。"""
|
|
157
165
|
try:
|
|
158
166
|
for line in self.scan(flag, timeout=timeout, replace_with_acc=replace_with_acc):
|
|
159
167
|
if self._display:
|
|
160
|
-
|
|
161
|
-
if find_flag:
|
|
162
|
-
if flag_type == "end_flag":
|
|
163
|
-
self._echo_flag(line)
|
|
164
|
-
elif flag_type == "prompt":
|
|
165
|
-
self._echo_prompt(line)
|
|
166
|
-
else:
|
|
168
|
+
if flag is None or flag_type is None:
|
|
167
169
|
self._echo_normal(line)
|
|
170
|
+
else:
|
|
171
|
+
find_flag = _line_matches_flag(line, flag)
|
|
172
|
+
if find_flag:
|
|
173
|
+
if flag_type == "end_flag":
|
|
174
|
+
self._echo_flag(line)
|
|
175
|
+
elif flag_type == "prompt":
|
|
176
|
+
self._echo_prompt(line)
|
|
177
|
+
else:
|
|
178
|
+
self._echo_normal(line)
|
|
168
179
|
yield line
|
|
169
180
|
except TimeoutError:
|
|
181
|
+
if flag is None:
|
|
182
|
+
return
|
|
170
183
|
t = f"{timeout}s" if timeout is not None else "None"
|
|
171
184
|
raise TimeoutError(f"scan_and_display timeout: flag={flag!r}, flag_type={flag_type!r}, timeout={t}")
|
|
172
185
|
|
|
@@ -213,26 +226,32 @@ class SerialDevice:
|
|
|
213
226
|
gen = self.scan_and_display(flag, "end_flag", timeout=timeout)
|
|
214
227
|
_ = list(gen)
|
|
215
228
|
|
|
229
|
+
def is_breathing(self) -> bool:
|
|
230
|
+
"""检查板子是否呼吸。"""
|
|
231
|
+
time.sleep(self._check_alive_timeout)
|
|
232
|
+
return self._has_output_in_buffer()
|
|
233
|
+
|
|
216
234
|
def check_alive(self, stable_flag: str):
|
|
217
235
|
"""
|
|
218
236
|
确认板子存活:有输出则 wait_for_flag(stable_flag) 后 return;否则 send_interrupt,成功则 return。
|
|
219
237
|
若 send_interrupt 超时则打 warning 并轮询 _has_output_in_buffer(),有数据再 wait_for_flag 后 return;未接串口会一直循环。
|
|
220
238
|
"""
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
self.
|
|
224
|
-
|
|
239
|
+
print("check_alive")
|
|
240
|
+
while self.is_breathing():
|
|
241
|
+
gen = self.scan_and_display(None, timeout=self._check_alive_timeout)
|
|
242
|
+
_ = list(gen)
|
|
225
243
|
|
|
226
|
-
# 测试是否能够接收ctrlc
|
|
244
|
+
# 测试是否能够接收ctrlc信号,若无反应,等待转生
|
|
245
|
+
print("send_interrupt")
|
|
227
246
|
try:
|
|
228
247
|
self.send_interrupt(self._check_alive_timeout, stable_flag)
|
|
229
248
|
except TimeoutError:
|
|
230
249
|
logger.warning(f"请接入串口")
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
250
|
+
print("wait for breathing")
|
|
251
|
+
while not self.is_breathing():
|
|
252
|
+
pass
|
|
253
|
+
print("check_alive again")
|
|
254
|
+
self.check_alive(stable_flag)
|
|
236
255
|
|
|
237
256
|
def __enter__(self):
|
|
238
257
|
self.connect()
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|