sdev 0.4.0__tar.gz → 0.4.2__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.4.2/PKG-INFO +117 -0
- sdev-0.4.2/README.md +83 -0
- {sdev-0.4.0 → sdev-0.4.2}/pyproject.toml +1 -1
- {sdev-0.4.0 → sdev-0.4.2}/sdev/__init__.py +1 -1
- {sdev-0.4.0 → sdev-0.4.2}/sdev/class_wrapper.py +8 -10
- {sdev-0.4.0 → sdev-0.4.2}/sdev/cli_wrapper.py +4 -55
- {sdev-0.4.0 → sdev-0.4.2}/sdev/demoboard/functional.py +33 -29
- sdev-0.4.2/sdev.egg-info/PKG-INFO +117 -0
- {sdev-0.4.0 → sdev-0.4.2}/setup.py +1 -1
- sdev-0.4.0/PKG-INFO +0 -119
- sdev-0.4.0/README.md +0 -85
- sdev-0.4.0/sdev.egg-info/PKG-INFO +0 -119
- {sdev-0.4.0 → sdev-0.4.2}/LICENSE +0 -0
- {sdev-0.4.0 → sdev-0.4.2}/MANIFEST.in +0 -0
- {sdev-0.4.0 → sdev-0.4.2}/sdev/core.py +0 -0
- {sdev-0.4.0 → sdev-0.4.2}/sdev/demoboard/__init__.py +0 -0
- {sdev-0.4.0 → sdev-0.4.2}/sdev/relay/__init__.py +0 -0
- {sdev-0.4.0 → sdev-0.4.2}/sdev/relay/functional.py +0 -0
- {sdev-0.4.0 → sdev-0.4.2}/sdev.egg-info/SOURCES.txt +0 -0
- {sdev-0.4.0 → sdev-0.4.2}/sdev.egg-info/dependency_links.txt +0 -0
- {sdev-0.4.0 → sdev-0.4.2}/sdev.egg-info/entry_points.txt +0 -0
- {sdev-0.4.0 → sdev-0.4.2}/sdev.egg-info/requires.txt +0 -0
- {sdev-0.4.0 → sdev-0.4.2}/sdev.egg-info/top_level.txt +0 -0
- {sdev-0.4.0 → sdev-0.4.2}/setup.cfg +0 -0
sdev-0.4.2/PKG-INFO
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: sdev
|
|
3
|
+
Version: 0.4.2
|
|
4
|
+
Summary: 串口控制器工具包
|
|
5
|
+
Home-page: https://github.com/klrc/sdev
|
|
6
|
+
Author: klrc
|
|
7
|
+
Author-email: klrc <144069824@qq.com>
|
|
8
|
+
License: MIT
|
|
9
|
+
Project-URL: Homepage, https://github.com/klrc/sdev
|
|
10
|
+
Project-URL: Repository, https://github.com/klrc/sdev
|
|
11
|
+
Project-URL: Bug Tracker, https://github.com/klrc/sdev/issues
|
|
12
|
+
Keywords: serial,controller,hardware,embedded
|
|
13
|
+
Classifier: Development Status :: 3 - Alpha
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.7
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
23
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
24
|
+
Classifier: Topic :: System :: Hardware
|
|
25
|
+
Requires-Python: >=3.7
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
License-File: LICENSE
|
|
28
|
+
Requires-Dist: pyserial>=3.5
|
|
29
|
+
Requires-Dist: loguru>=0.6.0
|
|
30
|
+
Dynamic: author
|
|
31
|
+
Dynamic: home-page
|
|
32
|
+
Dynamic: license-file
|
|
33
|
+
Dynamic: requires-python
|
|
34
|
+
|
|
35
|
+
# SDEV
|
|
36
|
+
|
|
37
|
+
串口开发板控制器:后台按行读缓冲、按 flag 扫描与回显着色、执行命令、存活检测。
|
|
38
|
+
|
|
39
|
+
**注意**:同一串口同一时间只能被一个 sdev 进程使用。请勿并行执行多条 `sdev shell ...` 或快速连续执行,否则可能报「串口已被其他 sdev 进程占用」或挂死;需等前一条命令结束后再执行下一条。
|
|
40
|
+
|
|
41
|
+
## 安装
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
pip install sdev
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## 命令行
|
|
48
|
+
|
|
49
|
+
安装后可使用 `sdev` 命令,在默认串口上执行单条命令并回显:
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
sdev shell "lsmod | grep -E 'mmz|nnp|vdsp'"
|
|
53
|
+
sdev -p /dev/ttyUSB1 -b 115200 shell "ls"
|
|
54
|
+
sdev --no-check-alive shell "echo hello"
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
- `-p` / `--port`:串口路径(默认取环境变量 `SDEV_PORT` 或 `/dev/ttyUSB0`)
|
|
58
|
+
- `-b` / `--baudrate`:波特率(默认取 `SDEV_BAUDRATE` 或 `115200`)
|
|
59
|
+
- `--flag`:提示符结束标志(默认 `" #"`)
|
|
60
|
+
- `--timeout`:等待输出超时秒数
|
|
61
|
+
- `--no-check-alive`:连接后不执行存活检测
|
|
62
|
+
|
|
63
|
+
## 快速使用 (Python)
|
|
64
|
+
|
|
65
|
+
推荐使用 `sdev()` 工厂函数或 `Demoboard` 类,它们提供了更高层、更易用的接口:
|
|
66
|
+
|
|
67
|
+
```python
|
|
68
|
+
from sdev import sdev
|
|
69
|
+
|
|
70
|
+
# 使用 context manager 自动连接/断开
|
|
71
|
+
with sdev("/dev/ttyUSB0", 115200) as board:
|
|
72
|
+
# connect 时默认会执行 check_alive(),确保板子已就绪
|
|
73
|
+
out = board.shell("ls") # 执行命令并返回输出行列表
|
|
74
|
+
print(out)
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
手动控制连接与存活检测:
|
|
78
|
+
|
|
79
|
+
```python
|
|
80
|
+
from sdev import Demoboard
|
|
81
|
+
|
|
82
|
+
board = Demoboard("/dev/ttyUSB0", 115200)
|
|
83
|
+
board.connect()
|
|
84
|
+
board.check_alive() # 检查并确保进入 shell 提示符
|
|
85
|
+
board.shell("echo hello")
|
|
86
|
+
board.disconnect()
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## 主要接口
|
|
90
|
+
|
|
91
|
+
### Demoboard (推荐)
|
|
92
|
+
|
|
93
|
+
继承自 `SerialDevice`,提供针对开发板的高层封装。
|
|
94
|
+
|
|
95
|
+
| 方法 | 说明 |
|
|
96
|
+
|------|------|
|
|
97
|
+
| `shell(cmd, prompt_flag=" #", timeout=None, stream=False)` | 清缓冲 -> 发送命令 -> 等待命令回显 -> 等待提示符。返回输出列表或生成器。 |
|
|
98
|
+
| `check_alive(uboot_flag="uboot#", prompt_flag=" #", response_timeout=3)` | 存活检测:处理 U-Boot 挂起、发送 Ctrl-C 唤醒并等待提示符。 |
|
|
99
|
+
|
|
100
|
+
### SerialDevice (底层)
|
|
101
|
+
|
|
102
|
+
| 方法 / 属性 | 说明 |
|
|
103
|
+
|-------------|------|
|
|
104
|
+
| `STABLE_FLAG` | 类常量 `" #"`,常用作默认提示符。 |
|
|
105
|
+
| `connect()` / `disconnect()` | 打开/关闭串口并启停后台读线程。 |
|
|
106
|
+
| `send(text)` | 发送一行(自动加换行)。 |
|
|
107
|
+
| `scan(end_flag, timeout=None)` | 从缓存中持续读取直到目标 flag,或读到 timeout 结束。 |
|
|
108
|
+
| `display(lines, flag=None, flag_type=None)` | 对传入的行按 flag 类型回显(着色)并 yield 原始行。 |
|
|
109
|
+
| `clear()` | 清空读缓冲。 |
|
|
110
|
+
|
|
111
|
+
- **回显着色**:匹配到 `flag` 的行为绿色,命令回显行为青色,其余灰色;支持设备折行后的跨行匹配。
|
|
112
|
+
|
|
113
|
+
## 依赖
|
|
114
|
+
|
|
115
|
+
- Python >= 3.7
|
|
116
|
+
- pyserial >= 3.5
|
|
117
|
+
- loguru >= 0.6.0
|
sdev-0.4.2/README.md
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# SDEV
|
|
2
|
+
|
|
3
|
+
串口开发板控制器:后台按行读缓冲、按 flag 扫描与回显着色、执行命令、存活检测。
|
|
4
|
+
|
|
5
|
+
**注意**:同一串口同一时间只能被一个 sdev 进程使用。请勿并行执行多条 `sdev shell ...` 或快速连续执行,否则可能报「串口已被其他 sdev 进程占用」或挂死;需等前一条命令结束后再执行下一条。
|
|
6
|
+
|
|
7
|
+
## 安装
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install sdev
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## 命令行
|
|
14
|
+
|
|
15
|
+
安装后可使用 `sdev` 命令,在默认串口上执行单条命令并回显:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
sdev shell "lsmod | grep -E 'mmz|nnp|vdsp'"
|
|
19
|
+
sdev -p /dev/ttyUSB1 -b 115200 shell "ls"
|
|
20
|
+
sdev --no-check-alive shell "echo hello"
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
- `-p` / `--port`:串口路径(默认取环境变量 `SDEV_PORT` 或 `/dev/ttyUSB0`)
|
|
24
|
+
- `-b` / `--baudrate`:波特率(默认取 `SDEV_BAUDRATE` 或 `115200`)
|
|
25
|
+
- `--flag`:提示符结束标志(默认 `" #"`)
|
|
26
|
+
- `--timeout`:等待输出超时秒数
|
|
27
|
+
- `--no-check-alive`:连接后不执行存活检测
|
|
28
|
+
|
|
29
|
+
## 快速使用 (Python)
|
|
30
|
+
|
|
31
|
+
推荐使用 `sdev()` 工厂函数或 `Demoboard` 类,它们提供了更高层、更易用的接口:
|
|
32
|
+
|
|
33
|
+
```python
|
|
34
|
+
from sdev import sdev
|
|
35
|
+
|
|
36
|
+
# 使用 context manager 自动连接/断开
|
|
37
|
+
with sdev("/dev/ttyUSB0", 115200) as board:
|
|
38
|
+
# connect 时默认会执行 check_alive(),确保板子已就绪
|
|
39
|
+
out = board.shell("ls") # 执行命令并返回输出行列表
|
|
40
|
+
print(out)
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
手动控制连接与存活检测:
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
from sdev import Demoboard
|
|
47
|
+
|
|
48
|
+
board = Demoboard("/dev/ttyUSB0", 115200)
|
|
49
|
+
board.connect()
|
|
50
|
+
board.check_alive() # 检查并确保进入 shell 提示符
|
|
51
|
+
board.shell("echo hello")
|
|
52
|
+
board.disconnect()
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## 主要接口
|
|
56
|
+
|
|
57
|
+
### Demoboard (推荐)
|
|
58
|
+
|
|
59
|
+
继承自 `SerialDevice`,提供针对开发板的高层封装。
|
|
60
|
+
|
|
61
|
+
| 方法 | 说明 |
|
|
62
|
+
|------|------|
|
|
63
|
+
| `shell(cmd, prompt_flag=" #", timeout=None, stream=False)` | 清缓冲 -> 发送命令 -> 等待命令回显 -> 等待提示符。返回输出列表或生成器。 |
|
|
64
|
+
| `check_alive(uboot_flag="uboot#", prompt_flag=" #", response_timeout=3)` | 存活检测:处理 U-Boot 挂起、发送 Ctrl-C 唤醒并等待提示符。 |
|
|
65
|
+
|
|
66
|
+
### SerialDevice (底层)
|
|
67
|
+
|
|
68
|
+
| 方法 / 属性 | 说明 |
|
|
69
|
+
|-------------|------|
|
|
70
|
+
| `STABLE_FLAG` | 类常量 `" #"`,常用作默认提示符。 |
|
|
71
|
+
| `connect()` / `disconnect()` | 打开/关闭串口并启停后台读线程。 |
|
|
72
|
+
| `send(text)` | 发送一行(自动加换行)。 |
|
|
73
|
+
| `scan(end_flag, timeout=None)` | 从缓存中持续读取直到目标 flag,或读到 timeout 结束。 |
|
|
74
|
+
| `display(lines, flag=None, flag_type=None)` | 对传入的行按 flag 类型回显(着色)并 yield 原始行。 |
|
|
75
|
+
| `clear()` | 清空读缓冲。 |
|
|
76
|
+
|
|
77
|
+
- **回显着色**:匹配到 `flag` 的行为绿色,命令回显行为青色,其余灰色;支持设备折行后的跨行匹配。
|
|
78
|
+
|
|
79
|
+
## 依赖
|
|
80
|
+
|
|
81
|
+
- Python >= 3.7
|
|
82
|
+
- pyserial >= 3.5
|
|
83
|
+
- loguru >= 0.6.0
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
|
|
8
8
|
from __future__ import annotations
|
|
9
9
|
|
|
10
|
-
from typing import Optional
|
|
10
|
+
from typing import Generator, List, Optional, Union
|
|
11
11
|
|
|
12
12
|
from .core import SerialDevice
|
|
13
13
|
from .demoboard import check_alive as _check_alive_func
|
|
@@ -21,6 +21,10 @@ class Demoboard(SerialDevice):
|
|
|
21
21
|
- 提供 shell() / check_alive() 等高层方法。
|
|
22
22
|
"""
|
|
23
23
|
|
|
24
|
+
def __init__(self, *args, **kwargs):
|
|
25
|
+
super().__init__(*args, **kwargs)
|
|
26
|
+
self.check_alive()
|
|
27
|
+
|
|
24
28
|
def shell(
|
|
25
29
|
self,
|
|
26
30
|
cmd: str,
|
|
@@ -28,27 +32,21 @@ class Demoboard(SerialDevice):
|
|
|
28
32
|
prompt_flag: str = " #",
|
|
29
33
|
timeout: Optional[float] = None,
|
|
30
34
|
stream: bool = False,
|
|
31
|
-
):
|
|
35
|
+
) -> Union[List[str], Generator[str, None, None]]:
|
|
32
36
|
return _shell_func(self, cmd, prompt_flag=prompt_flag, timeout=timeout, stream=stream)
|
|
33
37
|
|
|
34
38
|
def check_alive(
|
|
35
39
|
self,
|
|
36
40
|
*,
|
|
37
41
|
uboot_flag: str = "uboot#",
|
|
38
|
-
awaken_flag: str = "Process",
|
|
39
42
|
prompt_flag: str = " #",
|
|
40
|
-
|
|
41
|
-
reboot_timeout: float = 120.0,
|
|
42
|
-
prompt_timeout: float = 60.0,
|
|
43
|
+
response_timeout: float = 3,
|
|
43
44
|
) -> None:
|
|
44
45
|
_check_alive_func(
|
|
45
46
|
self,
|
|
46
47
|
uboot_flag=uboot_flag,
|
|
47
|
-
awaken_flag=awaken_flag,
|
|
48
48
|
prompt_flag=prompt_flag,
|
|
49
|
-
|
|
50
|
-
reboot_timeout=reboot_timeout,
|
|
51
|
-
prompt_timeout=prompt_timeout,
|
|
49
|
+
response_timeout=response_timeout,
|
|
52
50
|
)
|
|
53
51
|
|
|
54
52
|
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
"""
|
|
2
2
|
命令行入口:
|
|
3
3
|
|
|
4
|
-
- sdev shell "cmd"
|
|
5
|
-
- sdev demoboard status:枚举本机串口并通过 check_alive 检查是否存活。
|
|
4
|
+
- sdev shell "cmd":在串口设备上执行单条命令并回显输出。
|
|
6
5
|
"""
|
|
7
6
|
|
|
8
7
|
from __future__ import annotations
|
|
@@ -10,13 +9,8 @@ from __future__ import annotations
|
|
|
10
9
|
import argparse
|
|
11
10
|
import os
|
|
12
11
|
import sys
|
|
13
|
-
from typing import List
|
|
14
|
-
|
|
15
|
-
from serial.tools import list_ports
|
|
16
12
|
|
|
17
13
|
from .class_wrapper import Demoboard
|
|
18
|
-
from .demoboard.functional import check_alive as demoboard_check_alive
|
|
19
|
-
from .demoboard.functional import shell as demoboard_shell
|
|
20
14
|
|
|
21
15
|
|
|
22
16
|
def _get_default_port() -> str:
|
|
@@ -30,62 +24,24 @@ def _get_default_baudrate() -> int:
|
|
|
30
24
|
return 115200
|
|
31
25
|
|
|
32
26
|
|
|
33
|
-
def _list_serial_ports() -> List[str]:
|
|
34
|
-
"""跨平台列出本机可用串口设备路径。"""
|
|
35
|
-
ports = [info.device for info in list_ports.comports()]
|
|
36
|
-
return sorted(ports)
|
|
37
|
-
|
|
38
|
-
|
|
39
27
|
def cmd_shell(args: argparse.Namespace) -> int:
|
|
40
28
|
port = args.port or _get_default_port()
|
|
41
29
|
baudrate = args.baudrate or _get_default_baudrate()
|
|
42
30
|
with Demoboard(port, baudrate) as board:
|
|
43
31
|
try:
|
|
44
32
|
if not args.no_check_alive:
|
|
45
|
-
|
|
46
|
-
|
|
33
|
+
board.check_alive()
|
|
34
|
+
board.shell(args.command, prompt_flag=args.flag, timeout=args.timeout)
|
|
47
35
|
except TimeoutError as e:
|
|
48
36
|
print(e, file=sys.stderr)
|
|
49
37
|
return 1
|
|
50
38
|
return 0
|
|
51
39
|
|
|
52
40
|
|
|
53
|
-
def cmd_demoboard_status(args: argparse.Namespace) -> int: # noqa: ARG001
|
|
54
|
-
"""
|
|
55
|
-
sdev demoboard status
|
|
56
|
-
|
|
57
|
-
通过 check_alive 检查本机所有串口是否「alive」。
|
|
58
|
-
"""
|
|
59
|
-
GREEN = "\033[32m"
|
|
60
|
-
RED = "\033[31m"
|
|
61
|
-
RESET = "\033[0m"
|
|
62
|
-
|
|
63
|
-
baudrate = _get_default_baudrate()
|
|
64
|
-
ports = _list_serial_ports()
|
|
65
|
-
if not ports:
|
|
66
|
-
print("no serial ports found")
|
|
67
|
-
return 0
|
|
68
|
-
|
|
69
|
-
for port in ports:
|
|
70
|
-
try:
|
|
71
|
-
with Demoboard(port, baudrate) as board:
|
|
72
|
-
# 使用较短的超时时间,避免 status 长时间卡死
|
|
73
|
-
demoboard_check_alive(
|
|
74
|
-
board,
|
|
75
|
-
ctrl_c_timeout=2.0,
|
|
76
|
-
reboot_timeout=30.0,
|
|
77
|
-
prompt_timeout=10.0,
|
|
78
|
-
)
|
|
79
|
-
print(f"{GREEN}{port} alive{RESET}")
|
|
80
|
-
except Exception: # noqa: BLE001
|
|
81
|
-
print(f"{RED}{port} not connected{RESET}")
|
|
82
|
-
return 0
|
|
83
|
-
|
|
84
|
-
|
|
85
41
|
def main() -> int:
|
|
86
42
|
parser = argparse.ArgumentParser(
|
|
87
43
|
prog="sdev",
|
|
88
|
-
description="
|
|
44
|
+
description="串口开发板控制器:在串口设备上执行命令。",
|
|
89
45
|
)
|
|
90
46
|
parser.add_argument(
|
|
91
47
|
"-p",
|
|
@@ -126,13 +82,6 @@ def main() -> int:
|
|
|
126
82
|
)
|
|
127
83
|
shell_parser.set_defaults(func=cmd_shell)
|
|
128
84
|
|
|
129
|
-
# sdev demoboard status
|
|
130
|
-
demoboard_parser = subparsers.add_parser("demoboard", help="Demoboard 相关操作")
|
|
131
|
-
demoboard_subparsers = demoboard_parser.add_subparsers(dest="demoboard_subcommand", required=True)
|
|
132
|
-
|
|
133
|
-
status_parser = demoboard_subparsers.add_parser("status", help="检查本机关联的 Demoboard 串口状态")
|
|
134
|
-
status_parser.set_defaults(func=cmd_demoboard_status)
|
|
135
|
-
|
|
136
85
|
parsed = parser.parse_args()
|
|
137
86
|
return parsed.func(parsed)
|
|
138
87
|
|
|
@@ -85,13 +85,9 @@ def shell(
|
|
|
85
85
|
|
|
86
86
|
def check_alive(
|
|
87
87
|
device: SerialDevice,
|
|
88
|
-
*,
|
|
89
88
|
uboot_flag: str = "uboot#",
|
|
90
|
-
awaken_flag: str = "Process",
|
|
91
89
|
prompt_flag: str = " #",
|
|
92
|
-
|
|
93
|
-
reboot_timeout: float = 120.0,
|
|
94
|
-
prompt_timeout: float = 60.0,
|
|
90
|
+
response_timeout: float = 3,
|
|
95
91
|
) -> None:
|
|
96
92
|
"""
|
|
97
93
|
检查 demoboard 是否「醒着」且有 shell 提示符。
|
|
@@ -102,33 +98,41 @@ def check_alive(
|
|
|
102
98
|
2. 若在 U-Boot:
|
|
103
99
|
- 发送 "reboot";
|
|
104
100
|
- 等待 awaken_flag(如 "Process ... done" 中的 "Process")出现,超时则抛 TimeoutError;
|
|
105
|
-
3. 最后,无论是否经过 reboot,统一等待 shell 提示符 prompt_flag(如 "
|
|
101
|
+
3. 最后,无论是否经过 reboot,统一等待 shell 提示符 prompt_flag(如 "#"),超时抛 TimeoutError。
|
|
106
102
|
"""
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
lines = shell(device, CTRL_C, prompt_flag=prompt_flag, timeout=ctrl_c_timeout)
|
|
111
|
-
except TimeoutError:
|
|
112
|
-
# 如果超时,说明可能正在启动或者完全没响应,尝试盲发 Ctrl-C 并等待
|
|
113
|
-
device.send(CTRL_C)
|
|
114
|
-
lines = list(device.scan(end_flag=None, timeout=ctrl_c_timeout))
|
|
115
|
-
|
|
116
|
-
# 2. 检查是否在 U-Boot
|
|
117
|
-
is_uboot = any(uboot_flag in line for line in lines)
|
|
118
|
-
if is_uboot:
|
|
119
|
-
logger.warning(f"Device is in U-Boot (matched {uboot_flag!r}), rebooting...")
|
|
120
|
-
device.send("reboot")
|
|
121
|
-
# 等待系统开始启动(出现 awaken_flag)
|
|
122
|
-
list(device.scan(end_flag=awaken_flag, timeout=reboot_timeout))
|
|
123
|
-
logger.info("Reboot signal detected, waiting for system to boot...")
|
|
124
|
-
|
|
125
|
-
# 3. 统一等待最终提示符
|
|
103
|
+
|
|
104
|
+
# 1. Ctrl-C,判定控制台是否响应
|
|
105
|
+
under_reboot = False
|
|
126
106
|
try:
|
|
127
|
-
|
|
128
|
-
logger.info(f"Device is alive (matched prompt {prompt_flag!r})")
|
|
107
|
+
lines = shell(device, CTRL_C, prompt_flag, timeout=response_timeout)
|
|
129
108
|
except TimeoutError:
|
|
130
|
-
|
|
131
|
-
|
|
109
|
+
wait_iter = 0
|
|
110
|
+
while True:
|
|
111
|
+
device.send(CTRL_C)
|
|
112
|
+
lines = shell(device, None, None, timeout=response_timeout, clear=False)
|
|
113
|
+
if len(lines) > 0:
|
|
114
|
+
under_reboot = True
|
|
115
|
+
break
|
|
116
|
+
wait_iter += 1
|
|
117
|
+
if wait_iter > 5:
|
|
118
|
+
logger.warning("CTRL-C test failed, please check the connection")
|
|
119
|
+
break
|
|
120
|
+
|
|
121
|
+
# 2. 检查是否被中止进入uboot
|
|
122
|
+
for line in lines:
|
|
123
|
+
if uboot_flag in line:
|
|
124
|
+
logger.warning("uboot flag found, rebooting")
|
|
125
|
+
device.send("reboot")
|
|
126
|
+
under_reboot = True
|
|
127
|
+
break
|
|
128
|
+
|
|
129
|
+
# 3. 进入启动流程
|
|
130
|
+
if under_reboot:
|
|
131
|
+
try:
|
|
132
|
+
lines = shell(device, None, None, timeout=response_timeout)
|
|
133
|
+
except TimeoutError:
|
|
134
|
+
pass # 等待启动彻底完成
|
|
135
|
+
return check_alive(device, uboot_flag, prompt_flag, response_timeout)
|
|
132
136
|
|
|
133
137
|
|
|
134
138
|
if __name__ == "__main__":
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: sdev
|
|
3
|
+
Version: 0.4.2
|
|
4
|
+
Summary: 串口控制器工具包
|
|
5
|
+
Home-page: https://github.com/klrc/sdev
|
|
6
|
+
Author: klrc
|
|
7
|
+
Author-email: klrc <144069824@qq.com>
|
|
8
|
+
License: MIT
|
|
9
|
+
Project-URL: Homepage, https://github.com/klrc/sdev
|
|
10
|
+
Project-URL: Repository, https://github.com/klrc/sdev
|
|
11
|
+
Project-URL: Bug Tracker, https://github.com/klrc/sdev/issues
|
|
12
|
+
Keywords: serial,controller,hardware,embedded
|
|
13
|
+
Classifier: Development Status :: 3 - Alpha
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.7
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
23
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
24
|
+
Classifier: Topic :: System :: Hardware
|
|
25
|
+
Requires-Python: >=3.7
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
License-File: LICENSE
|
|
28
|
+
Requires-Dist: pyserial>=3.5
|
|
29
|
+
Requires-Dist: loguru>=0.6.0
|
|
30
|
+
Dynamic: author
|
|
31
|
+
Dynamic: home-page
|
|
32
|
+
Dynamic: license-file
|
|
33
|
+
Dynamic: requires-python
|
|
34
|
+
|
|
35
|
+
# SDEV
|
|
36
|
+
|
|
37
|
+
串口开发板控制器:后台按行读缓冲、按 flag 扫描与回显着色、执行命令、存活检测。
|
|
38
|
+
|
|
39
|
+
**注意**:同一串口同一时间只能被一个 sdev 进程使用。请勿并行执行多条 `sdev shell ...` 或快速连续执行,否则可能报「串口已被其他 sdev 进程占用」或挂死;需等前一条命令结束后再执行下一条。
|
|
40
|
+
|
|
41
|
+
## 安装
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
pip install sdev
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## 命令行
|
|
48
|
+
|
|
49
|
+
安装后可使用 `sdev` 命令,在默认串口上执行单条命令并回显:
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
sdev shell "lsmod | grep -E 'mmz|nnp|vdsp'"
|
|
53
|
+
sdev -p /dev/ttyUSB1 -b 115200 shell "ls"
|
|
54
|
+
sdev --no-check-alive shell "echo hello"
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
- `-p` / `--port`:串口路径(默认取环境变量 `SDEV_PORT` 或 `/dev/ttyUSB0`)
|
|
58
|
+
- `-b` / `--baudrate`:波特率(默认取 `SDEV_BAUDRATE` 或 `115200`)
|
|
59
|
+
- `--flag`:提示符结束标志(默认 `" #"`)
|
|
60
|
+
- `--timeout`:等待输出超时秒数
|
|
61
|
+
- `--no-check-alive`:连接后不执行存活检测
|
|
62
|
+
|
|
63
|
+
## 快速使用 (Python)
|
|
64
|
+
|
|
65
|
+
推荐使用 `sdev()` 工厂函数或 `Demoboard` 类,它们提供了更高层、更易用的接口:
|
|
66
|
+
|
|
67
|
+
```python
|
|
68
|
+
from sdev import sdev
|
|
69
|
+
|
|
70
|
+
# 使用 context manager 自动连接/断开
|
|
71
|
+
with sdev("/dev/ttyUSB0", 115200) as board:
|
|
72
|
+
# connect 时默认会执行 check_alive(),确保板子已就绪
|
|
73
|
+
out = board.shell("ls") # 执行命令并返回输出行列表
|
|
74
|
+
print(out)
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
手动控制连接与存活检测:
|
|
78
|
+
|
|
79
|
+
```python
|
|
80
|
+
from sdev import Demoboard
|
|
81
|
+
|
|
82
|
+
board = Demoboard("/dev/ttyUSB0", 115200)
|
|
83
|
+
board.connect()
|
|
84
|
+
board.check_alive() # 检查并确保进入 shell 提示符
|
|
85
|
+
board.shell("echo hello")
|
|
86
|
+
board.disconnect()
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## 主要接口
|
|
90
|
+
|
|
91
|
+
### Demoboard (推荐)
|
|
92
|
+
|
|
93
|
+
继承自 `SerialDevice`,提供针对开发板的高层封装。
|
|
94
|
+
|
|
95
|
+
| 方法 | 说明 |
|
|
96
|
+
|------|------|
|
|
97
|
+
| `shell(cmd, prompt_flag=" #", timeout=None, stream=False)` | 清缓冲 -> 发送命令 -> 等待命令回显 -> 等待提示符。返回输出列表或生成器。 |
|
|
98
|
+
| `check_alive(uboot_flag="uboot#", prompt_flag=" #", response_timeout=3)` | 存活检测:处理 U-Boot 挂起、发送 Ctrl-C 唤醒并等待提示符。 |
|
|
99
|
+
|
|
100
|
+
### SerialDevice (底层)
|
|
101
|
+
|
|
102
|
+
| 方法 / 属性 | 说明 |
|
|
103
|
+
|-------------|------|
|
|
104
|
+
| `STABLE_FLAG` | 类常量 `" #"`,常用作默认提示符。 |
|
|
105
|
+
| `connect()` / `disconnect()` | 打开/关闭串口并启停后台读线程。 |
|
|
106
|
+
| `send(text)` | 发送一行(自动加换行)。 |
|
|
107
|
+
| `scan(end_flag, timeout=None)` | 从缓存中持续读取直到目标 flag,或读到 timeout 结束。 |
|
|
108
|
+
| `display(lines, flag=None, flag_type=None)` | 对传入的行按 flag 类型回显(着色)并 yield 原始行。 |
|
|
109
|
+
| `clear()` | 清空读缓冲。 |
|
|
110
|
+
|
|
111
|
+
- **回显着色**:匹配到 `flag` 的行为绿色,命令回显行为青色,其余灰色;支持设备折行后的跨行匹配。
|
|
112
|
+
|
|
113
|
+
## 依赖
|
|
114
|
+
|
|
115
|
+
- Python >= 3.7
|
|
116
|
+
- pyserial >= 3.5
|
|
117
|
+
- loguru >= 0.6.0
|
sdev-0.4.0/PKG-INFO
DELETED
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: sdev
|
|
3
|
-
Version: 0.4.0
|
|
4
|
-
Summary: 串口控制器工具包
|
|
5
|
-
Home-page: https://github.com/klrc/sdev
|
|
6
|
-
Author: klrc
|
|
7
|
-
Author-email: klrc <144069824@qq.com>
|
|
8
|
-
License: MIT
|
|
9
|
-
Project-URL: Homepage, https://github.com/klrc/sdev
|
|
10
|
-
Project-URL: Repository, https://github.com/klrc/sdev
|
|
11
|
-
Project-URL: Bug Tracker, https://github.com/klrc/sdev/issues
|
|
12
|
-
Keywords: serial,controller,hardware,embedded
|
|
13
|
-
Classifier: Development Status :: 3 - Alpha
|
|
14
|
-
Classifier: Intended Audience :: Developers
|
|
15
|
-
Classifier: License :: OSI Approved :: MIT License
|
|
16
|
-
Classifier: Operating System :: OS Independent
|
|
17
|
-
Classifier: Programming Language :: Python :: 3
|
|
18
|
-
Classifier: Programming Language :: Python :: 3.7
|
|
19
|
-
Classifier: Programming Language :: Python :: 3.8
|
|
20
|
-
Classifier: Programming Language :: Python :: 3.9
|
|
21
|
-
Classifier: Programming Language :: Python :: 3.10
|
|
22
|
-
Classifier: Programming Language :: Python :: 3.11
|
|
23
|
-
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
24
|
-
Classifier: Topic :: System :: Hardware
|
|
25
|
-
Requires-Python: >=3.7
|
|
26
|
-
Description-Content-Type: text/markdown
|
|
27
|
-
License-File: LICENSE
|
|
28
|
-
Requires-Dist: pyserial>=3.5
|
|
29
|
-
Requires-Dist: loguru>=0.6.0
|
|
30
|
-
Dynamic: author
|
|
31
|
-
Dynamic: home-page
|
|
32
|
-
Dynamic: license-file
|
|
33
|
-
Dynamic: requires-python
|
|
34
|
-
|
|
35
|
-
# SDEV
|
|
36
|
-
|
|
37
|
-
串口控制器工具包:后台按行读缓冲、按 flag 扫描与回显着色、执行命令、存活检测。
|
|
38
|
-
|
|
39
|
-
**注意**:同一串口同一时间只能被一个 sdev 进程使用。系统会自动处理跨进程锁,后来的进程会阻塞等待。
|
|
40
|
-
|
|
41
|
-
## 安装
|
|
42
|
-
|
|
43
|
-
```bash
|
|
44
|
-
pip install sdev
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
## 命令行用法
|
|
48
|
-
|
|
49
|
-
安装后可使用 `sdev` 命令。默认会执行存活检测(check_alive)。
|
|
50
|
-
|
|
51
|
-
```bash
|
|
52
|
-
# 执行单条命令并回显
|
|
53
|
-
sdev shell "lsmod | grep -E 'mmz|nnp|vdsp'"
|
|
54
|
-
|
|
55
|
-
# 指定串口和波特率
|
|
56
|
-
sdev -p /dev/ttyUSB1 -b 115200 shell "ls"
|
|
57
|
-
|
|
58
|
-
# 跳过存活检测执行命令
|
|
59
|
-
sdev --no-check-alive shell "echo hello"
|
|
60
|
-
|
|
61
|
-
# 检查本机所有串口状态
|
|
62
|
-
sdev demoboard status
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
- `-p` / `--port`:串口路径(默认取环境变量 `SDEV_PORT` 或 `/dev/ttyUSB0`)
|
|
66
|
-
- `-b` / `--baudrate`:波特率(默认取 `SDEV_BAUDRATE` 或 `115200`)
|
|
67
|
-
- `--flag`:提示符结束标志(默认 `" #"`)
|
|
68
|
-
- `--timeout`:等待输出超时秒数
|
|
69
|
-
|
|
70
|
-
## Python API 使用
|
|
71
|
-
|
|
72
|
-
推荐使用 `sdev()` 工厂函数或 `Demoboard` 类,它们提供了更高层的封装。
|
|
73
|
-
|
|
74
|
-
### 推荐做法
|
|
75
|
-
|
|
76
|
-
```python
|
|
77
|
-
from sdev import sdev
|
|
78
|
-
|
|
79
|
-
# 使用上下文管理器,自动处理 connect() / disconnect()
|
|
80
|
-
with sdev("/dev/ttyUSB0", 115200) as board:
|
|
81
|
-
# 默认 connect 时会阻塞直到拿到串口锁
|
|
82
|
-
# 执行命令,返回输出行列表
|
|
83
|
-
output = board.shell("ls")
|
|
84
|
-
print(output)
|
|
85
|
-
```
|
|
86
|
-
|
|
87
|
-
### 存活检测
|
|
88
|
-
|
|
89
|
-
`Demoboard.check_alive()` 能够处理设备处于 U-Boot、内核启动中或已进入 Shell 的各种状态:
|
|
90
|
-
|
|
91
|
-
```python
|
|
92
|
-
with sdev("/dev/ttyUSB0") as board:
|
|
93
|
-
# 自动识别 U-Boot 并执行 reboot,直到进入 Shell
|
|
94
|
-
board.check_alive()
|
|
95
|
-
board.shell("cat /proc/version")
|
|
96
|
-
```
|
|
97
|
-
|
|
98
|
-
### 核心 API 说明
|
|
99
|
-
|
|
100
|
-
| 类/方法 | 说明 |
|
|
101
|
-
|-------------|------|
|
|
102
|
-
| `sdev(port, baudrate, device_type="demoboard")` | 工厂函数,返回设备实例(Demoboard 或 Relay) |
|
|
103
|
-
| `Demoboard.shell(cmd, prompt_flag=" #", timeout=None, stream=False)` | 执行命令。`stream=True` 时返回 generator |
|
|
104
|
-
| `Demoboard.check_alive(...)` | 综合检查存活状态,支持 U-Boot 自动重启和等待唤醒 |
|
|
105
|
-
| `SerialDevice.send(text)` | 底层接口:发送一行(自动加换行) |
|
|
106
|
-
| `SerialDevice.scan(end_flag, timeout)` | 底层接口:从缓冲中读取直到匹配 flag |
|
|
107
|
-
|
|
108
|
-
## 特色
|
|
109
|
-
|
|
110
|
-
- **回显着色**:匹配到 flag 的行为绿色,命令回显行为青色,其余灰色。
|
|
111
|
-
- **拼行匹配**:设备折行时会自动拼接多行进行 flag 匹配,提高成功率。
|
|
112
|
-
- **跨进程互斥**:基于文件锁,确保多进程安全访问串口。
|
|
113
|
-
- **异步读取**:后台线程实时消费串口数据,避免缓冲区溢出丢失数据。
|
|
114
|
-
|
|
115
|
-
## 依赖
|
|
116
|
-
|
|
117
|
-
- Python >= 3.7
|
|
118
|
-
- pyserial >= 3.5
|
|
119
|
-
- loguru >= 0.6.0
|
sdev-0.4.0/README.md
DELETED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
# SDEV
|
|
2
|
-
|
|
3
|
-
串口控制器工具包:后台按行读缓冲、按 flag 扫描与回显着色、执行命令、存活检测。
|
|
4
|
-
|
|
5
|
-
**注意**:同一串口同一时间只能被一个 sdev 进程使用。系统会自动处理跨进程锁,后来的进程会阻塞等待。
|
|
6
|
-
|
|
7
|
-
## 安装
|
|
8
|
-
|
|
9
|
-
```bash
|
|
10
|
-
pip install sdev
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
## 命令行用法
|
|
14
|
-
|
|
15
|
-
安装后可使用 `sdev` 命令。默认会执行存活检测(check_alive)。
|
|
16
|
-
|
|
17
|
-
```bash
|
|
18
|
-
# 执行单条命令并回显
|
|
19
|
-
sdev shell "lsmod | grep -E 'mmz|nnp|vdsp'"
|
|
20
|
-
|
|
21
|
-
# 指定串口和波特率
|
|
22
|
-
sdev -p /dev/ttyUSB1 -b 115200 shell "ls"
|
|
23
|
-
|
|
24
|
-
# 跳过存活检测执行命令
|
|
25
|
-
sdev --no-check-alive shell "echo hello"
|
|
26
|
-
|
|
27
|
-
# 检查本机所有串口状态
|
|
28
|
-
sdev demoboard status
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
- `-p` / `--port`:串口路径(默认取环境变量 `SDEV_PORT` 或 `/dev/ttyUSB0`)
|
|
32
|
-
- `-b` / `--baudrate`:波特率(默认取 `SDEV_BAUDRATE` 或 `115200`)
|
|
33
|
-
- `--flag`:提示符结束标志(默认 `" #"`)
|
|
34
|
-
- `--timeout`:等待输出超时秒数
|
|
35
|
-
|
|
36
|
-
## Python API 使用
|
|
37
|
-
|
|
38
|
-
推荐使用 `sdev()` 工厂函数或 `Demoboard` 类,它们提供了更高层的封装。
|
|
39
|
-
|
|
40
|
-
### 推荐做法
|
|
41
|
-
|
|
42
|
-
```python
|
|
43
|
-
from sdev import sdev
|
|
44
|
-
|
|
45
|
-
# 使用上下文管理器,自动处理 connect() / disconnect()
|
|
46
|
-
with sdev("/dev/ttyUSB0", 115200) as board:
|
|
47
|
-
# 默认 connect 时会阻塞直到拿到串口锁
|
|
48
|
-
# 执行命令,返回输出行列表
|
|
49
|
-
output = board.shell("ls")
|
|
50
|
-
print(output)
|
|
51
|
-
```
|
|
52
|
-
|
|
53
|
-
### 存活检测
|
|
54
|
-
|
|
55
|
-
`Demoboard.check_alive()` 能够处理设备处于 U-Boot、内核启动中或已进入 Shell 的各种状态:
|
|
56
|
-
|
|
57
|
-
```python
|
|
58
|
-
with sdev("/dev/ttyUSB0") as board:
|
|
59
|
-
# 自动识别 U-Boot 并执行 reboot,直到进入 Shell
|
|
60
|
-
board.check_alive()
|
|
61
|
-
board.shell("cat /proc/version")
|
|
62
|
-
```
|
|
63
|
-
|
|
64
|
-
### 核心 API 说明
|
|
65
|
-
|
|
66
|
-
| 类/方法 | 说明 |
|
|
67
|
-
|-------------|------|
|
|
68
|
-
| `sdev(port, baudrate, device_type="demoboard")` | 工厂函数,返回设备实例(Demoboard 或 Relay) |
|
|
69
|
-
| `Demoboard.shell(cmd, prompt_flag=" #", timeout=None, stream=False)` | 执行命令。`stream=True` 时返回 generator |
|
|
70
|
-
| `Demoboard.check_alive(...)` | 综合检查存活状态,支持 U-Boot 自动重启和等待唤醒 |
|
|
71
|
-
| `SerialDevice.send(text)` | 底层接口:发送一行(自动加换行) |
|
|
72
|
-
| `SerialDevice.scan(end_flag, timeout)` | 底层接口:从缓冲中读取直到匹配 flag |
|
|
73
|
-
|
|
74
|
-
## 特色
|
|
75
|
-
|
|
76
|
-
- **回显着色**:匹配到 flag 的行为绿色,命令回显行为青色,其余灰色。
|
|
77
|
-
- **拼行匹配**:设备折行时会自动拼接多行进行 flag 匹配,提高成功率。
|
|
78
|
-
- **跨进程互斥**:基于文件锁,确保多进程安全访问串口。
|
|
79
|
-
- **异步读取**:后台线程实时消费串口数据,避免缓冲区溢出丢失数据。
|
|
80
|
-
|
|
81
|
-
## 依赖
|
|
82
|
-
|
|
83
|
-
- Python >= 3.7
|
|
84
|
-
- pyserial >= 3.5
|
|
85
|
-
- loguru >= 0.6.0
|
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: sdev
|
|
3
|
-
Version: 0.4.0
|
|
4
|
-
Summary: 串口控制器工具包
|
|
5
|
-
Home-page: https://github.com/klrc/sdev
|
|
6
|
-
Author: klrc
|
|
7
|
-
Author-email: klrc <144069824@qq.com>
|
|
8
|
-
License: MIT
|
|
9
|
-
Project-URL: Homepage, https://github.com/klrc/sdev
|
|
10
|
-
Project-URL: Repository, https://github.com/klrc/sdev
|
|
11
|
-
Project-URL: Bug Tracker, https://github.com/klrc/sdev/issues
|
|
12
|
-
Keywords: serial,controller,hardware,embedded
|
|
13
|
-
Classifier: Development Status :: 3 - Alpha
|
|
14
|
-
Classifier: Intended Audience :: Developers
|
|
15
|
-
Classifier: License :: OSI Approved :: MIT License
|
|
16
|
-
Classifier: Operating System :: OS Independent
|
|
17
|
-
Classifier: Programming Language :: Python :: 3
|
|
18
|
-
Classifier: Programming Language :: Python :: 3.7
|
|
19
|
-
Classifier: Programming Language :: Python :: 3.8
|
|
20
|
-
Classifier: Programming Language :: Python :: 3.9
|
|
21
|
-
Classifier: Programming Language :: Python :: 3.10
|
|
22
|
-
Classifier: Programming Language :: Python :: 3.11
|
|
23
|
-
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
24
|
-
Classifier: Topic :: System :: Hardware
|
|
25
|
-
Requires-Python: >=3.7
|
|
26
|
-
Description-Content-Type: text/markdown
|
|
27
|
-
License-File: LICENSE
|
|
28
|
-
Requires-Dist: pyserial>=3.5
|
|
29
|
-
Requires-Dist: loguru>=0.6.0
|
|
30
|
-
Dynamic: author
|
|
31
|
-
Dynamic: home-page
|
|
32
|
-
Dynamic: license-file
|
|
33
|
-
Dynamic: requires-python
|
|
34
|
-
|
|
35
|
-
# SDEV
|
|
36
|
-
|
|
37
|
-
串口控制器工具包:后台按行读缓冲、按 flag 扫描与回显着色、执行命令、存活检测。
|
|
38
|
-
|
|
39
|
-
**注意**:同一串口同一时间只能被一个 sdev 进程使用。系统会自动处理跨进程锁,后来的进程会阻塞等待。
|
|
40
|
-
|
|
41
|
-
## 安装
|
|
42
|
-
|
|
43
|
-
```bash
|
|
44
|
-
pip install sdev
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
## 命令行用法
|
|
48
|
-
|
|
49
|
-
安装后可使用 `sdev` 命令。默认会执行存活检测(check_alive)。
|
|
50
|
-
|
|
51
|
-
```bash
|
|
52
|
-
# 执行单条命令并回显
|
|
53
|
-
sdev shell "lsmod | grep -E 'mmz|nnp|vdsp'"
|
|
54
|
-
|
|
55
|
-
# 指定串口和波特率
|
|
56
|
-
sdev -p /dev/ttyUSB1 -b 115200 shell "ls"
|
|
57
|
-
|
|
58
|
-
# 跳过存活检测执行命令
|
|
59
|
-
sdev --no-check-alive shell "echo hello"
|
|
60
|
-
|
|
61
|
-
# 检查本机所有串口状态
|
|
62
|
-
sdev demoboard status
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
- `-p` / `--port`:串口路径(默认取环境变量 `SDEV_PORT` 或 `/dev/ttyUSB0`)
|
|
66
|
-
- `-b` / `--baudrate`:波特率(默认取 `SDEV_BAUDRATE` 或 `115200`)
|
|
67
|
-
- `--flag`:提示符结束标志(默认 `" #"`)
|
|
68
|
-
- `--timeout`:等待输出超时秒数
|
|
69
|
-
|
|
70
|
-
## Python API 使用
|
|
71
|
-
|
|
72
|
-
推荐使用 `sdev()` 工厂函数或 `Demoboard` 类,它们提供了更高层的封装。
|
|
73
|
-
|
|
74
|
-
### 推荐做法
|
|
75
|
-
|
|
76
|
-
```python
|
|
77
|
-
from sdev import sdev
|
|
78
|
-
|
|
79
|
-
# 使用上下文管理器,自动处理 connect() / disconnect()
|
|
80
|
-
with sdev("/dev/ttyUSB0", 115200) as board:
|
|
81
|
-
# 默认 connect 时会阻塞直到拿到串口锁
|
|
82
|
-
# 执行命令,返回输出行列表
|
|
83
|
-
output = board.shell("ls")
|
|
84
|
-
print(output)
|
|
85
|
-
```
|
|
86
|
-
|
|
87
|
-
### 存活检测
|
|
88
|
-
|
|
89
|
-
`Demoboard.check_alive()` 能够处理设备处于 U-Boot、内核启动中或已进入 Shell 的各种状态:
|
|
90
|
-
|
|
91
|
-
```python
|
|
92
|
-
with sdev("/dev/ttyUSB0") as board:
|
|
93
|
-
# 自动识别 U-Boot 并执行 reboot,直到进入 Shell
|
|
94
|
-
board.check_alive()
|
|
95
|
-
board.shell("cat /proc/version")
|
|
96
|
-
```
|
|
97
|
-
|
|
98
|
-
### 核心 API 说明
|
|
99
|
-
|
|
100
|
-
| 类/方法 | 说明 |
|
|
101
|
-
|-------------|------|
|
|
102
|
-
| `sdev(port, baudrate, device_type="demoboard")` | 工厂函数,返回设备实例(Demoboard 或 Relay) |
|
|
103
|
-
| `Demoboard.shell(cmd, prompt_flag=" #", timeout=None, stream=False)` | 执行命令。`stream=True` 时返回 generator |
|
|
104
|
-
| `Demoboard.check_alive(...)` | 综合检查存活状态,支持 U-Boot 自动重启和等待唤醒 |
|
|
105
|
-
| `SerialDevice.send(text)` | 底层接口:发送一行(自动加换行) |
|
|
106
|
-
| `SerialDevice.scan(end_flag, timeout)` | 底层接口:从缓冲中读取直到匹配 flag |
|
|
107
|
-
|
|
108
|
-
## 特色
|
|
109
|
-
|
|
110
|
-
- **回显着色**:匹配到 flag 的行为绿色,命令回显行为青色,其余灰色。
|
|
111
|
-
- **拼行匹配**:设备折行时会自动拼接多行进行 flag 匹配,提高成功率。
|
|
112
|
-
- **跨进程互斥**:基于文件锁,确保多进程安全访问串口。
|
|
113
|
-
- **异步读取**:后台线程实时消费串口数据,避免缓冲区溢出丢失数据。
|
|
114
|
-
|
|
115
|
-
## 依赖
|
|
116
|
-
|
|
117
|
-
- Python >= 3.7
|
|
118
|
-
- pyserial >= 3.5
|
|
119
|
-
- loguru >= 0.6.0
|
|
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
|
|
File without changes
|
|
File without changes
|