Skip to main content

Modbus TCP Protocol

Introduction

The Modbus protocol is a messaging structure which was developed by Modicon in 1979, and later released as an open and free-to-use standard. The full standards documents can be downloaded from modbus.org for reference.

There are several variants of the protocol, which use the same basic messages encapsulated in different ways for transfer over serial lines (Modbus RTU and Modbus ASCII) or TCP/IP networks (Modbus TCP).

Modbus is a query-response protocol. Master devices send out query messages, and slave devices send back a response message for each query addressed to them. In the case of Modbus TCP, the slave device is a TCP/IP server which waits for a connection to be made to it (e.g. a Brainboxes ED device) and the master is the device which initiates the TCP connection (e.g. a PLC, HMI or PC).

Data Tables

Modbus organises the data to be transferred between devices into four address spaces or "data tables", each of which has 65536 addressable entries:

Data TableDescription
Discrete inputsSingle-bit (Boolean) values which can only be read
CoilsSingle-bit (Boolean) values which can be written as well as read
Input registers16-bit values which can only be read
Holding registers16-bit values which can be written as well as read

The Modbus standard does not specify how inputs and outputs should be related to addresses in these data tables, or how the values inside the 16-bit registers should be structured. These are up to each manufacturer to decide.

Supported Function Codes

Address SpaceReadWrite SingleWrite Multiple
Discrete inputs2--
Coils1515
Input registers4--
Holding registers3616

Slave ID

As well as the addresses for the inputs, coils, and registers of a device, there is also an "address" to identify a particular Modbus slave, known as the Slave ID or Unit Identifier. In Modbus TCP the Slave ID is somewhat redundant as the destination of the message is already defined by the IP address of the TCP packet. Brainboxes devices can be configured to either:

  • Respond to Slave IDs 0 and 255 only (as required by the Modbus TCP specification)
  • Respond to any valid Slave ID (0 to 247, and 255) for convenience

Addressing Notations

The long history of Modbus has resulted in several common ways for data addresses to be written. When setting up other devices or software to communicate with Brainboxes Modbus TCP products, you will need to identify the type of address notation being used.

Logical Addressing

Within Modbus messages, addresses are 16-bit values (0 to 65535). The Logical address is this number expressed in hexadecimal. Each type of addressable object has its own independent address space. When using logical addressing, the type of object always needs to be stated as well. Logical addresses are written as hexadecimal with a '0x' prefix.

984 Style Addressing

This notation (named after an early Modicon controller model) is widely used. The address is written in decimal with an offset of 1 from the logical address, padded to 4 digits, then a prefix digit indicates the address space:

984 AddressesTypeLogical Addresses
00001-09999Coil0-9998 (0x0000-0x270E)
10001-19999Discrete input0-9998 (0x0000-0x270E)
30001-39999Input register0-9998 (0x0000-0x270E)
40001-49999Holding register0-9998 (0x0000-0x270E)

The prefix is sometimes written as shorthand: '0x' for coils, '1x' for discrete inputs, '3x' for input registers, '4x' for holding registers.

IEC 61131 Addressing

Some PLCs and HMIs use IEC 61131 notation. %M addresses refer to coils, %MW addresses refer to holding registers. This format cannot represent read-only types.

IEC 61131 AddressesTypeLogical Addresses
%M0-%M65535Coil0-65535
%MW0-%MW65535Holding register0-65535

Modbus 1.1b3 Standard Addressing

The latest Modbus standard uses addresses starting at 1 (logical address plus 1). The object type must be stated separately.

ED-549 Data Tables

FunctionModbus TypeFunction CodesLogical Address984 AddressIEC 61131
Analogue inputs (integer)Holding register30x0000-740001-8%MW0-7
Analogue inputs (integer)Input register40x0000-730001-8N/A
Analogue inputs (float)Holding register30x0020-740033-40%MF32-39
Analogue inputs (float)Input register40x0020-730033-40N/A
Input error flagsDiscrete input20x0400-711025-1032N/A
Input error flagsInput register40x040031025N/A
Input error flagsHolding register30x040041025%MW1024
Input channel enableHolding register3, 6, 160x004040065%MW64
Input channel enableCoil1, 5, 150x0040-700065-72%M64-71
Input type/rangeHolding register3, 6, 160x0060-740097-104%MW96-103
Integer formatHolding register3, 6, 160x008040129%MW128
Integer formatCoil1, 5, 150x008000129%M128

ED-549 Integer Value Encoding

Integer values are encoded in one of two formats:

Hexadecimal format (integer format register = 0, coil OFF):

  • Input range scaled to fill 16-bit integer range
  • 2's complement for bipolar ranges
  • Unsigned for unipolar ranges

Engineering units format (integer format register = 1, coil ON):

  • Measurement in mA, mV or V scaled by power of 10
  • Scaled as high as possible while fitting in 16-bit register

ED-549 Floating-Point Values

  • Encoded as IEEE 754 32-bit floating-point
  • Uses two sequential 16-bit registers
  • Least-significant bits in lower register address (little-endian)
  • Both registers must be read in the same Modbus request
  • Units are Volts or milliAmps

ED-549 Range Codes and Data Ranges

Type CodeInput TypeFloat +FSFloat -FSInt Hex +FSInt Hex -FSInt Eng +FSInt Eng -FS
05+/-2.5V+2.5-2.532767-3276825000-25000
06/0D+/-20mA+20.0-20.032767-3276820000-20000
07+4 to +20mA+20.0+4.0655350200004000
08+/-10V+10.0-10.032767-3276810000-10000
09+/-5V+5.0-5.032767-327685000-5000
04/0A+/-1V+1.0-1.032767-3276810000-10000
03/0B+/-500mV+500.0-500.032767-327685000-5000
0C+/-150mV+150.0-150.032767-3276815000-15000
1A0 to +20mA+20.0+0.0655350200000
3A+/-75mV+75.0-75.032767-327687500-7500
3B+/-250mV+250.0-250.032767-3276825000-25000
note

Under-range inputs result in a reading equal to the -Full Scale value. Over-range inputs result in a reading equal to the +Full Scale value.

ED-560 Data Tables

FunctionModbus TypeFunction CodesLogical Address984 AddressIEC 61131
Analogue outputs (integer)Holding register3, 6, 160x0000-340001-4%MW0-3
Analogue outputs (float)Holding register3, 160x0020-740033-40%MF32-39
Analogue type/rangeHolding register3, 6, 160x0060-340097-100%MW96-99
Integer formatHolding register3, 6, 160x008040129%MW128
Integer formatCoil1, 5, 150x008000129%M128

ED-560 Integer Value Encoding

Hexadecimal format (integer format register = 0, coil OFF):

  • Output range scaled to 0-16383 (0x3FFF)

Engineering units format (integer format register = 1, coil ON):

  • Output in mA or V scaled by power of 10

ED-560 Floating-Point Values

  • Same encoding as ED-549 (IEEE 754 32-bit, little-endian)
  • Both registers must be written/read in same request
  • Units are Volts or milliAmps

ED-560 Range Codes and Data Ranges

Type CodeOutput TypeFloat +FSFloat MinInt Hex +FSInt Hex MinInt Eng +FSInt Eng Min
300 to +20mA20.00.0163830200000
31+4 to +20mA20.04.0163830200004000
320 to +10V10.00.0163830100000
note

Writing values below the minimum results in the minimum output. Writing values above the maximum results in the maximum output.