Skip to content

Add TCP, MQTT and Modbus TCP communication links - #1

Merged
txp666 merged 4 commits into
mainfrom
feature/tcp-mqtt-links
Oct 26, 2026
Merged

txp666 merged 4 commits into
mainfrom
feature/tcp-mqtt-links

Conversation

@txp666

@txp666 txp666 commented Oct 26, 2026

Copy link
Copy Markdown
Owner

概要

补齐 README 里标为"开发中"的 TCP 和 MQTT 通信,并顺带支持标准 Modbus TCP(MBAP)。

改动

1. 通信中枢 + TCP / MQTT 链路(ed520c3)

  • 新增 CommHub(app/core/comm_runtime.py),统一持有串口、TCP、MQTT 三条链路。三条链路实现同一组信号,中枢汇总后转发给信号总线,所以数据绑定、实时曲线、表格、日志不需要改动就能接收任意链路的数据;命令会发往每一条已连接的链路。
  • 设计器里的 serialManager 保留为 commHub.serial 的别名,旧调用方不受影响。
  • TCP(tcp_runtime.py):基于 QtNetwork,全部在 GUI 线程,不新增读线程。支持客户端和服务端;服务端可同时接多个设备,每个连接独立切帧,命令写给所有已连接设备。
  • MQTT(mqtt_runtime.py):基于 paho-mqtt,兼容 1.x 和 2.x 回调签名。paho 线程的回调通过带代际号的队列信号转到 GUI 线程,旧客户端的迟到回调一律丢弃。每条消息按一帧解析;纯数值载荷额外以主题名为键,可直接按主题绑定。意外断线自动重连并恢复订阅;首次连接失败(地址错误、认证失败)不重试。支持用户名密码、QoS 0/1/2、TLS、通配符订阅。
  • 新增 TCP 配置卡片 和 MQTT 配置卡片(控件总数 27 → 29),两者共用 link_card.py 基类,设计器预览和导出工程使用同一份源码。
  • 导出工程新增 comm_hub.py、tcp_handler.py、mqtt_handler.py,main.py 改为创建中枢;requirements.txt、导出的 requirements 和一键打包的依赖检查都加入 paho-mqtt。
  • 日志框文案从"串口已连接 / 已断开"改为不带链路名的"已连接 / 已断开"(连接描述里已经包含链路信息,如 TCP 127.0.0.1:8080)。

2. Modbus TCP(9f343c9)

  • 新增解析模式 modbus_tcp:按 MBAP 头的长度字段切帧,校验协议标识和功能码对应长度;输出字典与 RTU 解析器完全一致(单元标识符放在 slave),现有 Modbus 绑定无需修改即可接以太网设备。
  • Modbus 命令改为按链路组帧:解析模式为 modbus_tcp 的链路收到 MBAP 请求(事务标识符逐帧递增并在 65535 后回绕),其余链路照旧收到带 CRC 的 RTU 帧。控件和 Modbus 卡片不需要新增属性;请求只在发送前校验一次,非法请求不会发往任何链路。
  • 串口 / TCP / MQTT 卡片的解析模式下拉都加入 modbus_tcp。

文档与 demo

  • README 协议表、功能列表、数据解析规则更新;docs/protocol.md 新增"六、传输链路"一节和 Modbus TCP 帧格式参考。
  • demo/ 用新生成器重新导出(test_demo 要求提交的导出与生成结果逐字节一致)。

测试

  • 新增 test_tcp_runtime.py(真实回环 socket)、test_mqtt_runtime.py(假 paho 客户端在后台线程触发回调)、test_comm_hub.py、test_link_cards.py(预览与导出一致性),并在 test_protocol_runtime.py、test_command_runtime.py 中补充 Modbus TCP 用例。
  • 包内冒烟测试 dist_smoke.py 新增真实 TCP 回环步骤,用来确认 QtNetwork 被打进编译产物。
  • 本机(macOS arm64,Python 3.14)验证结果:
    • python -m unittest discover -s tests:全部通过
    • python tests/smoke_gui.py:NATIVE_GUI_EXPORT_OK: 29 widget types
    • python deploy.py 全量 Nuitka 打包成功,随后 tests/smoke_dist.py 对产物 zip 验证通过(compiled_runtime: true、tcp_loopback: true)

审阅提示

  • 多链路同时连接时,命令会发往所有已连接链路。这是有意的设计(例如串口和 TCP 同时接同一台设备时只需配置一套控件),但如果有人同时连接两台不同设备,需要知道这一点。docs/protocol.md 第六节已写明。
  • TCP 服务端模式下,HEX 字节提取绑定使用的是合并后的原始字节流,多个设备同时发送时建议改用带键名的行文本 / JSON,文档已说明。
  • 没有改版本号和 release notes,留到发版时处理。
  • Windows 和 Intel Mac 的打包只能由 CI 发布流程覆盖,本机未验证。

🤖 Generated with Claude Code

txp666 and others added 4 commits October 26, 2026 22:27
A CommHub owns the serial, TCP (QtNetwork client/server) and MQTT
(paho-mqtt) links. Every link reports through the same signals, so
bindings, charts, tables and logs stay transport-agnostic, and commands
go out over every connected link. TCP and MQTT configuration cards share
one runtime card frame with the designer preview and exported projects,
which now generate comm_hub.py, tcp_handler.py and mqtt_handler.py.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
A modbus_tcp parser mode frames responses by their MBAP header and
produces the same dictionaries as the RTU parser, so existing Modbus
bindings work unchanged over Ethernet devices. Modbus commands are now
framed for each connected link: links parsing Modbus TCP receive MBAP
requests with rolling transaction identifiers, every other link keeps
receiving RTU frames with CRC. Widgets and the Modbus card need no new
settings.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Checking against a real broker showed two gaps. While a TCP or MQTT link
was connecting, or MQTT was retrying after a drop, both card buttons were
disabled, so the attempt could not be cancelled; 断开 now cancels it and
the card shows 重连中 during retries. MQTT refusals now name the reason
(for example a bad user name or password), a plain connection loss no
longer shows paho's "Unspecified error", paho 1 disconnect codes are no
longer read as CONNACK codes, and a failed TLS connection points at the
port and the server certificate.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
TCP client and server links, MQTT publish and subscribe, and Modbus TCP,
all sharing the serial link's parsing, bindings and commands in the
designer and in exported applications.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@txp666
txp666 merged commit 6b47fa4 into main Oct 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant