Modbus TCP
The Modbus TCP driver (io.scadex.driver.modbus) connects the gateway to Modbus TCP devices. Each tag bound to it reads one address of one device, polled on the connection's interval, and a write to such a tag writes that address. This page is for engineers who set up Modbus connections and tags.
It speaks the protocol itself and has no third-party dependency, and it declares SDK contract 1.6. Its handling of data types, word order, grouped reads and scanning for data follows ideas from modbux, which is MIT-licensed.
What has been proven
The driver has run against a Modbus TCP device written for its tests. No real PLC or meter has been tried yet.
Adding a connection
Open Connections, then Modbus TCP (/connections/io.scadex.driver.modbus), and add a device. The form is rendered from the driver's descriptor:
| Field | Key | Meaning | Default |
|---|---|---|---|
| Host | host | Required. The device's address or name, without a scheme, a path or a port. | none |
| Port | port | 1 to 65535. | 502 |
| Unit id | unitId | 0 to 255, also called the slave id. A device on its own network address mostly ignores it; a gateway to serial devices uses it to pick one. | 1 |
| Poll interval (ms) | pollIntervalMs | How often every tag on the connection is read. 20 ms to 1 hour. | 1000 |
| Reply timeout (ms) | timeoutMs | How long to wait for an answer before the link counts as lost. 100 ms to 60 s. | 3000 |
| Word and byte order | byteOrder | How a value of more than one register is laid out. A tag can use its own. | ABCD |
| Registers per request | maxRegistersPerRead | The most registers one request reads, 1 to 125. Lower it for a device that refuses long reads. | 100 |
| Read across gaps of | readGap | Unused registers to read through to join two tags into one request. 0 reads only tags that touch. | 0 |
Every connection also has a unique name and an Enabled switch. Test connects, asks the unit for one holding register and reports the time. A device that answers with a Modbus exception counts as reached, because something there speaks Modbus.
Byte and word order
A number of 32 bits or more spans several registers, and the Modbus standard doesn't say in which order a device sends them. The orders are named by the bytes A (most significant) to D of the number:
| Order | Registers of 0x12345678 | Meaning |
|---|---|---|
ABCD | 0x1234 0x5678 | The standard: high byte and high word first |
CDAB | 0x5678 0x1234 | Low word first |
BADC | 0x3412 0x7856 | Bytes swapped inside each word |
DCBA | 0x7856 0x3412 | Little-endian all the way |
If a float reads as nonsense, try the others. Text keeps its words in sequence; BADC and DCBA swap the two characters inside each word.
Tags
A tag bound to this driver has the binding document:
{ "area": "holding", "address": 100, "dataType": "float32" }
| Field | Meaning |
|---|---|
area | holding (4x, read and write), input (3x, read-only), coil (0x, read and write) or discrete (1x, read-only). Default holding. |
address | Counted from 0, as the device receives it. The register a PLC manual calls 40001 is address 0 in holding. |
dataType | bool, int16, uint16, int32, uint32, int64, uint64, float32, float64 or string. Default uint16; a coil or discrete input is always bool. |
byteOrder | Optional. The tag's own order instead of the connection's. |
unitId | Optional. Another unit on the same connection. |
bit | Optional. For a bool read from a register: bit 0 (lowest) to 15. Without it, the register is on when it isn't 0. |
length | Required for string: how many registers the text takes, two characters each, up to 120. |
A 16-bit type takes one register, 32-bit types two and 64-bit types four. Scaling, units and alarms are the tag's own, as for every tag.
Making tags
- New tag on the connection page makes one tag from these fields.
- Browse lists the four areas, then ranges of a thousand and of a hundred addresses, then the addresses with what the device holds there now (decimal, hex and the PLC manual's number). An address the device refuses is shown as not readable; when it refuses a whole range, the range is split to find which parts are readable. Tick addresses, choose a data type and a byte order and create the tags. A type of two or four registers starts at the address ticked.
- Discovery also reads addresses typed as text:
holding:100,hr100,input:5:float32,3@coil:0(unit 3), and a PLC manual's five- or six-digit number such as40101(holding register 100). A bare number under five digits is refused, because it could be counted from 0 or from 1.
A tag made this way has kind external; the tag editor shows it as a Driver tag, with the same fields.
How the link runs
- The driver connects with the reply timeout and retries every 5 seconds after a failure. While the link is down every bound tag is marked bad and keeps its last value. A request that isn't answered in time, or a connection the device closes, counts as the link being lost.
- Every poll reads the tags in as few requests as it can. Tags of the same area and unit that touch, or are within the gap, share one request, up to Registers per request.
- A value is published when it changes, when its quality changes, and every 30 seconds even if neither did.
- When a device refuses a request that holds several tags (illegal data address or value), each of those tags is read alone from then on, so the ones that are fine go on working. The refused tag is bad, with the device's words as its quality detail, and is asked again at most every 5 to 30 seconds. The tags are grouped again after a reconnect.
- A tag whose binding can't be read (an unknown area or type, an address past 65535) is bad with the reason.
Writing
A write uses function code 5 for a coil, 6 for a single register and 16 for several registers. A bit of a register is written by reading the register, changing that bit and writing the register back, one write at a time. Input registers and discrete inputs can't be written. A value that doesn't fit the type is refused with a sentence: 70000 for a uint16, 3.7 for an int16, text that is too long. A device that answers with an exception makes the write fail with "The device refused the write" and its reason.
Connection status
The gateway publishes each connection under [system]connection.modbus.<name>: whether it is connected, its tag count, its last error, and numbers with history: values and messages per second, requests per poll, and how many tags are good, uncertain, bad or waiting.
API
Besides the standard driver routes (/connections, /connections/{id}/test, /browse, /discover and the rest, under /api/v1/modules/io.scadex.driver.modbus):
| Route | Role | Meaning |
|---|---|---|
GET /api/v1/modules/io.scadex.driver.modbus/status | any signed-in user | The live state of each link |
GET /api/v1/modules/io.scadex.driver.modbus/connections/{id}/read | any signed-in user | Raw registers or bits as the device holds them. Query: area, address, count (default 1), unit (default the connection's) |