Skip to content
The sensor industry magazine
Since 2008
Subscribe free

Home › Technical articles

Article

Modbus Explained: RTU vs TCP, Registers, Function Codes and Sensor Wiring

Modbus is still the most widely used protocol for reading sensors, meters and drives. This guide explains Modbus RTU and Modbus TCP, the four register tables, the main function codes, how a request looks on the wire and how to fix the most common problems.

Modbus was created in 1979 for programmable controllers and is still everywhere: in energy meters, flow meters, temperature controllers, gas detectors, drives and countless smart sensors. It is simple, open and free to use, which is why almost every PLC, SCADA system and data logger can speak it. If a sensor datasheet lists "digital output", there is a good chance Modbus is one of the options.

Diagram of a Modbus RTU network with one master and several sensor slaves on an RS-485 bus, and a Modbus TCP gateway
Modbus RTU runs on a serial bus; Modbus TCP runs on Ethernet. Gateways connect the two.

How Modbus works

Modbus is a request and response protocol. One device, the master (client), asks a question; the addressed device, the slave (server), answers. Slaves never speak on their own and never talk to each other. The master might ask "slave 5, send me two registers starting at address 16", and the sensor replies with the values.

Modbus only defines how messages are structured, not what the numbers mean. The meaning of each register (temperature in tenths of a degree, flow in litres per minute, a status bit) is defined by the device manufacturer in its register map, so the register map in the manual is the most important document when you integrate a Modbus sensor.

Modbus RTU vs Modbus TCP

Modbus RTUModbus TCP
Physical layerSerial, usually RS-485 (sometimes RS-232)Ethernet
AddressingSlave address 1 to 247 (0 = broadcast)IP address, plus a unit identifier
Error checkCRC-16 at the end of each frameNone in the message; TCP handles integrity
FramingSilent gap of at least 3.5 character times between frames7-byte MBAP header
Typical speed9,600 to 115,200 bit/s10/100/1000 Mbit/s
Portn/aTCP port 502
Best forLow-cost field devices, long cable runs, many sensors on one pairPlant networks, SCADA, gateways, higher data volumes

There is also Modbus ASCII, an older text-based serial format, which is now rare. In practice, many installations combine both: sensors on RS-485 speak Modbus RTU to a gateway, which presents them to the control system as Modbus TCP.

The four Modbus data tables

TableData typeAccessTraditional numberingTypical use
Coils1 bitRead and write0xxxxRelay outputs, on/off commands
Discrete inputs1 bitRead only1xxxxSwitch states, alarms
Input registers16-bit wordRead only3xxxxMeasured values
Holding registers16-bit wordRead and write4xxxxMeasured values, setpoints, configuration

Watch out for the off-by-one trap: manuals often use the traditional 5-digit numbering (register 40001), while the message itself uses zero-based addresses (address 0). If your readings are shifted by one register, this is usually why.

The function codes you will use most

  • 03 Read Holding Registers and 04 Read Input Registers: read measured values. Most sensors use one of these.
  • 01 Read Coils and 02 Read Discrete Inputs: read on/off states.
  • 05 Write Single Coil and 06 Write Single Register: change one output or setting.
  • 15 Write Multiple Coils and 16 Write Multiple Registers: write several values at once.

If a slave cannot carry out a request, it answers with an exception code, for example "illegal data address" when you ask for a register that does not exist.

What a Modbus RTU request looks like

A request to read two holding registers starting at address 0x0010 from slave 17 (0x11) is eight bytes long: 11 03 00 10 00 02 followed by a two-byte CRC. That is the slave address, the function code, the start address, the number of registers and the checksum. The reply contains the slave address, the function code, a byte count and the register values, again followed by a CRC.

Handling values larger than 16 bits

A register holds only 16 bits. Values such as 32-bit integers or floating-point numbers (common for flow totals or calibrated measurements) are spread over two consecutive registers. Devices differ in which register holds the high word, so if a value looks like nonsense, try swapping the word order in your master's settings.

Troubleshooting Modbus RTU

  1. No response at all: check the slave address, baud rate, parity and stop bits on both sides. All must match exactly.
  2. Swap A and B: RS-485 manufacturers label the two lines inconsistently. Swapping them is a quick, harmless test.
  3. Intermittent errors: check termination resistors, cable type and grounding. See our RS-485 wiring guide.
  4. Wrong values: check the register map, the off-by-one offset, scaling factors and word order.
  5. Two devices with the same address: every slave on a bus must have a unique address.

A free Modbus master test tool on a laptop with a USB to RS-485 adapter is the fastest way to prove a sensor works before you blame the PLC.

Frequently asked questions

Is Modbus the same as RS-485?

No. RS-485 is the electrical layer (the wires and voltages). Modbus RTU is the protocol (the message format) that most often runs over RS-485.

How many devices can be on one Modbus RTU network?

Modbus allows addresses 1 to 247, but the RS-485 bus itself limits the number of devices, typically to 32 standard unit loads per segment unless low-load transceivers or repeaters are used.

Should I choose a 4-20 mA or a Modbus sensor?

A 4-20 mA sensor gives one value per pair of wires and is very simple. A Modbus sensor can send many values and diagnostics over one bus, but needs configuration. Many modern transmitters offer both.

Related guides: RS-485 wiring and termination, 4-20 mA current loop and our overview of types of sensors used in industrial automation. See Modbus devices live at SPS 2027.

Find suppliers of Modbus sensors and gateways in the Sensor Times directory.