跳到主要内容

Modbus RTU 驱动 (RTU-over-TCP)

概述

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_typeMODBUS_RTU
实现类plantpulse.driver.protocol.modbus.ModbusRTUDriver
自研原生 (plantpulse-plc-protocolModbusRtuOverTcp)
继承AbstractNativeModbusDriver(非 PLC4j)
read
write✅ (coil / holding-register)
安全无(Modbus 标准本身如此)
非直连串口 — 以串口-TCP 转换器为前提

原生驱动仅处理 TCP 套接字。RS-485/232 设备需通过串口↔TCP 转换器(如 USR-TCP232、Moxa NPort)暴露,并将该转换器的 IP:端口注册为 opc_agent_ip / opc_agent_portbaud-rate / parity / stop-bit 等串行参数需在转换器侧设置,驱动不使用这些参数。


类结构

AbstractNativeModbusDriver (주소 파싱 · 디코드 · read/write 공통)
└── ModbusRTUDriver — createClient() = new ModbusRtuOverTcp(unitId, host, port)
└── ModbusTCPDriver — createClient() = new ModbusTcp(unitId, host, port)

RTU framing(slave address + PDU + CRC-16 LE)由 ModbusRtuOverTcp 在 TCP 流之上处理。


OPC 注册选项

字段含义默认值
opc_agent_ip串口-TCP 转换器 IP192.168.0.60
opc_agent_port转换器 TCP 端口转换器设置值(例 502 / 4001
options.unit-id(别名 unit-identifierModbus 从站 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-based):

表示法含义
coil:1FC 01 — Read Coils
discrete-input:1FC 02 — Read Discrete Inputs
holding-register:1FC 03 — Read Holding Registers (signed 16)
holding-register:1:UINTunsigned 16
holding-register:1:DINTsigned 32(2 个寄存器,MSWord-first)
holding-register:1:REAL32-bit float(2 个寄存器)
holding-register:100:STRING[10]10-word ASCII
input-register:1FC 04 — Read Input Registers

详细表示规则及 data_type · format 映射与 Modbus TCP 驱动页面相同。


Write(原生)

  • coil:N → single coil write (Boolean)。
  • holding-register:N[:TYPE] → 按 datatype 进行 register write(REAL / DINT = MSWord-first 2 个寄存器,其余为单个 16-bit)。
  • discrete-input / input-register 为只读。

不支持的范围

  • 直连串口(/dev/ttyUSB0 — 不支持。前提是经由串口-TCP 转换器。
  • Multi-master / token-passing — 仅支持单 master。
  • Modbus ASCII(冒号分隔、LRC 校验和) — 仅支持 RTU framing。

参考

  • Modbus over Serial Line (V1.02) — Modbus-IDA。
  • 代码:plantpulse-edge-driver/src/plantpulse/driver/protocol/modbus/ModbusRTUDriver.java + plantpulse-plc-protocolmodbus 包。
  • 运维:不要使用 USB ↔ RS-485 适配器,改用串口-TCP 转换器,并确认 120 Ω 终端电阻。unit-id / baud 设置错误时只会出现 timeout。