# Modbus TCP / RTU over TCP

Modbus device registers right in the chat

Регистры Modbus TCP или RTU поверх TCP через назначенный шлюз по карте тегов, только чтение

## How the agent works with Modbus TCP / RTU over TCP

Data is read by the gateway: a program installed as a service on a computer or server on the site network that reaches the device over Modbus TCP, port 502, or a serial gateway in RTU over TCP mode. The connection takes the address, the unit id (1 by default) and a register map: one line per tag with a path, an address and a type, such as holding.100 float32. The gateway sends data out over outbound connections only.

Addresses count from zero, as in the request: register 40001 of a manual is holding.0, and 30010 is input.9. Values in 2 and 4 registers are read in the byte order of the device manual (ABCD, CDAB, BADC, DCBA), and a tag may set its own. Neighbouring tags of one area and one unit are read in one request, up to 125 registers or 2000 bits; the gap between tags is 0 by default, because an unmapped register gives the whole request an exception.

The driver only reads: of the Modbus functions it sends read coils, discrete inputs, holding and input registers (codes 1 to 4). It has no write or combined functions. A device exception marks the tags of that request bad with its code, and a busy answer skips one scan; other units behind the same gateway keep reading.

In RTU over TCP mode a frame has no transaction id, so the gateway keeps one request at a time and after a new connection waits one answer timeout so that a late answer to an earlier request does not reach a tag. A slow device gets pauses after connecting and between requests. If the link to the cloud drops, data piles up on the gateway's disk and arrives once it is back.

## Integration scenarios

### What is on the line now
The agent finds tags by path, such as line1/tank, and shows current values of registers and coils with their timestamp and whether they are fresh.

### Daily summary
The agent takes the temperature, pressure or power history for a day and shows the minimum, maximum, average and last value per hour.

### Usage from a meter
The agent takes a month of readings of a totalizing meter held in a register pair and works out the usage per day.

### A task on a threshold
The agent sets up a task that fires when a tag goes past a threshold and stays there for a set time, not on every sample past it.

### Tags with an exception
The agent names tags the device answered with an exception and its code, such as 0x02 (no such address) or 0x0B (the device behind a gateway did not answer).

### Gaps in the data
The agent shows history intervals without samples and says whether the site gateway is online right now.

## How to connect

**Before you connect**

- A Modbus TCP device or a serial gateway in transparent mode
- Port 502 or the gateway's port open to the site gateway computer
- A register map from the device manual: addresses, types, byte order
- The unit id, if it is not 1
- A computer on the site network for the gateway service

### Choose the gateway's transmission mode
tcp matches each reply by transaction ID. RTU cannot distinguish identical late replies; choose tcp if the gateway supports Modbus TCP.

### Keep one master on the RTU line
rtu_over_tcp requires a serial gateway in transparent mode. Disconnect other masters and TCP clients that receive copies of the same stream.

## FAQ

### How do I connect Modbus TCP?
Add a gateway on the Gateways tab and install it as a service on a computer on the site network. In the connection enter the device address, such as tcp://10.0.12.5:502, the unit id, the byte order and a register map: one line per tag with a path, an address and a type, such as line1/tank/level holding.100 float32 unit=°C. Assign the connection to the gateway and it starts polling.

### What does the agent see in Modbus TCP?
The tags of the register map: coils, discrete inputs, holding and input registers, integer and floating values up to 64 bits, single bits of a register and strings. For each tag the agent reads the current value with its timestamp, the history in intervals and raw samples.

### Does the agent change values in the PLC?
No. The driver sends only the read functions 1 to 4 and has no write functions. The agent writes nothing to the device.

### How do I convert a register number from the manual?
A number like 40001 counts from one, and its first digit names the area: 4 is holding, 3 is input. The address drops that digit and counts from zero: 40001 is holding.0, 30010 is input.9. A bit of a register follows a dot, such as holding.100.3.
