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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sdev
3
- Version: 0.3.2
3
+ Version: 0.3.4
4
4
  Summary: 串口控制器工具包
5
5
  Home-page: https://github.com/klrc/sdev
6
6
  Author: klrc
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "sdev"
7
- version = "0.3.2"
7
+ version = "0.3.4"
8
8
  description = "串口控制器工具包"
9
9
  readme = "README.md"
10
10
  license = {text = "MIT"}
@@ -6,7 +6,7 @@ Demoboard:串口连接、后台异步读、发送、按行/按 flag 读、回
6
6
 
7
7
  from .core import SerialDevice
8
8
 
9
- __version__ = "0.3.2"
9
+ __version__ = "0.3.4"
10
10
  __author__ = "klrc"
11
11
  __email__ = "1440698245@qq.com"
12
12
 
@@ -49,8 +49,8 @@ class SerialDevice:
49
49
  self._display = True
50
50
  self._check_alive = check_alive
51
51
 
52
- self._reboot_timeout = 20 # check_alive 时 wait_for_flag 等稳定提示符的最大秒数
53
- self._check_alive_timeout = 0.5
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
- find_flag = _line_matches_flag(line, flag)
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
- time.sleep(self._check_alive_timeout)
222
- if self._has_output_in_buffer(): # 可能正在启动,等待稳定标志
223
- self.wait_for_flag(stable_flag, self._reboot_timeout)
224
- return
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信号,若无反应, wait for boot
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
- while True:
232
- time.sleep(self._check_alive_timeout)
233
- if self._has_output_in_buffer():
234
- self.wait_for_flag(stable_flag, self._reboot_timeout)
235
- return
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()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sdev
3
- Version: 0.3.2
3
+ Version: 0.3.4
4
4
  Summary: 串口控制器工具包
5
5
  Home-page: https://github.com/klrc/sdev
6
6
  Author: klrc
@@ -5,7 +5,7 @@ with open("README.md", "r", encoding="utf-8") as fh:
5
5
 
6
6
  setup(
7
7
  name="sdev",
8
- version="0.3.2",
8
+ version="0.3.4",
9
9
  author="klrc",
10
10
  author_email="144069824@qq.com",
11
11
  description="串口控制器工具包",
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes