Guide

Modbus TCP

Polls coils, discrete inputs and registers on Modbus TCP devices, addressed by device name, unit id and address — and writes coils and holding registers back.

This is the protocol reference for the Modbus TCP agent. For everything else in the config format, see Agent configuration.

01
Driver address

A point is addressed by four fields, which together are its join key (device::unit_id:table:address, plus .bit for a register bit) — so the same register on two devices, or two bits of one register, never collide:

  • device— a name you choose. It must match a device key in the agent's own config (its docker image config, below), which is what actually holds the connection.
  • unit_id — the Modbus unit (slave) id, 0–255. Set per point, so one connection can reach several units — the devices behind a TCP-to-RTU gateway, for instance.
  • address — the 0-based protocol address, not the 4xxxx register notation: register 40001 is holding register address 0, and 30101 is input register address 100.
  • table — which of the four Modbus tables to read: coil (FC01), discrete_input (FC02), holding_register (FC03), or input_register (FC04).

Coils and discrete inputs are single bits and take nothing else — they read as 0/1. A register point also needs a data_type, which says how many registers the value spans and how to decode them:

  • bit — one bit of a single register, read as 0/1. Takes a bit index, 0 (least significant) to 15.
  • u16, i16 — one register, unsigned or signed.
  • u32, i32, f32 — two consecutive registers, starting at address.
  • u64, i64, f64 — four consecutive registers.

Multi-register types also take a byte_order, since devices don't agree on one. The letters name the value's bytes from most to least significant (a is the most significant) in the order they come off the wire:

  • abcd — high word first, big-endian bytes. The Modbus standard, and the default.
  • cdab — low word first (word swap). Common on PLCs.
  • badc — high word first, bytes swapped in each word.
  • dcba — low word first, bytes swapped: fully little-endian.

If a float reads as nonsense, the byte order is the first thing to check. A field only exists where it applies — a byte_order on a u16, or a data_type on a coil, fails schema validation.

poll_group is optional: it names a group from the agent's own config, to poll the point faster or slower than the rest. Points without one are polled together at the default poll_interval. Every poll publishes every point it read, changed or not. Modbus carries no timestamp or quality, so a value is stamped with the time it was read and has good quality.

measurement
{
  "name": "flow",
  "equipment": "skid1",
  "type": "analogical",
  "store": true,
  "driver": {
    "type": "analogical",
    "device": "skid1",
    "unit_id": 1,
    "address": 100,
    "table": "holding_register",
    "data_type": "f32",
    "byte_order": "cdab",
    "poll_group": "fast"
  }
}

Status words often pack several flags into one register — give each its own point with data_type: bit:

measurement
{
  "name": "pump_running",
  "equipment": "skid1",
  "type": "discrete",
  "driver": {
    "type": "digital",
    "device": "skid1",
    "unit_id": 1,
    "address": 40,
    "table": "input_register",
    "data_type": "bit",
    "bit": 3
  }
}
Commands

Same device, unit_id and address, this time naming what to write. Only two tables are writable: coil and holding_register. A discrete command writes its fixed value, an analogical one writes the value sent with the request. Modbus has no select-before-operate, so there's no control model to pick; use an interlock if the write needs a condition first.

  • coil — any non-zero value turns the coil on, zero turns it off. Written with FC05; set write_multiple: true for devices that only accept FC15.
  • holding_register with data_type: bit— sets or clears one bit with a mask write (FC22), leaving the register's other bits untouched.
  • u16, i16 — written with FC06; set write_multiple: true for devices that only accept FC16.
  • u32 through f64 — written with FC16, taking the same byte_order as a measurement.

For an integer type, the value is rounded to the nearest whole number. A value outside the type's range (say -1 for a u16) fails the command without writing anything, and so does a command sent while its device is disconnected. If the device answers with a Modbus exception, that becomes the command's error.

command
{
  "name": "start",
  "equipment": "skid1",
  "driver": {
    "type": "discrete",
    "value": 1,
    "device": "skid1",
    "unit_id": 1,
    "address": 3,
    "table": "coil"
  }
}
command
{
  "name": "speed_setpoint",
  "equipment": "skid1",
  "driver": {
    "type": "analogical",
    "device": "skid1",
    "unit_id": 1,
    "address": 200,
    "table": "holding_register",
    "data_type": "u16"
  }
}
02
Docker image

Image: khorde/modbus-agent:latest. It doesn't serve an API — the only thing on 9090 is a liveness check, GET /health, which returns all_connected plus a connected flag per device — same compose shape as every agent (see Agent configuration). Save the agent's service account key next to it, renamed to service-user.json. Its own config.yaml:

config.yaml
devices:
  skid1:
    tcp:
      host: <device-ip>
      port: 502
  gateway:
    tcp:
      host: <gateway-ip>
    timeout: 2s
    max_gap: 4
poll_interval: 1s
poll_groups:
  fast: 200ms
  slow: 10s
agent:
  broker:
    ip: host.docker.internal
    port: "4222"
    tls: false
  credentials:
    url: http://auth.localhost:9310
    service_account_path: service-user.json
  command_timeout: 10s

devices maps each device name used in the drivers above to one connection. The agent needs at least one, connects to every device on startup whether or not a point references it yet, and keeps retrying any that are down.

  • tcp — the device's host and port (502 by default). Modbus TCP is the only transport today; for serial (RTU) devices, put a TCP-to-RTU gateway in front and address each one by unit_id.
  • timeout — how long one read or write may take (1s by default). A timeout drops the connection and reconnects.
  • max_read_registers / max_read_bits — the largest single read, in registers or in coils and discrete inputs. They default to the protocol maximums, 125 and 2000; lower them for devices that can't serve that much at once.
  • max_gap — how many unconfigured addresses one read may span between two points (0 by default: only adjacent points share a read). Raising it cuts the number of requests per poll.

The agent batches its reads: points on the same device, unit, table and poll group are sorted by address and merged into as few requests as those limits allow. Some devices reject a read that touches an unmapped address — when a merged read comes back with a Modbus exception, the agent reads its points one by one instead, so one bad address doesn't hide the rest.

  • poll_interval — how often the default group, every point without a poll_group, is read (1s by default).
  • poll_groups — named groups and their intervals, e.g. fast: 200ms. A point that names a group missing from here, or a device missing from devices, is skipped with a warning in the agent's log; a command naming an unknown device fails.
  • agent.comms_lost_after — in the agent block, how long a device must stay unreachable before its measurements are republished with bad quality (30s by default). Only that device's points go bad; the other devices keep reporting normally.

Reconnect backoff (reconnect_min_delay, reconnect_max_delay, 1s and 30s by default) is optional too; the defaults work for a first connection.

Next

Agent configuration

The full schema shared by every protocol. Agent configuration.

Other protocols

REST, IEC 60870-5-104, IEC 61850, and TwinCAT ADS agents work the same way.

Taking this past your laptop?

Talk to us