litearm-python 1.2.5__tar.gz → 1.3.0__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.
- {litearm_python-1.2.5 → litearm_python-1.3.0}/PKG-INFO +45 -1
- {litearm_python-1.2.5 → litearm_python-1.3.0}/README.md +44 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/pyproject.toml +1 -1
- {litearm_python-1.2.5 → litearm_python-1.3.0}/src/litearm/__init__.py +1 -1
- {litearm_python-1.2.5 → litearm_python-1.3.0}/src/litearm/arm.py +11 -2
- {litearm_python-1.2.5 → litearm_python-1.3.0}/src/litearm/transport.py +16 -0
- litearm_python-1.3.0/src/litearm/transport_tcp.py +255 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/src/litearm_python.egg-info/PKG-INFO +45 -1
- {litearm_python-1.2.5 → litearm_python-1.3.0}/src/litearm_python.egg-info/SOURCES.txt +2 -0
- litearm_python-1.3.0/tests/test_transport_tcp.py +217 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/LICENSE +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/setup.cfg +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/src/litearm/__main__.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/src/litearm/_protocol.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/src/litearm/_rot.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/src/litearm/cart.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/src/litearm/diagnostics.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/src/litearm/errors.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/src/litearm/log.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/src/litearm/model.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/src/litearm/params.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/src/litearm/payload.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/src/litearm/py.typed +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/src/litearm/state.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/src/litearm/testing.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/src/litearm/tool.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/src/litearm_python.egg-info/dependency_links.txt +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/src/litearm_python.egg-info/entry_points.txt +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/src/litearm_python.egg-info/requires.txt +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/src/litearm_python.egg-info/top_level.txt +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/tests/test_ack_echo.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/tests/test_arm_offline.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/tests/test_banner.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/tests/test_capability.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/tests/test_cart_errors.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/tests/test_cart_protocol.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/tests/test_cart_queue.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/tests/test_cart_wait.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/tests/test_cli.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/tests/test_dfu.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/tests/test_diagnostics.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/tests/test_doc_examples.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/tests/test_env_sh.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/tests/test_error_codes.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/tests/test_error_hierarchy.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/tests/test_ff_io.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/tests/test_fork_guard.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/tests/test_frame_ownership.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/tests/test_full_coverage.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/tests/test_home.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/tests/test_ik_bench.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/tests/test_license.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/tests/test_lifecycle.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/tests/test_live.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/tests/test_log.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/tests/test_model.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/tests/test_msg.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/tests/test_offline.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/tests/test_package_metadata.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/tests/test_params.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/tests/test_payload.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/tests/test_protocol_crc.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/tests/test_protocol_sync.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/tests/test_public_api.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/tests/test_request_and_get.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/tests/test_silent_noops.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/tests/test_status_layout.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/tests/test_teardown.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/tests/test_tool_offset.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/tests/test_transport.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/tests/test_transport_injection.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/tests/test_transport_threadsafe.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/tests/test_zero_g.py +0 -0
- {litearm_python-1.2.5 → litearm_python-1.3.0}/tests/test_zero_g_cart_guard.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: litearm-python
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.3.0
|
|
4
4
|
Summary: Python SDK for the LiteArm robotic arm
|
|
5
5
|
Author: NEXFORM ROBOTICS
|
|
6
6
|
License-Expression: Apache-2.0
|
|
@@ -145,6 +145,7 @@ from there.
|
|
|
145
145
|
`Arm().connect()` is the only entry point. Every session carries a read thread, so **you must
|
|
146
146
|
`close()` it** — `with` does that for you. Pass `port` to pick the serial port; leave it out and
|
|
147
147
|
the SDK auto-discovers it (VID:PID `1d50:606f`). The SDK **reads no environment variables**.
|
|
148
|
+
`port` also takes a network target — see [Over TCP](#over-tcp).
|
|
148
149
|
|
|
149
150
|
Every `arm` below refers to that connected session object; the snippets show only the steps
|
|
150
151
|
that section is about.
|
|
@@ -160,6 +161,46 @@ print(arm.n) # joint count
|
|
|
160
161
|
arm.close() # disconnect (a with block does this for you)
|
|
161
162
|
```
|
|
162
163
|
|
|
164
|
+
#### Over TCP
|
|
165
|
+
|
|
166
|
+
`port` also accepts a network target, so the same script drives a firmware stand-in — a
|
|
167
|
+
MuJoCo-backed simulation, or anything else that speaks this protocol — with nothing changed
|
|
168
|
+
below the connection call:
|
|
169
|
+
|
|
170
|
+
```text
|
|
171
|
+
pa.Arm(port="tcp://127.0.0.1:5601").connect()
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
Three spellings are accepted: `tcp://host:port`, `sim://host:port`, and a bare `host:port`
|
|
175
|
+
(exactly one colon, digits after it). A serial path is never mistaken for one:
|
|
176
|
+
|
|
177
|
+
```python
|
|
178
|
+
from litearm.transport import is_tcp_target
|
|
179
|
+
|
|
180
|
+
is_tcp_target("tcp://127.0.0.1:5601") # True
|
|
181
|
+
is_tcp_target("127.0.0.1:5601") # True — bare host:port
|
|
182
|
+
is_tcp_target("sim://127.0.0.1:5601") # True
|
|
183
|
+
is_tcp_target("/dev/ttyACM0") # False — still serial
|
|
184
|
+
is_tcp_target("COM5") # False
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
This is **not a second backend**: the frames, their timing assumptions and the error classes are
|
|
188
|
+
the serial ones — only the carrier changes, from USB CDC to a socket — so every `Arm` method
|
|
189
|
+
behaves identically. What does differ:
|
|
190
|
+
|
|
191
|
+
- **auto-discovery does not apply.** It enumerates USB CDC (`1d50:606f`) only, and with no arm
|
|
192
|
+
plugged in `Arm().connect()` raises. Name the network target explicitly.
|
|
193
|
+
- Nothing extra to install: stdlib `socket` / `select` only, so `pyserial` stays the sole
|
|
194
|
+
dependency.
|
|
195
|
+
- An unreachable endpoint fails inside `connect()` with `TransportError` (2 s connect timeout),
|
|
196
|
+
and a peer that closes the socket also raises `TransportError` — the link does not sit there
|
|
197
|
+
looking idle until some unrelated timeout fires.
|
|
198
|
+
- The target string is kept verbatim, so `connect()` on a live session remains a no-op instead
|
|
199
|
+
of re-targeting it.
|
|
200
|
+
|
|
201
|
+
`connect()` picks the carrier in this order: an injected `transport_factory`, then a
|
|
202
|
+
TCP-looking target, then `SerialTransport`.
|
|
203
|
+
|
|
163
204
|
### Joint motion
|
|
164
205
|
|
|
165
206
|
All three need `enable()` first: `movej` is a single shot (the firmware plans the S-curve and
|
|
@@ -349,6 +390,9 @@ litearm-python fw # version string + axis count
|
|
|
349
390
|
litearm-python tcp # current pose + frame rate
|
|
350
391
|
litearm-python movej -0.1 0 0 0 0 0 0 --speed 0.3
|
|
351
392
|
litearm-python home
|
|
393
|
+
|
|
394
|
+
# the same commands against a simulated firmware over TCP
|
|
395
|
+
litearm-python --port tcp://127.0.0.1:5601 status
|
|
352
396
|
```
|
|
353
397
|
|
|
354
398
|
`status` / `fw` / `tcp` are read-only; the rest really drive the arm.
|
|
@@ -115,6 +115,7 @@ from there.
|
|
|
115
115
|
`Arm().connect()` is the only entry point. Every session carries a read thread, so **you must
|
|
116
116
|
`close()` it** — `with` does that for you. Pass `port` to pick the serial port; leave it out and
|
|
117
117
|
the SDK auto-discovers it (VID:PID `1d50:606f`). The SDK **reads no environment variables**.
|
|
118
|
+
`port` also takes a network target — see [Over TCP](#over-tcp).
|
|
118
119
|
|
|
119
120
|
Every `arm` below refers to that connected session object; the snippets show only the steps
|
|
120
121
|
that section is about.
|
|
@@ -130,6 +131,46 @@ print(arm.n) # joint count
|
|
|
130
131
|
arm.close() # disconnect (a with block does this for you)
|
|
131
132
|
```
|
|
132
133
|
|
|
134
|
+
#### Over TCP
|
|
135
|
+
|
|
136
|
+
`port` also accepts a network target, so the same script drives a firmware stand-in — a
|
|
137
|
+
MuJoCo-backed simulation, or anything else that speaks this protocol — with nothing changed
|
|
138
|
+
below the connection call:
|
|
139
|
+
|
|
140
|
+
```text
|
|
141
|
+
pa.Arm(port="tcp://127.0.0.1:5601").connect()
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
Three spellings are accepted: `tcp://host:port`, `sim://host:port`, and a bare `host:port`
|
|
145
|
+
(exactly one colon, digits after it). A serial path is never mistaken for one:
|
|
146
|
+
|
|
147
|
+
```python
|
|
148
|
+
from litearm.transport import is_tcp_target
|
|
149
|
+
|
|
150
|
+
is_tcp_target("tcp://127.0.0.1:5601") # True
|
|
151
|
+
is_tcp_target("127.0.0.1:5601") # True — bare host:port
|
|
152
|
+
is_tcp_target("sim://127.0.0.1:5601") # True
|
|
153
|
+
is_tcp_target("/dev/ttyACM0") # False — still serial
|
|
154
|
+
is_tcp_target("COM5") # False
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
This is **not a second backend**: the frames, their timing assumptions and the error classes are
|
|
158
|
+
the serial ones — only the carrier changes, from USB CDC to a socket — so every `Arm` method
|
|
159
|
+
behaves identically. What does differ:
|
|
160
|
+
|
|
161
|
+
- **auto-discovery does not apply.** It enumerates USB CDC (`1d50:606f`) only, and with no arm
|
|
162
|
+
plugged in `Arm().connect()` raises. Name the network target explicitly.
|
|
163
|
+
- Nothing extra to install: stdlib `socket` / `select` only, so `pyserial` stays the sole
|
|
164
|
+
dependency.
|
|
165
|
+
- An unreachable endpoint fails inside `connect()` with `TransportError` (2 s connect timeout),
|
|
166
|
+
and a peer that closes the socket also raises `TransportError` — the link does not sit there
|
|
167
|
+
looking idle until some unrelated timeout fires.
|
|
168
|
+
- The target string is kept verbatim, so `connect()` on a live session remains a no-op instead
|
|
169
|
+
of re-targeting it.
|
|
170
|
+
|
|
171
|
+
`connect()` picks the carrier in this order: an injected `transport_factory`, then a
|
|
172
|
+
TCP-looking target, then `SerialTransport`.
|
|
173
|
+
|
|
133
174
|
### Joint motion
|
|
134
175
|
|
|
135
176
|
All three need `enable()` first: `movej` is a single shot (the firmware plans the S-curve and
|
|
@@ -319,6 +360,9 @@ litearm-python fw # version string + axis count
|
|
|
319
360
|
litearm-python tcp # current pose + frame rate
|
|
320
361
|
litearm-python movej -0.1 0 0 0 0 0 0 --speed 0.3
|
|
321
362
|
litearm-python home
|
|
363
|
+
|
|
364
|
+
# the same commands against a simulated firmware over TCP
|
|
365
|
+
litearm-python --port tcp://127.0.0.1:5601 status
|
|
322
366
|
```
|
|
323
367
|
|
|
324
368
|
`status` / `fw` / `tcp` are read-only; the rest really drive the arm.
|
|
@@ -92,7 +92,7 @@ from litearm.state import JointState, RobotState
|
|
|
92
92
|
from litearm.tool import TcpOffset
|
|
93
93
|
from litearm.transport import find_cdc_port
|
|
94
94
|
|
|
95
|
-
__version__ = "1.
|
|
95
|
+
__version__ = "1.3.0"
|
|
96
96
|
|
|
97
97
|
__all__ = [
|
|
98
98
|
"Arm", "Msg", "LicenseInfo", "RobotState", "JointState", "find_cdc_port", "main",
|
|
@@ -47,7 +47,7 @@ from litearm.diagnostics import Diagnostics
|
|
|
47
47
|
from litearm.log import ArmLog
|
|
48
48
|
from litearm.model import ModelParams
|
|
49
49
|
from litearm.params import JointParams
|
|
50
|
-
from litearm.transport import SerialTransport, find_cdc_port
|
|
50
|
+
from litearm.transport import SerialTransport, find_cdc_port, is_tcp_target
|
|
51
51
|
|
|
52
52
|
__all__ = ["Arm", "Msg", "main", "MIN_FW", "FIRMWARE_PREFIX"]
|
|
53
53
|
|
|
@@ -931,7 +931,16 @@ class Arm:
|
|
|
931
931
|
raise TransportError("未找到 STM32 CDC (VID:PID 1d50:606f), 请用 --port 指定")
|
|
932
932
|
# 唯一的传输构造点(`reconnect()` = `close()` + 本函数,故也走这里)。
|
|
933
933
|
# 注入了工厂就用工厂 —— 离线 (`testing.FakeTransport`) 与真机走同一条会话装配路径。
|
|
934
|
-
|
|
934
|
+
if self._transport_factory is not None:
|
|
935
|
+
self._tr = self._transport_factory(p)
|
|
936
|
+
elif is_tcp_target(p):
|
|
937
|
+
# 远端/仿真: 帧与串口完全相同, 只是载体是 socket (见 transport_tcp)。
|
|
938
|
+
# ⚠ 刻意**不**把串口分支也塞进一个总工厂: 那会挡掉
|
|
939
|
+
# `tests/conftest.py` 替换 `SerialTransport` 的注入点。
|
|
940
|
+
from litearm.transport_tcp import TcpTransport
|
|
941
|
+
self._tr = TcpTransport(p)
|
|
942
|
+
else:
|
|
943
|
+
self._tr = SerialTransport(p)
|
|
935
944
|
self._a = _Ack(self)
|
|
936
945
|
#: fork 守卫的落章点 —— 本会话属于**这个**进程 (见 `ForkedSessionError`)。
|
|
937
946
|
#: 位置: 会话对象一建好就落, 早于下面那句握手写帧, 也早于读线程起。
|
|
@@ -370,6 +370,22 @@ class SerialTransport:
|
|
|
370
370
|
raise TransportError(f"读失败: {e}") from e
|
|
371
371
|
|
|
372
372
|
|
|
373
|
+
def is_tcp_target(port: object) -> bool:
|
|
374
|
+
"""`port` 是不是一个"要走 TCP 的远端目标"(`tcp://` / `sim://` / 裸 `host:port`)。
|
|
375
|
+
|
|
376
|
+
判定与误判防护在 :func:`litearm.transport_tcp.is_tcp_url`; 这里只是薄委托,
|
|
377
|
+
**不在本模块再实现一遍** (两份判定必然漂)。
|
|
378
|
+
|
|
379
|
+
为什么本模块需要知道这件事、而不是干脆提供一个 `make_transport()` 总工厂:
|
|
380
|
+
`Arm.connect()` 的串口分支必须**仍然直接引用 `SerialTransport` 这个名字** ——
|
|
381
|
+
离线测试与 `litearm-server --dry-run` 都靠替换那个属性来注入假传输
|
|
382
|
+
(`tests/conftest.py` 的 `fake_transport_factory`)。总工厂会把那个注入点挡掉。
|
|
383
|
+
所以分流的形状是"TCP 例外走新路, 其余原路不动", 而不是"所有传输过一道工厂"。
|
|
384
|
+
"""
|
|
385
|
+
from litearm.transport_tcp import is_tcp_url
|
|
386
|
+
return is_tcp_url(port)
|
|
387
|
+
|
|
388
|
+
|
|
373
389
|
def _now() -> float:
|
|
374
390
|
import time
|
|
375
391
|
return time.monotonic()
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
"""TCP 传输 —— 与串口**同一套帧**, 只是载体从 USB CDC 换成 socket。
|
|
2
|
+
|
|
3
|
+
用途只有一个: 把 `litearm-python` 接到一个"固件替身"上 (仿真的 MuJoCo 后端, 或任何
|
|
4
|
+
在别处实现了本协议的东西), 命令行与真机逐字节相同 —— 于是同一份采集/策略脚本
|
|
5
|
+
不用改一行, 只是把端口从 `/dev/ttyACM0` 换成 `tcp://127.0.0.1:5601`。
|
|
6
|
+
|
|
7
|
+
**它不是"另一个后端"**: 帧的语义、时序假设、错误分类全部与 :mod:`litearm.transport`
|
|
8
|
+
一致。下面每一条都是 :class:`~litearm.transport.SerialTransport` 的契约照抄, 因为它们
|
|
9
|
+
是 `Arm` 层的承重假设, 不是串口特有的实现细节:
|
|
10
|
+
|
|
11
|
+
* :attr:`port` **原样保存 URL 字符串** —— `Arm.connect()` 的幂等比较读的就是它
|
|
12
|
+
(`arm.py` 里 `port == getattr(self._tr, "port", None)`)。存成 `(host, port)` 元组会让
|
|
13
|
+
重复 `connect()` 每次都被判成"换了目标", 把在途笛卡尔记账与零重力保活推倒重来。
|
|
14
|
+
* :meth:`read_frame` 超时返回 ``None``, **半帧留在缓冲里**不丢; 且
|
|
15
|
+
``timeout=0`` 是"有就给我", 不是"一个字节都不碰" (否则 `Arm.poll_cart()` 恒 ``None``)。
|
|
16
|
+
* :meth:`write_frame` 的**写失败必须抛** :class:`~litearm.errors.TransportError` ——
|
|
17
|
+
`_CartPending` 靠"抛了 ⟹ 整帧未送达"这条不变量摘 token, 破了就是**假成功**。
|
|
18
|
+
* :meth:`close` 幂等; 对端关闭 (recv 得到 ``b""``) 要**报链路断**, 不能静默返回 ``None``
|
|
19
|
+
—— 否则上层只能靠超时发现, 归因会指向"固件不理我"。
|
|
20
|
+
|
|
21
|
+
⚠ 零依赖: 只用标准库 ``socket``/``select``。本包的唯一依赖仍是 pyserial。
|
|
22
|
+
"""
|
|
23
|
+
from __future__ import annotations
|
|
24
|
+
|
|
25
|
+
import select
|
|
26
|
+
import socket
|
|
27
|
+
import threading
|
|
28
|
+
import time
|
|
29
|
+
from typing import Optional, Tuple
|
|
30
|
+
|
|
31
|
+
from litearm import _protocol as P
|
|
32
|
+
from litearm.errors import TransportError
|
|
33
|
+
|
|
34
|
+
#: 残缺帧 (SOF 起算但未收齐) 最多等多久。与 `transport._PARTIAL_MAX_S` 同值同理由:
|
|
35
|
+
#: 噪声里凑出的假帧头永远收不齐, 不设上限就会把后面的真帧永久挡住。
|
|
36
|
+
_PARTIAL_MAX_S = 0.25
|
|
37
|
+
|
|
38
|
+
#: 读超时之后那段"非阻塞补读"的时间上限 (秒)。照抄 `transport._NONBLOCK_EXTRA_S`:
|
|
39
|
+
#: `timeout=0` 时要把**已经到达**的字节读进来, 但数据持续到达时不能变成无界循环。
|
|
40
|
+
_NONBLOCK_EXTRA_S = 0.02
|
|
41
|
+
|
|
42
|
+
#: 一次 recv 的字节数。串口那边按 `in_waiting` 要, socket 这边一次给多少都行 ——
|
|
43
|
+
#: 装配循环本来就按"来多少字节都可能"写的。
|
|
44
|
+
_RECV_MAX = 4096
|
|
45
|
+
|
|
46
|
+
#: 连接超时。真机上是 `serial.Serial()` 打开设备, 这边是 TCP 三次握手。
|
|
47
|
+
_CONNECT_TIMEOUT_S = 2.0
|
|
48
|
+
|
|
49
|
+
#: 写超时 —— **必须有界**。`emergency_stop()` 必须永远可达 (见 SerialTransport 的同名注释)。
|
|
50
|
+
_WRITE_TIMEOUT_S = 0.5
|
|
51
|
+
|
|
52
|
+
_SCHEMES = ("tcp://", "sim://")
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def is_tcp_url(port: object) -> bool:
|
|
56
|
+
"""`port` 是不是一个"要走 TCP 的目标"。
|
|
57
|
+
|
|
58
|
+
认三种写法: `tcp://host:port`、`sim://host:port`、以及裸的 `host:port`
|
|
59
|
+
(只含一个冒号、冒号后是纯数字、不含路径分隔符 —— 这样 `/dev/ttyACM0`
|
|
60
|
+
与 `COM3` 都不会被误判)。
|
|
61
|
+
"""
|
|
62
|
+
if not isinstance(port, str) or not port:
|
|
63
|
+
return False
|
|
64
|
+
if port.startswith(_SCHEMES):
|
|
65
|
+
return True
|
|
66
|
+
if "/" in port or "\\" in port or port.count(":") != 1:
|
|
67
|
+
return False
|
|
68
|
+
host, _, portnum = port.partition(":")
|
|
69
|
+
if not host or not portnum.isdigit():
|
|
70
|
+
return False
|
|
71
|
+
# ⚠ 必须查范围: 光看 isdigit 会把 "host:70000" 判成 TCP 目标, 然后在
|
|
72
|
+
# connect 时以一个和"端口写错"毫不相干的错误炸掉。
|
|
73
|
+
return 0 < int(portnum) < 65536
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def parse_tcp_url(port: str) -> Tuple[str, int]:
|
|
77
|
+
"""`tcp://host:port` / `host:port` -> `(host, port)`。非法时抛 ValueError。"""
|
|
78
|
+
s = port
|
|
79
|
+
for scheme in _SCHEMES:
|
|
80
|
+
if s.startswith(scheme):
|
|
81
|
+
s = s[len(scheme):]
|
|
82
|
+
break
|
|
83
|
+
host, sep, portnum = s.rpartition(":")
|
|
84
|
+
if not sep or not host:
|
|
85
|
+
raise ValueError(f"TCP 目标应形如 tcp://host:port (收到 {port!r})")
|
|
86
|
+
try:
|
|
87
|
+
n = int(portnum)
|
|
88
|
+
except ValueError:
|
|
89
|
+
raise ValueError(f"端口号不是整数: {portnum!r} (来自 {port!r})") from None
|
|
90
|
+
if not 0 < n < 65536:
|
|
91
|
+
raise ValueError(f"端口号越界: {n} (来自 {port!r})")
|
|
92
|
+
return host, n
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
class TcpTransport:
|
|
96
|
+
"""帧读写传输 (TCP 版)。接口与行为契约见模块 docstring。
|
|
97
|
+
|
|
98
|
+
**读写各一把锁, 读写之间不互斥** —— 与 :class:`~litearm.transport.SerialTransport`
|
|
99
|
+
同结构: 读者互斥 `_buf`/`_partial_since`, 写者互斥"一帧的字节原子交给 socket";
|
|
100
|
+
读写之间并发本来就安全, 而用单把锁罩住会让一次阻塞读把保活写拖过 0.1s 看门狗窗口。
|
|
101
|
+
"""
|
|
102
|
+
|
|
103
|
+
TEXT_LOG_MAX = 2048
|
|
104
|
+
|
|
105
|
+
def __init__(self, port: str, timeout: float = 0.2,
|
|
106
|
+
connect_timeout: float = _CONNECT_TIMEOUT_S):
|
|
107
|
+
self.port = port # ⚠ 原样保存, 见模块 docstring
|
|
108
|
+
self.timeout = timeout
|
|
109
|
+
host, portnum = parse_tcp_url(port) if isinstance(port, str) else (None, None)
|
|
110
|
+
if host is None:
|
|
111
|
+
raise TransportError(f"不是合法的 TCP 目标: {port!r}")
|
|
112
|
+
try:
|
|
113
|
+
self._sock = socket.create_connection((host, portnum),
|
|
114
|
+
timeout=connect_timeout)
|
|
115
|
+
except OSError as e:
|
|
116
|
+
raise TransportError(f"连接 {port} 失败: {e}") from e
|
|
117
|
+
try:
|
|
118
|
+
self._sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
|
|
119
|
+
except OSError:
|
|
120
|
+
pass # 无 Nagle 更好; 设不上不致命
|
|
121
|
+
self._sock.settimeout(None) # 一律用 select 控阻塞, 不用 socket 超时
|
|
122
|
+
self._buf = bytearray()
|
|
123
|
+
self._partial_since: Optional[float] = None
|
|
124
|
+
self._text = bytearray()
|
|
125
|
+
self._rlock = threading.RLock()
|
|
126
|
+
self._wlock = threading.RLock()
|
|
127
|
+
self._closed = False
|
|
128
|
+
self._peer_closed = False
|
|
129
|
+
self.flush_failures = 0 # 与串口同名同义 (socket 无 flush, 恒 0)
|
|
130
|
+
|
|
131
|
+
# -- 状态 ---------------------------------------------------------------
|
|
132
|
+
@property
|
|
133
|
+
def text_log(self) -> str:
|
|
134
|
+
"""最近被跳过的非帧字节 —— 与串口同语义 (开机签名在那边靠它解析)。"""
|
|
135
|
+
with self._rlock:
|
|
136
|
+
return bytes(self._text).decode("utf-8", errors="replace")
|
|
137
|
+
|
|
138
|
+
@property
|
|
139
|
+
def is_open(self) -> bool:
|
|
140
|
+
if self._closed:
|
|
141
|
+
return False
|
|
142
|
+
try:
|
|
143
|
+
return not self._peer_closed
|
|
144
|
+
except Exception: # noqa: BLE001
|
|
145
|
+
return False
|
|
146
|
+
|
|
147
|
+
def close(self) -> None:
|
|
148
|
+
"""幂等关闭。**先 shutdown 再 close** —— 只 close 会让对端读到一个
|
|
149
|
+
半关闭的连接而继续写, 从这里看就是"关了还在收"。"""
|
|
150
|
+
with self._rlock, self._wlock:
|
|
151
|
+
if self._closed:
|
|
152
|
+
return
|
|
153
|
+
self._closed = True
|
|
154
|
+
try:
|
|
155
|
+
self._sock.shutdown(socket.SHUT_RDWR)
|
|
156
|
+
except OSError:
|
|
157
|
+
pass
|
|
158
|
+
try:
|
|
159
|
+
self._sock.close()
|
|
160
|
+
except OSError:
|
|
161
|
+
pass
|
|
162
|
+
|
|
163
|
+
# -- 写 -----------------------------------------------------------------
|
|
164
|
+
def write_frame(self, cmd: int, payload: bytes = b"") -> None:
|
|
165
|
+
"""写一帧。失败**必须抛** `TransportError` (整帧未送达的判据, 见模块 docstring)。"""
|
|
166
|
+
frame = P.pack_frame(cmd, payload)
|
|
167
|
+
with self._wlock:
|
|
168
|
+
if self._closed or self._peer_closed:
|
|
169
|
+
raise TransportError(f"写失败: {self.port} 已关闭")
|
|
170
|
+
try:
|
|
171
|
+
self._sock.settimeout(_WRITE_TIMEOUT_S)
|
|
172
|
+
self._sock.sendall(frame)
|
|
173
|
+
except OSError as e:
|
|
174
|
+
raise TransportError(f"写失败: {e}") from e
|
|
175
|
+
finally:
|
|
176
|
+
try:
|
|
177
|
+
self._sock.settimeout(None)
|
|
178
|
+
except OSError:
|
|
179
|
+
pass
|
|
180
|
+
|
|
181
|
+
# -- 读 -----------------------------------------------------------------
|
|
182
|
+
def read_frame(self, timeout: Optional[float] = None) -> Optional[Tuple[int, bytes]]:
|
|
183
|
+
"""读一帧; 超时返回 None。跳过噪声字节, 丢弃 CRC 坏帧 (与串口逐字同义)。"""
|
|
184
|
+
end = time.monotonic() + (self.timeout if timeout is None else timeout)
|
|
185
|
+
with self._rlock:
|
|
186
|
+
return self._read_frame_locked(end)
|
|
187
|
+
|
|
188
|
+
def _read_frame_locked(self, end: float) -> Optional[Tuple[int, bytes]]:
|
|
189
|
+
nb_end: Optional[float] = None
|
|
190
|
+
while True:
|
|
191
|
+
while self._buf:
|
|
192
|
+
if self._buf[0] != P.SOF:
|
|
193
|
+
self._note_text(self._buf[0])
|
|
194
|
+
del self._buf[0]
|
|
195
|
+
continue
|
|
196
|
+
if len(self._buf) < 3:
|
|
197
|
+
break
|
|
198
|
+
ln = self._buf[2]
|
|
199
|
+
total = 3 + ln + 2
|
|
200
|
+
if len(self._buf) < total:
|
|
201
|
+
break
|
|
202
|
+
got = P.unpack_frame(bytes(self._buf[:total]))
|
|
203
|
+
if got is not None:
|
|
204
|
+
del self._buf[:total]
|
|
205
|
+
self._partial_since = None
|
|
206
|
+
return got
|
|
207
|
+
del self._buf[0]
|
|
208
|
+
now = time.monotonic()
|
|
209
|
+
if self._buf and self._buf[0] == P.SOF:
|
|
210
|
+
if self._partial_since is None:
|
|
211
|
+
self._partial_since = now
|
|
212
|
+
elif now - self._partial_since > _PARTIAL_MAX_S:
|
|
213
|
+
del self._buf[0]
|
|
214
|
+
self._partial_since = None
|
|
215
|
+
continue
|
|
216
|
+
else:
|
|
217
|
+
self._partial_since = None
|
|
218
|
+
if now >= end:
|
|
219
|
+
# 窗口用尽, 但仍要把**已经到达**的字节读进来 (timeout=0 的语义)
|
|
220
|
+
if nb_end is None:
|
|
221
|
+
nb_end = now + _NONBLOCK_EXTRA_S
|
|
222
|
+
elif now > nb_end:
|
|
223
|
+
return None
|
|
224
|
+
chunk = self._recv(0.0)
|
|
225
|
+
if chunk:
|
|
226
|
+
self._buf += chunk
|
|
227
|
+
continue
|
|
228
|
+
return None
|
|
229
|
+
chunk = self._recv(end - now)
|
|
230
|
+
if chunk:
|
|
231
|
+
self._buf += chunk
|
|
232
|
+
|
|
233
|
+
def _recv(self, wait: float) -> Optional[bytes]:
|
|
234
|
+
"""等至多 `wait` 秒取一块。对端关闭时抛 `TransportError` (链路断, 不静默)。"""
|
|
235
|
+
if self._closed:
|
|
236
|
+
raise TransportError("读失败: 链路已关闭")
|
|
237
|
+
try:
|
|
238
|
+
ready, _, _ = select.select([self._sock], [], [], max(wait, 0.0))
|
|
239
|
+
except (OSError, ValueError) as e:
|
|
240
|
+
raise TransportError(f"读失败: {e}") from e
|
|
241
|
+
if not ready:
|
|
242
|
+
return None
|
|
243
|
+
try:
|
|
244
|
+
data = self._sock.recv(_RECV_MAX)
|
|
245
|
+
except OSError as e:
|
|
246
|
+
raise TransportError(f"读失败: {e}") from e
|
|
247
|
+
if not data:
|
|
248
|
+
self._peer_closed = True
|
|
249
|
+
raise TransportError(f"读失败: {self.port} 对端关闭了连接")
|
|
250
|
+
return data
|
|
251
|
+
|
|
252
|
+
def _note_text(self, byte: int) -> None:
|
|
253
|
+
self._text.append(byte)
|
|
254
|
+
if len(self._text) > self.TEXT_LOG_MAX:
|
|
255
|
+
del self._text[:len(self._text) - self.TEXT_LOG_MAX]
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: litearm-python
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.3.0
|
|
4
4
|
Summary: Python SDK for the LiteArm robotic arm
|
|
5
5
|
Author: NEXFORM ROBOTICS
|
|
6
6
|
License-Expression: Apache-2.0
|
|
@@ -145,6 +145,7 @@ from there.
|
|
|
145
145
|
`Arm().connect()` is the only entry point. Every session carries a read thread, so **you must
|
|
146
146
|
`close()` it** — `with` does that for you. Pass `port` to pick the serial port; leave it out and
|
|
147
147
|
the SDK auto-discovers it (VID:PID `1d50:606f`). The SDK **reads no environment variables**.
|
|
148
|
+
`port` also takes a network target — see [Over TCP](#over-tcp).
|
|
148
149
|
|
|
149
150
|
Every `arm` below refers to that connected session object; the snippets show only the steps
|
|
150
151
|
that section is about.
|
|
@@ -160,6 +161,46 @@ print(arm.n) # joint count
|
|
|
160
161
|
arm.close() # disconnect (a with block does this for you)
|
|
161
162
|
```
|
|
162
163
|
|
|
164
|
+
#### Over TCP
|
|
165
|
+
|
|
166
|
+
`port` also accepts a network target, so the same script drives a firmware stand-in — a
|
|
167
|
+
MuJoCo-backed simulation, or anything else that speaks this protocol — with nothing changed
|
|
168
|
+
below the connection call:
|
|
169
|
+
|
|
170
|
+
```text
|
|
171
|
+
pa.Arm(port="tcp://127.0.0.1:5601").connect()
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
Three spellings are accepted: `tcp://host:port`, `sim://host:port`, and a bare `host:port`
|
|
175
|
+
(exactly one colon, digits after it). A serial path is never mistaken for one:
|
|
176
|
+
|
|
177
|
+
```python
|
|
178
|
+
from litearm.transport import is_tcp_target
|
|
179
|
+
|
|
180
|
+
is_tcp_target("tcp://127.0.0.1:5601") # True
|
|
181
|
+
is_tcp_target("127.0.0.1:5601") # True — bare host:port
|
|
182
|
+
is_tcp_target("sim://127.0.0.1:5601") # True
|
|
183
|
+
is_tcp_target("/dev/ttyACM0") # False — still serial
|
|
184
|
+
is_tcp_target("COM5") # False
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
This is **not a second backend**: the frames, their timing assumptions and the error classes are
|
|
188
|
+
the serial ones — only the carrier changes, from USB CDC to a socket — so every `Arm` method
|
|
189
|
+
behaves identically. What does differ:
|
|
190
|
+
|
|
191
|
+
- **auto-discovery does not apply.** It enumerates USB CDC (`1d50:606f`) only, and with no arm
|
|
192
|
+
plugged in `Arm().connect()` raises. Name the network target explicitly.
|
|
193
|
+
- Nothing extra to install: stdlib `socket` / `select` only, so `pyserial` stays the sole
|
|
194
|
+
dependency.
|
|
195
|
+
- An unreachable endpoint fails inside `connect()` with `TransportError` (2 s connect timeout),
|
|
196
|
+
and a peer that closes the socket also raises `TransportError` — the link does not sit there
|
|
197
|
+
looking idle until some unrelated timeout fires.
|
|
198
|
+
- The target string is kept verbatim, so `connect()` on a live session remains a no-op instead
|
|
199
|
+
of re-targeting it.
|
|
200
|
+
|
|
201
|
+
`connect()` picks the carrier in this order: an injected `transport_factory`, then a
|
|
202
|
+
TCP-looking target, then `SerialTransport`.
|
|
203
|
+
|
|
163
204
|
### Joint motion
|
|
164
205
|
|
|
165
206
|
All three need `enable()` first: `movej` is a single shot (the firmware plans the S-curve and
|
|
@@ -349,6 +390,9 @@ litearm-python fw # version string + axis count
|
|
|
349
390
|
litearm-python tcp # current pose + frame rate
|
|
350
391
|
litearm-python movej -0.1 0 0 0 0 0 0 --speed 0.3
|
|
351
392
|
litearm-python home
|
|
393
|
+
|
|
394
|
+
# the same commands against a simulated firmware over TCP
|
|
395
|
+
litearm-python --port tcp://127.0.0.1:5601 status
|
|
352
396
|
```
|
|
353
397
|
|
|
354
398
|
`status` / `fw` / `tcp` are read-only; the rest really drive the arm.
|
|
@@ -18,6 +18,7 @@ src/litearm/state.py
|
|
|
18
18
|
src/litearm/testing.py
|
|
19
19
|
src/litearm/tool.py
|
|
20
20
|
src/litearm/transport.py
|
|
21
|
+
src/litearm/transport_tcp.py
|
|
21
22
|
src/litearm_python.egg-info/PKG-INFO
|
|
22
23
|
src/litearm_python.egg-info/SOURCES.txt
|
|
23
24
|
src/litearm_python.egg-info/dependency_links.txt
|
|
@@ -65,6 +66,7 @@ tests/test_teardown.py
|
|
|
65
66
|
tests/test_tool_offset.py
|
|
66
67
|
tests/test_transport.py
|
|
67
68
|
tests/test_transport_injection.py
|
|
69
|
+
tests/test_transport_tcp.py
|
|
68
70
|
tests/test_transport_threadsafe.py
|
|
69
71
|
tests/test_zero_g.py
|
|
70
72
|
tests/test_zero_g_cart_guard.py
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
"""TCP 传输 —— 契约与串口逐条对齐 (见 `litearm/transport_tcp.py` 模块 docstring)。
|
|
2
|
+
|
|
3
|
+
这些用例**不依赖**任何仿真后端: 一个本地 socket 假装成固件, 只验传输层本身。
|
|
4
|
+
"""
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
import contextlib
|
|
8
|
+
import socket
|
|
9
|
+
import threading
|
|
10
|
+
import time
|
|
11
|
+
|
|
12
|
+
import pytest
|
|
13
|
+
|
|
14
|
+
from litearm import _protocol as P
|
|
15
|
+
from litearm import transport as tr_mod
|
|
16
|
+
from litearm.errors import TransportError
|
|
17
|
+
from litearm.transport import is_tcp_target
|
|
18
|
+
from litearm.transport_tcp import TcpTransport, is_tcp_url, parse_tcp_url
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@contextlib.contextmanager
|
|
22
|
+
def fake_firmware():
|
|
23
|
+
"""起一个只监听 127.0.0.1 的"固件", 返回 (url, 服务端 socket)。"""
|
|
24
|
+
srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
25
|
+
srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
26
|
+
srv.bind(("127.0.0.1", 0))
|
|
27
|
+
srv.listen(1)
|
|
28
|
+
port = srv.getsockname()[1]
|
|
29
|
+
accepted = {}
|
|
30
|
+
|
|
31
|
+
def _accept():
|
|
32
|
+
conn, _ = srv.accept()
|
|
33
|
+
accepted["conn"] = conn
|
|
34
|
+
|
|
35
|
+
th = threading.Thread(target=_accept, daemon=True)
|
|
36
|
+
th.start()
|
|
37
|
+
try:
|
|
38
|
+
yield f"tcp://127.0.0.1:{port}", accepted
|
|
39
|
+
finally:
|
|
40
|
+
th.join(timeout=2.0)
|
|
41
|
+
for s in (accepted.get("conn"), srv):
|
|
42
|
+
if s is not None:
|
|
43
|
+
try:
|
|
44
|
+
s.close()
|
|
45
|
+
except OSError:
|
|
46
|
+
pass
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _conn(box, timeout=2.0):
|
|
50
|
+
"""等服务端 accept 落地, 返回那条连接 (构造 TcpTransport 是异步完成的)。"""
|
|
51
|
+
end = time.time() + timeout
|
|
52
|
+
while "conn" not in box and time.time() < end:
|
|
53
|
+
time.sleep(0.005)
|
|
54
|
+
assert "conn" in box, "服务端没接受连接"
|
|
55
|
+
return box["conn"]
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
# --------------------------------------------------------------------------- #
|
|
59
|
+
# 端口字符串的判定
|
|
60
|
+
# --------------------------------------------------------------------------- #
|
|
61
|
+
@pytest.mark.parametrize("port", ["tcp://127.0.0.1:5601", "sim://localhost:9000",
|
|
62
|
+
"127.0.0.1:5601", "host:1"])
|
|
63
|
+
def test_tcp_urls_are_recognized(port):
|
|
64
|
+
assert is_tcp_url(port) is True
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
@pytest.mark.parametrize("port", ["/dev/ttyACM0", "COM3", "fake", "", None, 42,
|
|
68
|
+
"C:\\Users\\x", "host:abc", "host:70000", ":5601"])
|
|
69
|
+
def test_non_tcp_ports_are_not_misdetected(port):
|
|
70
|
+
assert is_tcp_url(port) is False, f"{port!r} 不应被判成 TCP"
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def test_parse_rejects_bad_urls():
|
|
74
|
+
for bad in ("tcp://noport", "tcp://:123", "tcp://h:notanumber", "tcp://h:99999"):
|
|
75
|
+
with pytest.raises(ValueError):
|
|
76
|
+
parse_tcp_url(bad)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def test_parse_accepts_scheme_less_form():
|
|
80
|
+
assert parse_tcp_url("127.0.0.1:5601") == ("127.0.0.1", 5601)
|
|
81
|
+
assert parse_tcp_url("sim://localhost:7") == ("localhost", 7)
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
# --------------------------------------------------------------------------- #
|
|
85
|
+
# 收发契约
|
|
86
|
+
# --------------------------------------------------------------------------- #
|
|
87
|
+
def test_roundtrip_write_then_read():
|
|
88
|
+
with fake_firmware() as (url, accepted):
|
|
89
|
+
tr = TcpTransport(url, timeout=0.5)
|
|
90
|
+
try:
|
|
91
|
+
tr.write_frame(P.CMD_GET_STATUS)
|
|
92
|
+
_conn(accepted).settimeout(2.0)
|
|
93
|
+
data = _conn(accepted).recv(64)
|
|
94
|
+
assert P.unpack_frame(data) == (P.CMD_GET_STATUS, b"")
|
|
95
|
+
_conn(accepted).sendall(P.pack_frame(P.RSP_FIRMWARE, b"Litearm1.10.0-7J"))
|
|
96
|
+
got = tr.read_frame(timeout=2.0)
|
|
97
|
+
assert got == (P.RSP_FIRMWARE, b"Litearm1.10.0-7J")
|
|
98
|
+
finally:
|
|
99
|
+
tr.close()
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def test_port_is_kept_verbatim_for_connect_idempotency():
|
|
103
|
+
"""`Arm.connect()` 的幂等判据读的是 `transport.port`, 必须是原样的 URL。"""
|
|
104
|
+
with fake_firmware() as (url, accepted):
|
|
105
|
+
tr = TcpTransport(url)
|
|
106
|
+
try:
|
|
107
|
+
assert tr.port == url
|
|
108
|
+
assert isinstance(tr.port, str)
|
|
109
|
+
finally:
|
|
110
|
+
tr.close()
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def test_half_frame_survives_a_timeout():
|
|
114
|
+
"""半帧超时**不丢**: 下一次读要能接着把它装配出来 (串口的同名契约)。"""
|
|
115
|
+
with fake_firmware() as (url, accepted):
|
|
116
|
+
tr = TcpTransport(url, timeout=0.05)
|
|
117
|
+
try:
|
|
118
|
+
frame = P.pack_frame(P.RSP_TCP, P.pack_f32s([0.1, 0.2, 0.3, 0.0, 0.0, 0.0]))
|
|
119
|
+
conn = _conn(accepted)
|
|
120
|
+
conn.sendall(frame[:6])
|
|
121
|
+
assert tr.read_frame(timeout=0.15) is None, "半帧不该装配成功"
|
|
122
|
+
conn.sendall(frame[6:])
|
|
123
|
+
got = tr.read_frame(timeout=1.0)
|
|
124
|
+
assert got is not None and got[0] == P.RSP_TCP
|
|
125
|
+
finally:
|
|
126
|
+
tr.close()
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def test_timeout_zero_returns_what_already_arrived():
|
|
130
|
+
"""`timeout=0` 是"有就给我" —— `Arm.poll_cart()` 靠它。"""
|
|
131
|
+
with fake_firmware() as (url, accepted):
|
|
132
|
+
tr = TcpTransport(url, timeout=0.2)
|
|
133
|
+
try:
|
|
134
|
+
_conn(accepted).sendall(P.pack_frame(P.RSP_ACK, bytes([P.CMD_ENABLE])))
|
|
135
|
+
time.sleep(0.05) # 让字节真的到达内核缓冲
|
|
136
|
+
got = tr.read_frame(0)
|
|
137
|
+
assert got == (P.RSP_ACK, bytes([P.CMD_ENABLE]))
|
|
138
|
+
finally:
|
|
139
|
+
tr.close()
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def test_noise_bytes_go_to_text_log():
|
|
143
|
+
with fake_firmware() as (url, accepted):
|
|
144
|
+
tr = TcpTransport(url, timeout=0.2)
|
|
145
|
+
try:
|
|
146
|
+
_conn(accepted).sendall(b"[litearm-usbcdc] ready (normal)")
|
|
147
|
+
assert tr.read_frame(timeout=0.2) is None
|
|
148
|
+
assert "ready" in tr.text_log
|
|
149
|
+
finally:
|
|
150
|
+
tr.close()
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def test_peer_close_raises_instead_of_silently_timing_out():
|
|
154
|
+
"""对端关闭必须**报链路断**: 静默超时会把归因指向"固件不理我"。"""
|
|
155
|
+
with fake_firmware() as (url, accepted):
|
|
156
|
+
tr = TcpTransport(url, timeout=0.5)
|
|
157
|
+
try:
|
|
158
|
+
_conn(accepted).close()
|
|
159
|
+
with pytest.raises(TransportError):
|
|
160
|
+
for _ in range(50):
|
|
161
|
+
tr.read_frame(timeout=0.05)
|
|
162
|
+
finally:
|
|
163
|
+
tr.close()
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
def test_write_after_close_raises_transport_error():
|
|
167
|
+
with fake_firmware() as (url, accepted):
|
|
168
|
+
tr = TcpTransport(url)
|
|
169
|
+
tr.close()
|
|
170
|
+
with pytest.raises(TransportError):
|
|
171
|
+
tr.write_frame(P.CMD_GET_STATUS)
|
|
172
|
+
assert tr.is_open is False
|
|
173
|
+
tr.close() # 幂等
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def test_connect_failure_is_a_transport_error():
|
|
177
|
+
"""端口没人监听时, 必须是本包的 TransportError (调用方按它做分支)。"""
|
|
178
|
+
with pytest.raises(TransportError):
|
|
179
|
+
TcpTransport("tcp://127.0.0.1:1", connect_timeout=0.5)
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
# --------------------------------------------------------------------------- #
|
|
183
|
+
# 分发 (Arm.connect 的分支)
|
|
184
|
+
# --------------------------------------------------------------------------- #
|
|
185
|
+
def test_is_tcp_target_delegates_to_the_same_rules():
|
|
186
|
+
assert is_tcp_target("tcp://127.0.0.1:5601") is True
|
|
187
|
+
assert is_tcp_target("127.0.0.1:5601") is True
|
|
188
|
+
assert is_tcp_target("/dev/ttyACM0") is False
|
|
189
|
+
assert is_tcp_target("fake") is False
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
def test_arm_keeps_using_serial_transport_for_non_tcp_ports(monkeypatch):
|
|
193
|
+
"""串口分支必须**仍然直接引用 `arm.SerialTransport`** —— 那是离线测试与
|
|
194
|
+
`litearm-server --dry-run` 的注入点 (tests/conftest.py 靠替换它注桩)。
|
|
195
|
+
"""
|
|
196
|
+
from litearm import arm as arm_mod
|
|
197
|
+
from litearm.testing import FakeTransport
|
|
198
|
+
|
|
199
|
+
made = []
|
|
200
|
+
|
|
201
|
+
def factory(port):
|
|
202
|
+
made.append(port)
|
|
203
|
+
return FakeTransport()
|
|
204
|
+
|
|
205
|
+
monkeypatch.setattr(arm_mod, "SerialTransport", factory)
|
|
206
|
+
arm_mod.Arm(port="fake").connect().close()
|
|
207
|
+
assert made == ["fake"], "非 TCP 端口没有走 SerialTransport 注入点"
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
def test_arm_routes_a_tcp_url_to_the_tcp_transport(monkeypatch):
|
|
211
|
+
from litearm import arm as arm_mod
|
|
212
|
+
|
|
213
|
+
monkeypatch.setattr(arm_mod, "SerialTransport",
|
|
214
|
+
lambda *a, **k: pytest.fail("TCP 目标不该去开串口"))
|
|
215
|
+
with pytest.raises(TransportError):
|
|
216
|
+
# 端口没人监听 -> 传输层报 TransportError, 但**不是**串口路径
|
|
217
|
+
arm_mod.Arm(port="tcp://127.0.0.1:1").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
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{litearm_python-1.2.5 → litearm_python-1.3.0}/src/litearm_python.egg-info/dependency_links.txt
RENAMED
|
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
|
|
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
|
|
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
|
|
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
|