桌面 APP 下 ECAT motor 发送只走 pywebview ESF1→native TCP; 删除桥内 HTTP/JSON 回落。附延迟测量与 pack 自测脚本与规格文档。
61 lines
1.7 KiB
Python
61 lines
1.7 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""ESF1 打包/解包单测(无网卡、无 native)。"""
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import struct
|
|
import sys
|
|
|
|
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
|
from esp32_server import ( # noqa: E402
|
|
ESF_FRAME_W,
|
|
ESF_MAGIC,
|
|
FSOK_MAGIC,
|
|
pack_esf,
|
|
unpack_fsok,
|
|
)
|
|
|
|
|
|
def main() -> int:
|
|
frames = [
|
|
{"ch": 2, "id": 0x00030200, "dlc": 32, "data": "01" + "00" * 31},
|
|
{"ch": 0, "id": 1, "dlc": 2, "data": "AABB"},
|
|
]
|
|
body = pack_esf(frames)
|
|
assert len(body) == 8 + 2 * ESF_FRAME_W, len(body)
|
|
magic, n, flags, reserved = struct.unpack_from("<IBBH", body, 0)
|
|
assert magic == ESF_MAGIC
|
|
assert n == 2 and flags == 0 and reserved == 0
|
|
ch0, dlc0, pad0, id0 = struct.unpack_from("<BBHI", body, 8)
|
|
assert (ch0, dlc0, pad0, id0) == (2, 32, 0, 0x00030200)
|
|
data0 = body[8 + 8 : 8 + 8 + 64]
|
|
assert data0[0] == 0x01 and data0[1] == 0x00 and len(data0) == 64
|
|
ch1, dlc1, _pad1, id1 = struct.unpack_from("<BBHI", body, 8 + ESF_FRAME_W)
|
|
assert (ch1, dlc1, id1) == (0, 2, 1)
|
|
assert body[8 + ESF_FRAME_W + 8 : 8 + ESF_FRAME_W + 10] == b"\xAA\xBB"
|
|
|
|
ok = unpack_fsok(struct.pack("<IBBH", FSOK_MAGIC, 1, 3, 7))
|
|
assert ok["ok"] is True and ok["queued"] == 3 and ok["backlog"] == 7
|
|
|
|
bad = unpack_fsok(b"\x00" * 8)
|
|
assert bad["ok"] is False
|
|
|
|
try:
|
|
pack_esf([{"ch": 9, "id": 1, "dlc": 1, "data": "00"}])
|
|
raise AssertionError("非法 ch 未拒绝")
|
|
except ValueError:
|
|
pass
|
|
try:
|
|
pack_esf([])
|
|
raise AssertionError("空帧未拒绝")
|
|
except ValueError:
|
|
pass
|
|
|
|
print("test_esf_pack: PASS")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|