Skip to content
Tutorials Center
My learning

Topics

Lesson 27 of 37 ยท Communication Protocols

Modbus RTU and Modbus TCP for PLCs

Learn Modbus: coils and registers, function codes, RTU and TCP frame formats, exceptions and addressing pitfalls, with runnable C code that builds and parses frames.

Modbus is the grandfather of industrial protocols. Published by Modicon in 1979, it is still found in meters, drives, sensors, gateways and PLCs because it is small, open and easy to implement. If you work with automation you will meet it, probably this month.

No Modbus device or master is available on this site. The lesson shows real frame layouts and a C stand-in that builds and parses them, so you understand what is on the wire.

The data model

Modbus devices expose four tables of numbered items:

Table Type Access Typical use
Coils 1 bit read/write Outputs, commands
Discrete inputs 1 bit read only Switch states
Input registers 16 bit read only Measured values
Holding registers 16 bit read/write Setpoints, settings

A register is 16 bits. A 32-bit float or integer takes two consecutive registers, and the word order (high word first or low word first) varies by vendor. This is the most common source of "my temperature reads nonsense".

Function codes

The master sends a request containing a function code:

Code Name
01 Read coils
02 Read discrete inputs
03 Read holding registers
04 Read input registers
05 Write single coil
06 Write single register
15 (0x0F) Write multiple coils
16 (0x10) Write multiple registers

If the slave cannot do what was asked, it answers with the function code plus 0x80 and an exception code: 01 illegal function, 02 illegal data address, 03 illegal data value, 04 slave device failure.

Modbus RTU frames

RTU runs over serial lines such as RS-485 (see networks overview). A frame is:

TEXT
| Address (1) | Function (1) | Data (n) | CRC-16 (2) |

Frames are separated by silence of at least 3.5 character times. Example request to read 2 holding registers from address 0x0010 on slave 1:

TEXT
01 03 00 10 00 02 C5 CE

Modbus TCP frames

Over Ethernet, Modbus is wrapped in TCP (port 502). The CRC is dropped (TCP checks integrity) and a 7-byte MBAP header is added:

TEXT
| Transaction ID (2) | Protocol ID = 0 (2) | Length (2) | Unit ID (1) | Function (1) | Data (n) |

The transaction ID lets a client match answers to requests. The unit ID selects a device behind a gateway.

Building a request

This C stand-in builds an RTU request and the equivalent TCP request for the same read and prints both as hex bytes.

C C stand-in: build Modbus RTU and TCP read requests

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The two requests carry the same PDU (function, address, count). Only the envelope differs.

Parsing a response

A response to function 3 holds a byte count and the register values, high byte first. Here a parser extracts them and decodes a 32-bit float spread over two registers, trying both word orders.

C C stand-in: parse registers and decode a float

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The same four bytes mean 91.20 under one word order and a meaningless number under the other. When integrating a device, check its manual for word order and test with a known value.

Addressing pitfalls

  • Zero-based versus one-based. The protocol sends zero-based addresses, but manuals often list "40001" for the first holding register. The leading 4 is a legacy convention marking the table, and software may or may not subtract it.
  • Register versus byte. Modbus counts 16-bit registers, not bytes.
  • Endianness. Within a register the high byte comes first; word order for 32-bit values is up to the device.
  • Timeouts and retries. Set a timeout, retry once or twice, then flag the device as failed rather than waiting forever.
  • Security. Modbus has no authentication or encryption. Keep it inside protected networks or use gateways with security.

Modbus is the simplest way to exchange values with a PLC. Richer, self-describing options follow in OPC UA and MQTT.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which Modbus table holds read/write 16-bit setpoints?

    Holding registers can be read and written and are the usual home for setpoints and settings.
  2. 2. What does a slave send when it rejects a function 3 request?

    An exception response sets the high bit of the function code (3 + 0x80 = 0x83) and adds an exception code.
  3. 3. What is the main difference between a Modbus RTU and a Modbus TCP frame?

    The PDU is the same; TCP wraps it in an MBAP header and relies on TCP for integrity.
  4. 4. A 32-bit float read as two registers shows nonsense. What is the most likely cause?

    Vendors store the high and low words in different orders, so swapping them is the first thing to try.

Summary

  • Modbus exposes coils, discrete inputs, input registers and holding registers.
  • Function codes select the operation; errors return the code plus 0x80 and an exception code.
  • RTU adds a CRC-16 on serial lines; TCP uses an MBAP header on port 502.
  • Watch address offsets, word order and timeouts; Modbus has no built-in security.
  • The C programs are stand-ins that build and parse frames.

Next: OPC UA and MQTT for PLCs