Modbus RTU 驱动
概述
Modbus RTU — 基于 RS-485 / RS-232 串行线路运行的 Modbus 变体。2026-07 原生重写 — 旧的 Apache PLC4j modbus-rtu 委托已移除。现在使用 plantpulse-plc-protocol 中的原生 RTU-over-TCP 实现(ModbusRtuOverTcp)。串行 RTU 设备通过 串行-TCP 转换器暴露,并注册为 host:port — 驱动仅处理 TCP 套接字。
| 项目 | 值 |
|---|---|
opc_type | MODBUS_RTU |
| 实现类 | plantpulse.driver.protocol.modbus.ModbusRTUDriver |
| 库 | 自主原生 (plantpulse-plc-protocol 的 ModbusRtuOverTcp) |
| 继承 | AbstractNativeModbusDriver (非 PLC4j) |
| read | ✅ |
| write | ✅ (coil / holding-register) |
| 安全 | 无 (Modbus 标准本身) |
非直接串行 — 前提为串行-TCP 转换器
原生驱动仅处理 TCP 套接字。RS-485/232 设备需通过串行↔TCP 转换器(例如: USR-TCP232, Moxa NPort)暴露,并将该转换器的 IP:端口注册为 opc_agent_ip / opc_agent_port。波特率 / 校验位 / 停止位等串行参数在转换器侧配置,驱动不会使用这些参数。
类结构
AbstractNativeModbusDriver (주소 파싱 · 디코드 · read/write 공통)
└── ModbusRTUDriver — createClient() = new ModbusRtuOverTcp(unitId, host, port)
└── ModbusTCPDriver — createClient() = new ModbusTcp(unitId, host, port)
RTU 帧结构(从站地址 + PDU + CRC-16 LE)由 ModbusRtuOverTcp 在 TCP 流上处理。
OPC 注册选项
| 字段 | 含义 | 默认值 |
|---|---|---|
opc_agent_ip | 串行-TCP 转换器 IP | 192.168.0.60 |
opc_agent_port | 转换器 TCP 端口 | 转换器设置值 (例如 502 / 4001) |
options.unit-id (别名 unit-identifier) | Modbus 从站 ID (1..247) | 1 |
options.request-timeout | 响应超时 (ms) | 5000 |
baud-rate/parity/stop-bits等串行参数为转换器侧配置 — 驱动不接收。
标签 plc_address 格式
与 Modbus TCP 相同 — <area>:<address>[:<datatype>][[<count>]] (保留 PLC4X modbus4x 语法,基于 1):
| 表示法 | 含义 |
|---|---|
coil:1 | FC 01 — 读取线圈 |
discrete-input:1 | FC 02 — 读取离散输入 |
holding-register:1 | FC 03 — 读取保持寄存器 (有符号16位) |
holding-register:1:UINT | 无符号16位 |
holding-register:1:DINT | 有符号32位 (2寄存器, 高字在前) |
holding-register:1:REAL | 32位浮点数 (2寄存器) |
holding-register:100:STRING[10] | 10字 ASCII |
input-register:1 | FC 04 — 读取输入寄存器 |
详细的表示规则 / data_type · format 映射与 Modbus TCP 驱动页面相同。
Write (原生)
coil:N→ 单线圈写入 (布尔)。holding-register:N[:TYPE]→ 按数据类型进行寄存器写入 (REAL/DINT= 高字在前 2 寄存器,其他为单 16 位)。discrete-input/input-register为只读。
不支持的领域
- 直接串行端口(
/dev/ttyUSB0) — 不支持。前提为通过串行-TCP 转换器。 - 多主站 / 令牌传递 — 仅支持单主站。
- Modbus ASCII (冒号分隔,LRC 校验和) — 仅支持 RTU 帧。
参考
- Modbus over Serial Line (V1.02) — Modbus-IDA。
- 代码:
plantpulse-edge-driver/src/plantpulse/driver/protocol/modbus/ModbusRTUDriver.java+plantpulse-plc-protocol的modbus包。 - 运维: 代替 USB ↔ RS-485 适配器,使用串行-TCP 转换器并确认终端电阻 120 Ω。若 unit-id / baud 错误,则仅出现超时。