Date: | 1979 |
Modbus or MODBUS is a client/server data communications protocol in the application layer. It was originally designed for use with its programmable logic controllers (PLCs), but has become a de facto standard communication protocol for communication between industrial electronic devices in a wide range of buses and networks.[1]
Modbus is popular in industrial environments because it is openly published and royalty-free. It was developed for industrial applications, is relatively easy to deploy and maintain compared to other standards, and places few restrictions on the format of the data to be transmitted.
The Modbus protocol uses serial communication lines, Ethernet, or the Internet protocol suite as a transport layer. Modbus supports communication to and from multiple devices connected to the same cable or Ethernet network. For example, there can be a device that measures temperature and another device to measure humidity connected to the same cable, both communicating measurements to the same computer, via Modbus.
Modbus is often used to connect a plant/system supervisory computer with a remote terminal unit (RTU) in supervisory control and data acquisition (SCADA) systems. Many of the data types are named from industrial control of factory devices, such as ladder logic because of its use in driving relays: a single-bit physical output is called a coil, and a single-bit physical input is called a discrete input or a contact.
It was originally published by Modicon in 1979. The company was acquired by Schneider Electric in 1997. In 2004, they transferred the rights to the Modbus Organization[2] which is trade association of users and suppliers of Modbus-compliant devices that advocates for the continued use of the technology.[3]
Modbus standards or buses include:
To support Modbus communication on a network, many modems and gateways incorporate proprietary designs (refer to the diagram: Architecture of a network for Modbus communication). Implementations may deploy either wireline or wireless communication, such as in the ISM radio band, and even Short Message Service (SMS) or General Packet Radio Service (GPRS).
Modbus defines client which is an entity which initiates a transaction to request any specific task from its "request receiver". The client's "request receiver", which the client has initiated the transaction with, is then called server. For example, when a Microcontroller unit (MCU) connects to a sensor to read its data by Modbus on a wired network, e.g RS485 bus, the MCU in this context is the client and the sensor is the server. In former terminology, the client was named master and the server named slave.
Modbus defines a protocol data unit (PDU) independently to its lower layer protocols in its protocol stack. The mapping of MODBUS protocol on specific buses or network requires some additional fields, which are defined as application data unit (ADU). ADU is formed by a client inside a Modbus network when the client initiates a transaction. Contents are:
ADU is officially called a Modbus frame by the Modbus Organization, although frame is used as the data unit in the data-link layer in the OSI and TCP/IP model (while Modbus is an application layer protocol).
PDU max size is 253 bytes. ADU max size on RS232/RS485 network is 256 bytes, and with TCP is 260 bytes.
For data encoding, Modbus uses a big-endian representation for addresses and data fields. Thus, for a 16-bit value, the most significant byte is sent first. For example, when a 16-bit register has value 0x1234, byte 0x12 is sent before byte 0x34.
Function code is 1 byte which gives the code of the function to execute. Function codes are integer values, ranging from 1 to 255, and the range from 128 to 255 is for exception responses.
The data field of the PDU has the address from 0 to 65535 (not to be confused with the address of the Additional address field of ADU). The data field of the PDU can be empty, and then has a size of 0. In this case, the server will not request any information and the function code defines the function to be executed. If there is no error during the execution process, the data field of the ADU response from server to client will include the data requested, i.e. the data the client previously received. If there is any error, the server will respond with an exception code.
A Modbus transaction between client and server includes:
Based on that, Modbus defines 3 PDU types:
mb_req_pdu = Function code (1 byte) + request data (n bytes)
request data field's size depends on the function code and usually includes values like variable values, data offset, and sub-function codes.
mb_rsp_pdu = Function code (1 byte) + response data (n bytes)
As in mb_req_pdu, response data field's size depends on the function code and usually includes values like variable values, data offset, and sub-function codes.
mb_excep_rsp_pdu = Exception Function code (1 byte) + exception code (1 byte)
Exception Function code = Function code (1 byte) + 0x80
Exception Function code is equal to the Function code, except that its MSB is set to 1.
Exception code (1 byte) of mb_excep_rsp_pdu is defined in the "MODBUS Exception Codes" table.
Modbus defines its data model based on a series of table with four primary tables:
Primary tables | Access | Size | Features | |
---|---|---|---|---|
Coil (discrete output)[4] | Read-write | 1 bit | Read/Write on/off value | |
Discrete input | Read-only | 1 bit | Read on/off value | |
Input register | Read-only | 16 bits (0–65,535) | Read measurements and statuses | |
Holding register | Read-write | 16 bits (0–65,535) | Read/Write configuration values |
Modbus defines three types of function codes: Public, User-Defined and Reserved.
Function type | Function name | Function code | Comment | |||
---|---|---|---|---|---|---|
Data Access | Bit access | Physical Discrete Inputs | Read Discrete Inputs | 2 | ||
Internal Bits or Physical Coils | Read Coils | 1 | ||||
Write Single Coil | 5 | |||||
Write Multiple Coils | 15 | |||||
16-bit access | Physical Input Registers | Read Input Registers | 4 | |||
Internal Registers or Physical Output Registers | Read Multiple Holding Registers | 3 | ||||
Write Single Holding Register | 6 | |||||
Write Multiple Holding Registers | 16 | |||||
Read/Write Multiple Registers | 23 | |||||
Mask Write Register | 22 | |||||
Read FIFO Queue | 24 | |||||
File Record Access | Read File Record | 20 | ||||
Write File Record | 21 | |||||
Diagnostics | Read Exception Status | 7 | serial only | |||
Diagnostic | 8 | serial only | ||||
Get Com Event Counter | 11 | serial only | ||||
Get Com Event Log | 12 | serial only | ||||
Report Server ID | 17 | serial only | ||||
Read Device Identification | 43 | |||||
Other | Encapsulated Interface Transport | 43 |
Note: Some sources use terminology that differs from the standard; for example Force Single Coil instead of Write Single Coil.[5]
Function code 01 (read coils) allow reading the state from 1 to 2000 coil of a remote device. mb_req_pdu (request PDU) will then have 2 bytes to indicate the address of the first coil to read (from 0x0000 to 0xFFFF), and 2 bytes to indicate the number of coils to read. mb_req_pdu defines coil address by index 0, i.e the first coil has address 0x0. mb_rsp_pdu (response PDU) – if executing successfully – has 1 byte to indicate the number of bytes which is the number of coils that mb_req_pdu has required, and the left bytes store the status (on/off value) of those requested coils. Specifically, mb_rsp_pdu and mb_rsp_pdu of function code 01 is:
mb_req_pdu:
mb_rsp_pdu
For instance, mb_req_pdu and mb_rsp_pdu to read coils status from 20-38 will be:
mb_req_pdu:
Starting Address (2 bytes) is 0x0013, (or 19 in decimal) which is the 20th coil.
Quantity of Outputs (2 bytes) is 0x0013, (or 19 in decimal) which corresponds to 19 values of status of coils 20th to 38th.
mb_rsp_pdu:
As 19 coils (20-38) are required, 3 bytes is used to indicate the coil's state. So that Byte Count is 0x03. States of coil from 20 to 27 is 0xCD, which is 1100 1101 in binary. So coil 27 is MSb, and coil 20 is LSb. Same for coil 28 to 35. With coil from 36 to 38, the state will be 0x05, which is 0000 0101. Coil 38 state is the 3rd bit (count from the right), i.e 1, coil 37 is 0, and coil 36 state is LSb bit, i.e. 1. 5 left bits are all 0.
User-Defined Function Codes are function codes defined by users. Modbus gives two range of values for user-defined function codes: 65 to 72 and 100 to 110. Obviously, user-defined function codes are not unique.
Reserved Function Codes are function codes used by some companies for legacy product and are not available for public use.
When a client sends a request to a server, there can be four possible events for that request:
Exception response message includes two other fields when compared to a normal response message:
All Modbus exception code:
Code | Text | Details | |
---|---|---|---|
1 | Illegal Function | Function code received in the query is not recognized or allowed by server | |
2 | Illegal Data Address | Data address of some or all the required entities are not allowed or do not exist in server | |
3 | Illegal Data Value | Value is not accepted by server | |
4 | Server Device Failure | Unrecoverable error occurred while server was attempting to perform requested action | |
5 | Acknowledge | Server has accepted request and is processing it, but a long duration of time is required. This response is returned to prevent a timeout error from occurring in the client. client can next issue a Poll Program Complete message to determine whether processing is completed | |
6 | Server Device Busy | Server is engaged in processing a long-duration command; client should retry later | |
7 | Negative Acknowledge | Server cannot perform the programming functions; client should request diagnostic or error information from server | |
8 | Memory Parity Error | Server detected a parity error in memory; client can retry the request | |
10 | Gateway Path Unavailable | Specialized for Modbus gateways: indicates a misconfigured gateway | |
11 | Gateway Target Device Failed to Respond | Specialized for Modbus gateways: sent when server fails to respond |
Modbus standard also defines Modbus over Serial Line, a protocol over the data link layer of the OSI model for the Modbus application layer protocol to be communicated over a serial bus. Modbus Serial Line protocol is a master-slave protocol which supports one master and multiple slaves in the serial bus. With Modbus protocol on the application layer, client/server model is used for the devices on the communication channel. With Modbus over Serial Line, client role is implemented by master, and the server role is implemented by slave.
The organization's naming convention inverts the common usage of having multiple clients and only one server. To avoid this confusion, the RS-485 transport layer uses the terms "node" or "device" instead of "server", and the "client" is not a "node".
A serial bus for Modbus over Serial Line can have a maximum of 247 slaves communicating with 1 master. Those slaves have a unique address ranging from 1 to 247 (decimal value). The master doesn't need to have an address. The communication process is initiated by the master, as only it can initiate a Modbus transaction. A slave will never transmit any data or perform any action without a request from the master, and slaves cannot communicate with each other.
In Modbus over Serial Line, the master initiates requests to the slaves in unicast or broadcast modes. In unicast mode, the master will initiate a request to a single slave with a specific address. Upon receiving and finishing the request, the slave will respond with a message to the master. In this mode, a Modbus transaction includes two messages: one request from the master and one reply from the slave. Each slave must have a unique address (from 1 to 247) to be addressed independently for the communication. In broadcast mode, the master can send a request to all the slaves, using the broadcast address 0, which is the address reserved for broadcast exchanges (and not the master address). Slaves must accept broadcast exchanges but must not respond.The mapping of PDU of Modbus to the serial bus of Modbus over Serial Line protocol results in Modbus Serial Line PDU.
Modbus Serial Line PDU = Address + PDU + CRC (or LRC)
With PDU = Function code + data
On the Physical layer, MODBUS over Serial Line performs its communication on bit by RS485 or RS232, with TIA/EIA-485 Two-Wire interface as the most popular way. RS485 Four-Wire interface is also used. TIA/EIA-232-E (RS232) can also be used but is limited to point-to-point short-range communication. MODBUS over Serial Line has two transmission modes RTU and ASCII which are corresponded to two versions of the protocol, known as Modbus RTU and Modbus ASCII.
Modbus RTU (Remote Terminal Unit), which is the most common implementation available for Modbus, makes use of a compact, binary representation of the data for protocol communication. The RTU format follows the commands/data with a cyclic redundancy check checksum as an error check mechanism to ensure the reliability of data. A Modbus RTU message must be transmitted continuously without inter-character hesitations. Modbus messages are framed (separated) by idle (silent) periods. Each byte (8 bits) of data is sent as 11 bits:
A Modbus RTU frame then will be:
The CRC calculation is widely known as CRC-16-MODBUS, whose polynomial is x16 + x15 + x2 + 1 (normal hexadecimal algebraic polynomial being8005
and reversed A001
).Example of a Modbus RTU frame in hexadecimal: 01 04 02 FF FF B8 80
(CRC-16-MODBUS calculation for the 5 bytes from 01
to FF
gives 80B8
, which is transmitted least significant byte first).
To ensure frame integrity during the transmission, the time interval between two frames must be at least the transmission time of 3.5 characters, and the time interval between two consecutive characters must be no more than the transmission time of 1.5 characters. For example, with the default data rate of 19200 bit/s, the transmission times of 3.5 (t3.5) and 1.5 (t1.5) 11-bit characters are:
t3.5=3.5*\left(
11*1000 | |
19200 |
\right)=2.005ms
t1.5=1.5*\left(
11*106 | |
19200 |
\right)=859.375\mus
For higher data rates, Modbus RTU recommends to use the fixed values 750 μs for t1.5 and 1.750 ms for t3.5.
Modbus ASCII makes use of ASCII characters for protocol communication. The ASCII format uses a longitudinal redundancy check checksum. Modbus ASCII messages are framed by a leading colon (":") and trailing newline (CR/LF).
A Modbus ASCII frame includes:
Name | Length (bytes) | Function | |
---|---|---|---|
Start | 1 | Colon : (ASCII value 3A16) | |
Address | 2 | Station address | |
Function | 2 | Indicates the function code e.g. "read coils" | |
Data | n × 2 | Data + length will be filled depending on the message type | |
LRC | 2 | Checksum (longitudinal redundancy check) | |
End | 2 | Carriage return + line feed (CR/LF) pair (ASCII values 0D16 and 0A16) |
55
(3716, ASCII value for "7") and 65
(4116, ASCII value for "A").LRC is calculated as the sum of 8-bit values (excluding the start and end characters), negated (two's complement) and encoded as an 8-bit value. For example, if Address, Function, and Data are 247, 3, 19, 137, 0, and 10, the two's complement of their sum (416) is −416; this trimmed to 8 bits is 96 (256 × 2 − 416 = 6016), giving the following 17 ASCII character frame: :F7031389000A60␍␊
. LRC is specified for use only as a checksum: because it is calculated on the encoded data rather than the transmitted characters, its 'longitudinal' characteristic is not available for use with parity bits to locate single-bit errors.
Modbus TCP or Modbus TCP/IP is a Modbus variant used for communications over TCP/IP networks, connecting over port 502. It does not require a checksum calculation, as lower layers already provide checksum protection.
Modbus TCP nomenclature is the same as for the Modbus over Serial line protocol, as any device which send out a Modbus command, is the 'client' and the response comes from a 'server'.[6]
The ADU for Modbus TCP is officially called MODBUS TCP/IP ADU (or Modbus TCP/IP ADU) by the Modbus organization and is also called Modbus TCP frame by other parties.
MODBUS TCP/IP ADU = MBAP Header + Function code + Data
Where MBAP - which stands for MODBUS Application Protocol header - is the dedicated header used on TCP/IP to identify the MODBUS Application Data Unit.
The MBAP Header contains the following fields:
Name | Length (bytes) | Function | |
---|---|---|---|
Transaction identifier | 2 | For synchronization between messages of server and client | |
Protocol identifier | 2 | 0 for Modbus/TCP | |
Length field | 2 | Number of remaining bytes in this frame | |
Unit identifier | 1 | Server address (255 if not used), treated like slave address in Modbus over Serial line |
Unit identifier is used with Modbus TCP devices that are composites of several Modbus devices, e.g. Modbus TCP to Modbus RTU gateways. In such a case, the unit identifier is the Server Address of the device behind the gateway.
A MODBUS TCP/IP ADU/Modbus TCP frame format then will be:
Example of a Modbus TCP/IP ADU/Modbus TCP frame in hexadecimal:12 34 00 00 00 06 01 03 00 01 00 01
0x12
and 0x34
: With transaction ID = 0x1234 (2 bytes) as a "unique number" to be identified between the Modbus TCP client/server, the transaction ID High byte is 0x12 and transaction ID Low byte is 0x340x00
and 0x00
: Protocol identifier high byte and low byte0x00
and 0x06
: Length high byte and low byte. The length is 6 bytes which includes: unit identifier (slave address) (1 byte), function code (1 byte), high byte of the register address to read (1 byte), low byte of the register address to read (1 byte) and data (2 bytes = high byte and low byte of the number of registers to read)0x01
: Unit identifier (slave address)0x03
: Function code (Read Multiple Holding Registers)0x00
and 0x01
: high byte and low byte of the register address to read. The register address to read in this case is 0x0001
.0x00
and 0x01
: high byte and low byte of the number of registers to read. The number of registers to read in this case is 0x0001
. (i.e 1 register)Besides the widely used Modbus RTU, Modbus ASCII and Modbus TCP, there are many variants of Modbus protocols:
Data models and function calls are identical for the first four variants listed above; only the encapsulation is different. However the variants are not interoperable, nor are the frame formats.
Another de facto protocol closely related to Modbus appeared later, and was defined by PLC maker April Automates, the result of a collaborative effort between French companies Renault Automation and Merlin Gerin et Cie in 1985: JBUS. Differences between Modbus and JBUS at that time (number of entities, server stations) are now irrelevant as this protocol almost disappeared with the April PLC series, which AEG Schneider Automation bought in 1994 and then made obsolete. However, the name JBUS has survived to some extent.
JBUS supports function codes 1, 2, 3, 4, 5, 6, 15, and 16 and thus all the entities described above, although numbering is different:
Specifications
Other