Modbus Protocol Integration in Industrial IoT Systems
Modbus is an open industrial communication protocol developed by Modicon (today Schneider Electric) in 1979, built on a master/slave (client/server) model. Thanks to its simple structure it is still the most common shared language between PLCs, transmitters, energy analyzers and IoT gateways. Integrating a device over Modbus means choosing the right variant, interpreting the register map correctly and setting line and timeout parameters properly.
What Modbus variants exist and how do they differ?
| Variant | Physical layer | Data format | Error check | Typical use |
|---|---|---|---|---|
| Modbus RTU | RS485 / RS232 serial line | Binary | CRC-16 | Field devices, transmitters, long lines |
| Modbus ASCII | RS485 / RS232 serial line | ASCII characters | LRC | Legacy systems, debugging |
| Modbus TCP | Ethernet (TCP, port 502) | Binary, with MBAP header | At the TCP/Ethernet layer | SCADA, HMI, IoT gateways, plant networks |
Modbus RTU transmits binary data and carries a CRC-16 checksum at the end of every frame. Modbus ASCII sends the same data as readable characters, which makes debugging easy, but frames are roughly twice as long. Modbus TCP uses Ethernet infrastructure for higher bandwidth and lets several clients connect to the same device; choose based on speed needs, existing cabling and distance. For a short summary, see our Modbus RTU/TCP glossary entry.
Which function codes are used most?
| Code | Operation | Data type |
|---|---|---|
| 0x01 | Read coils | Bit (read/write) |
| 0x02 | Read discrete inputs | Bit (read-only) |
| 0x03 | Read holding registers | 16-bit (read/write) |
| 0x04 | Read input registers | 16-bit (read-only) |
| 0x05 | Write single coil | Bit |
| 0x06 | Write single register | 16-bit |
| 0x10 | Write multiple registers | 16-bit block |
If a device cannot process a request, it returns an exception code by setting the top bit of the function code (e.g. 0x03 → 0x83). The most common ones are: 0x01 illegal function, 0x02 illegal data address, 0x03 illegal data value, 0x04 device failure.
How do you read a Modbus RTU frame? (Hex example)
Let's read 2 holding registers (one 32-bit float value) starting at address 8 from a device with slave address 1.
Request (master → slave):
01 03 00 08 00 02 45 C9
│ │ │ │ └─ CRC-16 (low byte first: 0xC945 → 45 C9)
│ │ │ └──────── Number of registers = 2
│ │ └─────────────── Starting address = 0x0008
│ └─────────────────── Function code 0x03 (Read Holding Registers)
└─────────────────────── Slave address = 1
Response (slave → master):
01 03 04 44 9A 50 00 F2 EC
│ │ │ │ └─ CRC-16 (0xECF2 → F2 EC)
│ │ │ └────────────── Data: register 8 = 0x449A, register 9 = 0x5000
│ │ └────────────────── Byte count = 4
│ └────────────────────── Function code 0x03
└────────────────────────── Slave address = 1
Combining the two registers gives 0x449A5000, which as an IEEE 754 32-bit float is 1234.5. Two details matter here:
- CRC byte order: Register data in Modbus is sent big-endian, but the CRC-16 is sent low byte first. That is why the computed CRC 0xC945 appears on the line as
45 C9. - Word order: The Modbus standard does not define how a 32-bit value is split across two registers. If the device sends the low word first (
50 00 44 9A), combining the same bytes in the wrong order yields a meaningless number of about 8.6 × 10⁹. Always check the word order in the device's register documentation during integration.
Register addressing: 40009 or 8?
Many documents write holding registers in "4xxxx" notation (40001 = first holding register). In the frame on the wire, however, addresses start from zero: 40009 in that notation is address 0x0008 in the frame. If a value appears shifted by one register, this off-by-one is the first thing to suspect.
Modbus RTU implementation over RS485
An excerpt from the RS485-based Modbus implementation used in our ZMA Data Acquisition system:
#define MODBUS_SLAVE_ID 1
#define BAUD_RATE 9600
typedef struct {
uint8_t slave_id;
uint8_t function_code;
uint16_t start_address;
uint16_t num_registers;
uint16_t crc;
} modbus_request_t;
uint16_t calculate_crc16(uint8_t *buffer, uint8_t length) {
uint16_t crc = 0xFFFF;
for (uint8_t i = 0; i < length; i++) {
crc ^= buffer[i];
for (uint8_t j = 0; j < 8; j++) {
if (crc & 0x0001) {
crc = (crc >> 1) ^ 0xA001;
} else {
crc >>= 1;
}
}
}
return crc;
}
For the first 6 bytes of the request above (01 03 00 08 00 02) this function returns 0xC945; when sending, write crc & 0xFF first and then crc >> 8.
How does a Modbus TCP frame differ from RTU?
Modbus TCP replaces the slave address and CRC with a 7-byte MBAP header: transaction identifier (2 bytes), protocol identifier (2 bytes, 0x0000), length (2 bytes) and unit identifier (1 byte). The same read request over TCP looks like this:
00 01 00 00 00 06 01 03 00 08 00 02
There is no CRC because error checking happens at the TCP/Ethernet layer. With an RTU-to-TCP gateway, the unit identifier is usually mapped to the slave address on the serial line.
Practical integration tips
- Timeout management: Set timeouts according to baud rate and frame length; at 9600 baud with 11-bit characters each byte takes about 1.15 ms.
- Error handling: Parse exception codes and count CRC errors to monitor line quality.
- Bus termination: Use 120 Ω termination resistors at both ends of the RS485 line; do not fit them on devices in between.
- Address planning: No two devices on the same line may share a slave address; document address and baud settings.
- Polling rate: Don't poll static parameters often; read fast-changing measurements as consecutive register blocks in a single request.
You can see Modbus used for a weight display in our GDT + HMI integration article, and the digital I/O side on our Conio Modbus Digital I/O Module page.
Summary
Modbus remains a preferred choice in industrial systems thanks to its simplicity, openness and broad device support. On the IoT side it can be bridged to MQTT and cloud systems through gateways. The key to a successful integration is verifying frame structure, address notation and word order against the device documentation.
Our ZMA Data Acquisition system integrates into your industrial systems with Modbus support over RS485. Contact us for detailed information.