Modbus 点位读
x/modbusRead组件:v0.37.0+ 通过 Modbus 协议(TCP/RTU)批量读取点位,结果以统一契约 iot_points.Data 列表写回 msg.Data,成功走 Success 链、失败走 Failure 链。
与命令式 x/modbus(单点 Cmd/Address/Quantity)不同,本组件面向点位表采集,地址用工业主流的 Modicon 传统地址,输出与 s7/eip/snmp/opcua 一致,可直接接 x/tsdbWrite(配置 measurement)落盘时序库。
需要额外引入扩展库: rulego-components-iot (opens new window)
# 配置
| 字段 | 类型 | 说明 | 默认值 |
|---|---|---|---|
| server | string | Modbus 服务器地址(格式:tcp://host:port 或 rtu:///dev/ttyUSB0) | tcp://127.0.0.1:502 |
| unitId | uint8 | 从机编号 | 1 |
| points | array | 默认采集点位表(见下)。若 msg.Data 带点位列表则优先使用(按需读) | 无 |
| tcpConfig | object | TCP 配置(timeout/certFile/certKeyFile/caFile) | 无 |
| rtuConfig | object | RTU 串口配置(speed/dataBits/parity/stopBits) | 无 |
| encodingConfig | object | 编码配置(endianness/wordOrder,多寄存器字节序) | 大端高字 |
# 点位表 points
| 字段 | 说明 |
|---|---|
| name | 点位名称(输出 Data 的 name) |
| addr | Modicon 地址(1-based):00001=线圈(Coil)、10001=离散输入(Discrete Input)、30001=输入寄存器(Input Register)、40001=保持寄存器(Holding Register);扩展 6 位 400001+ 为保持寄存器 |
| type | 数据类型:BOOL/UINT16/INT16/UINT32/INT32/UINT64/INT64/FLOAT32/FLOAT64 |
| scale | 缩放系数,工程量 = raw × scale + offset |
| offset | 偏移量 |
| endian | 多寄存器字节序:ABCD/CDAB/BADC/DCBA |
各字段支持 ${msg.xx} / ${metadata.xx} 模板。
# 输出
msg.Data(统一契约,与 s7/eip/snmp/opcua 一致):
[
{"name": "温度", "value": 23.5, "timestamp": 1789999999000000000},
{"name": "电机", "value": true, "timestamp": 1789999999000000000}
]
1
2
3
4
2
3
4
# 示例
定时采集(前置 schedule 端点),点位在 points 配置:
{
"id": "modbus_read",
"type": "x/modbusRead",
"configuration": {
"server": "tcp://192.168.1.100:502",
"unitId": 1,
"points": [
{"name": "temp", "addr": "40001", "type": "FLOAT32", "scale": 0.1},
{"name": "motor", "addr": "00001", "type": "BOOL"}
]
}
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
按需读(msg.Data 带点位列表,优先于配置):
[{"name": "setpoint", "addr": "40010", "type": "FLOAT32"}]
1
# 关联组件
- 写入:x/modbusWrite
- 统一采集入口:x/iotRead
- 转时序点落盘:x/tsdbWrite(配置 measurement)
- 命令式(旧):x/modbus(保留,输出也已统一为 iot_points.Data)
在 GitHub 上编辑此页 (opens new window)
上次更新: 2026/08/01, 05:05:14