Modbus TCP
Modbus TCP
It is used to create a multipoint network where a single client device may communicate with
multiple server devices over a physical Ethernet layer. With Modbus TCP, a message is
wrapped in a TCP packet, which is then wrapped in an IP packet, which uses Ethernet electrical
signaling to transmit the package.
Modbus
20 languages
Article
Talk
Read
Edit
View history
Tools
From Wikipedia, the free encyclopedia
Not to be confused with M-Bus.
Modbus or MODBUS is a client/server data communications protocol in the application
layer.[1] It was originally published by Modicon (now Schneider Electric) in 1979 for use
with its programmable logic controllers (PLCs).[2] Modbus has become a de
facto standard communication protocol for communication between
industrial electronic devices in a wide range of buses and network.[3][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.[1] 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.
The development and update of Modbus protocols have been managed by the Modbus
Organization[4] since April 2004, when Schneider Electric transferred rights to that
organization.[5] The Modbus Organization is an association of users and suppliers of
Modbus-compliant devices that advocates for the continued use of the technology.
[6]
Modbus Organization, Inc. is a trade association for the promotion and development of
the Modbus protocol.[4]
Protocol description[edit]
MODBUS communication stack
Modbus standards or buses include:[1]
Step 1: Client initiates a request with PDU = Function code + data request
Step 2: Server receives the request from client. Server will then read/parse the
function code, get the address of the data field of the PDU, then get this data field
value and finally perform the action based on the function code. If there is no error
during those steps, server will response PDU = Function code + data response. As
there is no error during those steps, the server responded function code will also be
the function code sent from the client. If there is any error during those steps, server
will response PDU = Exception Function code + Exception code (Reference to PDU
mb_excep_rsp_pdu defined below).
Step 3: Client receives the response and ends the transaction.
Based on that, Modbus defines 3 PDU types:[9]
16 bits (0–
Input register Read-only Read measurements and statuses
65,535)
16 bits (0–
Holding register Read-write Read/Write configuration values
65,535)
Function code[edit]
Modbus defines three types of function codes: Public, User-Defined and Reserved. [14]
Public function codes[edit]
Function
Function type Function name Comment
code
Read Coils 1
Bit
access
Internal Bits or Physical
Write Single Coil 5
Coils
Read/Write Multiple
23
Registers
Diagnostics
Get Com Event Log 12 serial only
Read Device
43
Identification
Encapsulated Interface
Other 43
Transport
Note: Some sources use terminology that differs from the standard; for example Force
Single Coil instead of Write Single Coil.[15]
Function code 01 (read coils) as an example of public function code [edit]
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 1st coil (the very first
coil) has address 0x0. mb_rsp_pdu (response PDU) - if reading 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.
[16]
Specifically, mb_rsp_pdu and mb_rsp_pdu of function code 01 is:[16]
mb_req_pdu:
If server receives the request and execute successfully, server will return a normal
response.
If server cannot receive the request as having communication channel error, server
will not response anything to the client. Client will then have the timeout request
error.
If server receives the request and detect an error on the communication
channel (e.g parity, LRC, CRC), server will not response anything to the client.
Client will then have the timeout request error.
If server receives the request and is unable to execute it (e.g client requests to read
a non-existent register), server will return an exception response to client to
indicate the nature of the error.
Exception response message includes two other fields when compared to a normal
response message:[18]
Function Code: Function code's MSB bit of Exception is 1. This will make this
function code 0x80 higher than then request message function code.
Data: Server returns the exception code inside the Data field. This field defines the
nature of the error.
All Modbus exception code:[19]
Cod
Text Details
e
1 Illegal Function Function code received in the query is not recognized or allowed by server
Data address of some or all the required entities are not allowed or do not
2 Illegal Data Address
exist in server
Server Device Unrecoverable error occurred while server was attempting to perform
4
Failure requested action
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
5 Acknowledge
in the client. client can next issue a Poll Program Complete message to
determine whether processing is completed
Negative Server cannot perform the programming functions; client should request
7
Acknowledge diagnostic or error information from server
Memory Parity
8 Server detected a parity error in memory; client can retry the request
Error
Gateway Path
10 Specialized for Modbus gateways: indicates a misconfigured gateway
Unavailable
Gateway Target
11 Device Failed to Specialized for Modbus gateways: sent when server fails to respond
Respond
1 start bit
8 bit data/message, LSB bit is first sent
1 bit parity
1 stop bit
A Modbus RTU frame then will be:[26]
Slave address Function Code Data CRC
0 - 252
1 byte 1 byte 2 bytes: 1 CRC Low byte and 1 CRC High byte
byte
Data n×2 Data + length will be filled depending on the message type
End 2 Carriage return + line feed (CR/LF) pair (ASCII values 0D16 and 0A16)
Address, Function, Data, and LRC are ASCII hexadecimal encoded values, whereby 8-
bit values (0–255) are encoded as two human-readable ASCII characters from the
ranges 0–9 and A–F. For example, a value of 122 (7A16) is encoded as two ASCII
characters, "7" and "A", and transmitted as two bytes, 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 Messaging on TCP/IP[edit]
Modbus TCP[edit]
Modbus TCP or Modbus TCP/IP is a Modbus variant used for communications
over TCP/IP networks, connecting over port 502.[29] 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'.[30]
The ADU for Modbus TCP is officially called MODBUS TCP/IP ADU (or Modbus TCP/IP
ADU) by the Modbus organization[31] and is also called Modbus TCP frame by other
parties.[3]
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:[32]
Length
Name Function
(bytes)
Transaction
2 For synchronization between messages of server and client
identifier
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:[32][31]
Unit
Transaction identifier Protocol identifier Length Function code Data
identifier
Number and address coincide: entity #x has address x in the data frame.
Consequently, entity number does not include the entity type. For example, holding
register #40010 in Modbus will be holding register #9, at address 9 in JBUS.
Number 0 (and thus address 0) is not supported. The server should not implement
any real data at this number and address, and it can return a null value or throw an
error when requested.
Limitations[edit]
Since Modbus was designed in the late 1970s to communicate to programmable
logic controllers, the number of data types is limited to those understood by PLCs at
the time. Large binary objects are not supported.
No standard way exists for a node to find the description of a data object, for
example, to learn that a register value represents a temperature between 30 and
175 degrees.
Since Modbus is a client/server (formerly master/slave) protocol,[22] there is no way
for a field device to get data by the event handler mechanism (except over Ethernet
TCP/IP, called open-mbus) as the client node must routinely poll each field device
and look for changes in the data. This consumes bandwidth and network time in
applications where bandwidth may be expensive, such as over a low-bit-rate radio
link.
Modbus is restricted to addressing 247 devices on one data link, which limits the
number of field devices that may be connected to a parent station (again, Ethernet
TCP/IP is an exception).
Modbus protocol itself provides no security against unauthorized commands or
interception of data.[37]