Skip to content

Modbus TCP (modbus) ​

Reads holding registers, input registers, coils or discrete inputs from a Modbus TCP device or gateway, decodes each value, applies a scale and an offset, and reports it as a channel that can be held to limits. Use it for meters, controllers, drives, sensors and anything else that exposes its readings as Modbus registers: a temperature, a power figure, a tank level, a bit that says a pump is running.

The sensor only reads. It uses function codes 1, 2, 3 and 4 and nothing else; there is no parameter, and no code path, that sends a write.

What it looks like ​

The page of a modbus sensor in the light theme

The sensor's page after a few polls, reading two registers and a floating point value. In the dark theme: the sensor's page. The form for adding it, with its parameters filled in: light, dark.

Channels ​

KeyNameUnitDefault limits
connect_msConnect timemswarning above 1000, error above 5000
response_msResponse timemswarning above 1000, error above 5000
one for each entry of readsits name, or its keyits unitnone: set them on the sensor

response_ms is the time from the first request sent until the last answer was read. A value that cannot be read (the device refuses it with an exception) is left unknown and makes the sensor Down; the other values of the same poll are still reported.

Parameters ​

ParameterDefaultMeaning
port502TCP port of the device or gateway.
unit_id1The unit id (slave address), 0 to 255. A device that answers for itself usually accepts 1 or 255; a gateway uses it to pick the device behind it.
readsrequiredThe values to read, 1 to 50. Each is its own request, sent in order on one connection.

Each entry of reads has:

FieldDefaultMeaning
keyrequiredThe channel key: lower case letters, digits and _, up to 40 characters. Not connect_ms or response_ms, and not used twice.
addressrequiredThe address as it goes on the wire, counted from 0 (0 to 65535).
tableholdingholding (function 3), input (function 4), coil (function 1) or discrete (function 2).
data_typeu16u16 or i16 (one register), u32, i32 or f32 (two registers; f32 is IEEE 754 single precision). Not used for coils and discrete inputs, which read as 0 or 1.
word_orderbigFor two-register values, which register has the high word: big (the lower address, bytes AB CD, the usual order) or little (the lower address has the low word, CD AB).
scale, offset1, 0The reported value is the decoded value times scale plus offset. 0.1 for a temperature the device keeps in tenths of a degree.
name, unitthe key, noneThe name and unit shown for the channel.

Suggested interval: 30 seconds. Credential: none (Modbus has none).

Counting from 0 or from 1. The address in a device's manual often counts from 1, or is written with the table in front of it (40001 is the first holding register, 30001 the first input register). The wire address is the number minus that base, so 40001 is address: 0 with table: holding. When a value comes out wrong by one register, this is the first thing to check. The second is word_order: a 32-bit value that is nonsense with big is usually right with little.

Not offered. Values of 64 bits, byte swapping inside a register, strings, bit fields inside a register (read it as u16 and set limits on the number), and Modbus RTU over a serial line. The last needs a serial port on the probe host and its own settings; a device behind a Modbus TCP gateway is read as TCP with the gateway's unit_id. RTU framing carried over TCP is not spoken either.

On the target ​

A device that speaks Modbus TCP on the port, and that allows the probe's address. Many devices serve a handful of connections only; the sensor opens one for each poll and closes it, so a long interval and few sensors on one device are kind to it. Nothing is installed, and nothing is changed.

Failures ​

MessageMeaning
a timeout (kind timeout), connection refused (kind unreachable)The device did not answer, or nothing listens on the port.
the answer is not Modbus (protocol id is not 0); is this the right port? (kind protocol)Another service answered.
the device closed the connection without answering (kind protocol)The device refuses this client, or the unit id, or its connections are all in use.
the answer is for unit ..., not ..., the answer is for function ... (kind protocol)A gateway or device that answers for another unit.
<key>: exception 2, illegal data address: the device has nothing at this addressA successful poll that is Down. The other exceptions are named too: 1 (illegal function, the table is not supported), 3, 4, 6, and 11 (a gateway whose device did not answer, which usually means a wrong unit_id).

Verification ​

Run against a Modbus TCP device (a simulator on a lab machine) that holds known registers: holding registers 0 to 9 with 100 to 109, input registers 0 to 4 with 1000 to 1004, and a floating point value 21.5 in input registers 5 and 6. Seen live: each table, 16-bit signed and unsigned values, two registers read as a 32-bit integer, the float in both word orders (and the wrong order giving a different number), scale and offset, an address the device does not have (exception 2, with the other values of the poll still reported), and a port nobody listens on. The frames, the bit order of coils, the exceptions, a protocol id that is not 0, a device that closes the connection and one that never answers are also tested against a server written for the tests, which needs no lab. The decoding and the sensor's rules are tested against scripted answers.

Not seen: a device with real coils and discrete inputs (the coil and discrete reads are tested against the test server only), a gateway, a device that limits its connections, a slow device on a serial line behind a gateway.

The live test reads its target from the environment and says so and returns when it is not set:

VariableMeaning
PROBEWARD_TEST_MODBUS_HOSTThe device's address. It must hold the registers listed above in unit 1.
PROBEWARD_TEST_MODBUS_PORTOptional: its port, 502 when not set.
sh
cargo test -p probeward-sensors --test industrial_live modbus -- --nocapture

AGPL-3.0-only