Modbus Application Protocol Specification V1.1b3
Modbus Application Protocol Specification V1.1b3
Modbus Application Protocol Specification V1.1b3
V1.1b3
CONTENTS
1
Introduction ................................................................................................................. 2
Context ....................................................................................................................... 3
General description...................................................................................................... 3
http://www.modbus.org
1/50
Modbus
Introduction
1.1
MODBUS is an application layer messaging protocol, positioned at level 7 of the OSI model,
which provides client/server communication between devices connected on different types of
buses or networks.
The industrys serial de facto standard since 1979, MODBUS continues to enable millions of
automation devices to communicate. Today, support for the simple and elegant structure of
MODBUS continues to grow. The Internet community can access MODBUS at a reserved
system port 502 on the TCP/IP stack.
MODBUS is a request/reply protocol and offers services specified by function codes.
MODBUS function codes are elements of MODBUS request/reply PDUs. The objective of this
document is to describe the function codes used within the framework of MODBUS
transactions.
MODBUS is an application layer messaging protocol for client/server communication between
devices connected on different types of buses or networks.
It is currently implemented using:
TCP/IP over Ethernet. See MODBUS Messaging Implementation Guide V1.0a.
Asynchronous serial transmission over a variety of media (wire : EIA/TIA-232-E, EIA-422,
EIA/TIA-485-A; fiber, radio, etc.)
Modbus on TCP
TCP
IP
Other
MODBUS+ / HDLC
Master / Slave
Ethernet II /802.3
Other
Physical layer
EIA/TIA-232 or
EIA/TIA-485
Ethernet
Physical layer
Figure 1:
References
1. RFC 791, Internet Protocol, Sep81 DARPA
Abbreviations
ADU
HDLC
HMI
IETF
I/O
IP
MAC
MB
http://www.modbus.org
2/50
Modbus
Context
The MODBUS protocol allows an easy communication within all types of network
architectures.
MODBUS COMMUNICATION
Drive
PLC
HMI
I/ O
I/ O
PLC
I/ O
MODBUS ON TCP/IP
Gateway
Gateway
MODBUS ON MB+
PLC
HMI
Device
MODBUS ON RS485
MODBUS ON RS232
Gateway
PLC
I/ O
I/ O
Drive
I/ O
Device
I/ O
Figure 2:
Every type of devices (PLC, HMI, Control Panel, Driver, Motion control, I/O Device) can use
MODBUS protocol to initiate a remote operation.
The same communication can be done as well on serial line as on an Ethernet TCP/IP
networks. Gateways allow a communication between several types of buses or network using
the MODBUS protocol.
General description
4.1
Protocol description
The MODBUS protocol defines a simple protocol data unit (PDU) independent of the
underlying communication layers. The mapping of MODBUS protocol on specific buses or
network can introduce some additional fields on the application data unit (ADU).
ADU
Additional address
Function code
Data
Error check
PDU
April 26, 2012
Figure 3:
General MODBUS frame
http://www.modbus.org
3/50
Modbus
The MODBUS application data unit is built by the client that initiates a MODBUS transaction.
The function indicates to the server what kind of action to perform. The MODBUS application
protocol establishes the format of a request initiated by a client.
The function code field of a MODBUS data unit is coded in one byte. Valid codes are in the
range of 1 ... 255 decimal (the range 128 255 is reserved and used for exception
responses). When a message is sent from a Client to a Server device the function code field
tells the server what kind of action to perform. Function code "0" is not valid.
Sub-function codes are added to some function codes to define multiple actions.
The data field of messages sent from a client to server devices contains additional information
that the server uses to take the action defined by the function code. This can include items
like discrete and register addresses, the quantity of items to be handled, and the count of
actual data bytes in the field.
The data field may be nonexistent (of zero length) in certain kinds of requests, in this case the
server does not require any additional information. The function code alone specifies the
action.
If no error occurs related to the MODBUS function requested in a properly received MODBUS
ADU the data field of a response from a server to a client contains the data requested. If an
error related to the MODBUS function requested occurs, the field contains an exception code
that the server application can use to determine the next action to be taken.
For example a client can read the ON / OFF states of a group of discrete outputs or inputs or
it can read/write the data contents of a group of registers.
When the server responds to the client, it uses the function code field to indicate either a
normal (error-free) response or that some kind of error occurred (called an exception
response). For a normal response, the server simply echoes to the request the original
function code.
Client
Server
Initiate request
Function code
Data Request
Data Response
Figure 4:
For an exception response, the server returns a code that is equivalent to the original function
code from the request PDU with its most significant bit set to logic 1.
Client
Server
Initiate request
Function code
Data Request
Figure 5:
April 26, 2012
Exception code
4/50
Modbus
Note: It is desirable to manage a time o ut in order no t to indefinitely wait for an answer which will perhaps
never arrive.
The size of the MODBUS PDU is limited by the size constraint inherited from the first
MODBUS implementation on Serial Line network (max. RS485 ADU = 256 bytes).
Therefore:
MODBUS PDU for serial line communication = 256 - Server address (1 byte) - CRC (2
bytes) = 253 bytes.
Consequently:
RS232 / RS485 ADU = 253 bytes + Server address (1 byte) + CRC (2 bytes) = 256 bytes.
TCP MODBUS ADU = 253 bytes + MBAP (7 bytes) = 260 bytes.
The MODBUS protocol defines three PDUs. They are :
Data Encoding
MODBUS uses a big-Endian representation for addresses and data items. This means
that when a numerical quantity larger than a single byte is transmitted, the most significant
byte is sent first. So for example
Register size
16 - bits
value
0x1234
0x12
then 0x34
http://www.modbus.org
5/50
4.3
Modbus
MODBUS bases its data model on a series of tables that have distinguishing characteristics.
The four primary tables are:
Primary tables
Object type
Type of
Discretes Input
Single bit
Read-Only
Coils
Single bit
Read-W rite
Input Registers
16-bit word
Read-Only
Holding Registers
16-bit word
Read-W rite
Comments
This type of data can be provided by an I/O system.
This type of data can be alterable by an applicatio n
program.
This type of data can be provided by an I/O system
This type of data can be alterable by an applicatio n
program.
The distinctions between inputs and outputs, and between bit-addressable and wordaddressable data items, do not imply any application behavior. It is perfectly acceptable, and
very common, to regard all four tables as overlaying one another, if this is the most natural
interpretation on the target machine in question.
For each of the primary tables, the protocol allows individual selection of 65536 data items,
and the operations of read or write of those items are designed to span multiple consecutive
data items up to a data size limit which is dependent on the transaction function code.
Its obvious that all the data handled via MODBUS (bits, registers) must be located in device
application memory. But physical address in memory should not be confused with data
reference. The only requirement is to link data reference with physical address.
MODBUS logical reference numbers, which are used in MODBUS functions, are unsigned
integer indices starting at zero.
Implementation examples of MODBUS model
The examples below show two ways of organizing the data in device. There are different
organizations possible, but not all are described in this document. Each device can have its
own organization of the data according to its application
Example 1 : Device having 4 separate blocks
The example below shows data organization in a device having digital and analog, inputs and
outputs. Each block is separate because data from different blocks have no correlation. Each
block is thus accessible with different MODBUS functions.
Device application memory
MODBUS access
Input Discrete
Coils
MODBUS Request
Input Registers
Holding
Registers
Figure 6
April 26, 2012
6/50
Modbus
MODBUS access
Input Discrete
R
W
Coils
R
MODBUS Request
Input Registers
Holding
Registers
Figure 7
4.4
http://www.modbus.org
7/50
Device application
Modbus
Read input 0
1
Discrete Input
Coils
.
.
.
1
.
5
Read coils 4
1
Input Registers 2
.
Read Registers 1
1
.
Holding Registers
.
55
Read Registers 54
Mapping
Application specific
MODBUS Standard
Figure 8
The previous figure shows that a MODBUS data numbered X is addressed in the MODBUS
PDU X-1.
4.5
The following state diagram describes the generic processing of a MODBUS transaction in
server side.
http://www.modbus.org
8/50
Modbus
V
Wait for a MB
indication
[Receive MB indication]
Validate function
code
ExeptionCode = 1
[invalid]
[valid]
Validate data
Address
ExceptionCode = 2
[invalid]
[valid]
Validate data
value
ExceptionCode = 3
[invalid]
[valid]
Execute MB
function
ExceptionCode = 4, 5, 6
[invalid]
[valid]
Send Modbus
Exception
Response
Figure 9
Send Modbus
Response
Once the request has been processed by a server, a MODBUS response using the
adequate MODBUS server transaction is built.
Depending on the result of the processing two types of response are built :
A positive MODBUS response :
the response function code = the request function code
http://www.modbus.org
9/50
Modbus
Function Codes
Are well defined function codes ,
guaranteed to be unique,
validated by the MODBUS.org community,
publicly documented
have available conformance test,
includes both defined public assigned function codes as well as unassigned function
codes reserved for future use.
User-Defined Function Codes
there are two ranges of user-defined function codes, i.e. 65 to 72 and from 100 to 110
decimal.
user can select and implement a function code that is not supported by the
specification.
there is no guarantee that the use of the selected function code will be unique
if the user wants to re-position the functionality as a public function code, he must
initiate an RFC to introduce the change into the public category and to have a new
public function code assigned.
MODBUS Organization, Inc expressly reserves the right to develop the proposed RFC.
Reserved Function Codes
Function Codes currently used by some companies for legacy products and that
are not available for public use.
Informative Note: The reader is asked refer to Annex A (Informative) MODBUS
RESERVED FUNCTION CODES, SUBCODES AND MEI TYPES.
127
72
65
1
Figure 10
http://www.modbus.org
10/50
Physical Discrete
Inputs
Bit
access
Internal Bits
Or
Physical coils
Physical Input
Registers
Data
Access
16 bits
access
Internal Registers
Or
Physical Output
Registers
Diagnostics
Other
6
6.1
Modbus
Function Codes
code
Sub
(hex) Section
code
02
02
6.2
Read Coils
Write Single Coil
Write Multiple Coils
01
05
15
01
05
0F
6.1
6.5
6.11
04
04
6.4
03
06
16
23
22
24
20
21
07
08
11
12
17
43
43
03
06
10
17
16
18
14
15
07
00-18,20 08
OB
0C
11
14
2B
13,14
2B
6.3
6.6
6.12
6.17
6.16
6.18
6.14
6.15
6.7
6.8
6.9
6.10
6.13
6.21
6.19
43
13
2B
6.20
This function code is used to read from 1 to 2000 contiguous status of coils in a remote
device. The Request PDU specifies the starting address, i.e. the address of the first coil
specified, and the number of coils. In the PDU Coils are addressed starting at zero. Therefore
coils numbered 1-16 are addressed as 0-15.
The coils in the response message are packed as one coil per bit of the data field. Status is
indicated as 1= ON and 0= OFF. The LSB of the first data byte contains the output addressed
in the query. The other coils follow toward the high order end of this byte, and from low order
to high order in subsequent bytes.
If the returned output quantity is not a multiple of eight, the remaining bits in the final data
byte will be padded with zeros (toward the high order end of the byte). The Byte Count field
specifies the quantity of complete bytes of data.
Request
Functio n code
Starting Address
Quantity of coils
1 Byte
2 Bytes
2 Bytes
0x01
0x0000 to 0xFFFF
1 to 2000 (0x7D0)
1 Byte
1 Byte
n Byte
0x01
N*
n = N or N+1
Response
Functio n code
Byte co unt
Coil Status
http://www.modbus.org
11/50
Modbus
1 Byte
1 Byte
Response
Field Name
Functio n
Byte Co unt
Outputs status 27-20
Outputs status 35-28
Outputs status 38-36
(Hex)
01
00
13
00
13
(Hex)
01
03
CD
6B
05
The status of outputs 2720 is shown as the byte value CD hex, or binary 1100 1101. Output
27 is the MSB of this byte, and output 20 is the LSB.
By convention, bits within a byte are shown with the MSB to the left, and the LSB to the right.
Thus the outputs in the first byte are 27 through 20, from left to right. The next byte has
outputs 35 through 28, left to right. As the bits are transmitted serially, they flow from LSB to
MSB: 20 . . . 27, 28 . . . 35, and so on.
In the last data byte, the status of outputs 38-36 is shown as the byte value 05 hex, or binary
0000 0101. Output 38 is in the sixth bit position from the left, and output 36 is the LSB of this
byte. The five remaining high order bits are zero filled.
Note: T he five remaining bits (toward the high order end) are zero filled.
ENTRY
MB Server receives mb_req_pdu
NO
Function code
supported
YES
ExceptionCode = 01
NO
YES
ExceptionCode = 03
NO
Starting Address == OK
AND
Starting Address + Quantity of Outputs == OK
YES
ExceptionCode = 02
Request Processing
NO
ReadDiscreteOutputs
== OK
YES
ExceptionCode = 04
MB Server Sends mb_rsp
Figure 11:
6.2
EXIT
This function code is used to read from 1 to 2000 contiguous status of discrete inputs in a
remote device. The Request PDU specifies the starting address, i.e. the address of the first
April 26, 2012
http://www.modbus.org
12/50
Modbus
input specified, and the number of inputs. In the PDU Discrete Inputs are addressed starting
at zero. Therefore Discrete inputs numbered 1-16 are addressed as 0-15.
The discrete inputs in the response message are packed as one input per bit of the data field.
Status is indicated as 1= ON; 0= OFF. The LSB of the first data byte contains the input
addressed in the query. The other inputs follow toward the high order end of this byte, and
from low order to high order in subsequent bytes.
If the returned input quantity is not a multiple of eight, the remaining bits in the final data byte
will be padded with zeros (toward the high order end of the byte). The Byte Count field
specifies the quantity of complete bytes of data.
Request
Function code
1 Byte
0x02
Starting Address
2 Bytes
0x0000 to 0xFFFF
Quantity of Inputs
2 Bytes
1 to 2000 (0x7D0)
Function code
1 Byte
0x02
Byte count
1 Byte
N*
Input Status
N* x 1 Byte
Response
1 Byte
0x82
Exception code
1 Byte
01 or 02 or 03 or 04
(Hex)
02
00
C4
00
16
Response
Field Name
Functio n
Byte Co unt
Inputs S tatus 204-197
Inputs S tatus 212-205
Inputs S tatus 218-213
(Hex)
02
03
AC
DB
35
The status of discrete inputs 204197 is shown as the byte value AC hex, or binary 1010
1100. Input 204 is the MSB of this byte, and input 197 is the LSB.
The status of discrete inputs 218213 is shown as the byte value 35 hex, or binary 0011 0101.
Input 218 is in the third bit position from the left, and input 213 is the LSB.
Note: T he two remaining bits (to ward the high order end) are zero filled.
http://www.modbus.org
13/50
Modbus
ENTRY
MB Server receives mb_req_pdu
NO
Function code
supported
YES
ExceptionCode = 01
NO
YES
ExceptionCode = 03
NO
Starting Address == OK
AND
Starting Address + Quantity of Inputs == OK
YES
ExceptionCode = 02
Request Processing
NO
ReadDiscreteInputs
== OK
YES
ExceptionCode = 04
Figure 12:
EXIT
http://www.modbus.org
14/50
Modbus
This function code is used to read the contents of a contiguous block of holding registers in a
remote device. The Request PDU specifies the starting register address and the number of
registers. In the PDU Registers are addressed starting at zero. Therefore registers numbered
1-16 are addressed as 0-15.
The register data in the response message are packed as two bytes per register, with the
binary contents right justified within each byte. For each register, the first byte contains the
high order bits and the second contains the low order bits.
Request
Functio n code
Starting Address
Quantity of Registers
1 Byte
2 Bytes
2 Bytes
0x03
0x0000 to 0xFFFF
1 to 125 (0x7D)
1 Byte
1 Byte
N* x 2 B ytes
0x03
2 x N*
1 Byte
1 Byte
0x83
01 or 02 or 03 or 04
Response
Functio n code
Byte co unt
Register value
*N = Quantity of Registers
Error
Error code
Exceptio n code
(Hex)
03
00
6B
00
03
Response
Field Name
Functio n
Byte Co unt
Register value
Register value
Register value
Register value
Register value
Register value
Hi (108)
Lo (108)
Hi (109)
Lo (109)
Hi (110)
Lo (110)
(Hex)
03
06
02
2B
00
00
00
64
The contents of register 108 are shown as the two byte values of 02 2B hex, or 555 decimal.
The contents of registers 109110 are 00 00 and 00 64 hex, or 0 and 100 decimal,
respectively.
http://www.modbus.org
15/50
Modbus
ENTRY
MB Server receives mb_req_pdu
NO
Function code
supported
YES
ExceptionCode = 01
NO
YES
ExceptionCode = 03
NO
Starting Address == OK
AND
Starting Address + Quantity of Registers == OK
YES
ExceptionCode = 02
Request Processing
NO
ReadMultipleRegisters
== OK
YES
ExceptionCode = 04
EXIT
Figure 13:
6.4
This function code is used to read from 1 to 125 contiguous input registers in a remote device.
The Request PDU specifies the starting register address and the number of registers. In the
PDU Registers are addressed starting at zero. Therefore input registers numbered 1-16 are
addressed as 0-15.
The register data in the response message are packed as two bytes per register, with the
binary contents right justified within each byte. For each register, the first byte contains the
high order bits and the second contains the low order bits.
Request
Functio n code
Starting Address
Quantity of Input Registers
1 Byte
2 Bytes
2 Bytes
0x04
0x0000 to 0xFFFF
0x0001 to 0x007D
1 Byte
1 Byte
N* x 2 B ytes
0x04
2 x N*
Response
Functio n code
Byte co unt
Input Registers
1 Byte
1 Byte
0x84
01 or 02 or 03 or 04
(Hex)
04
00
08
00
Response
Field Name
Functio n
Byte Co unt
Input Reg. 9 Hi
Input Reg. 9 Lo
http://www.modbus.org
(Hex)
04
02
00
0A
16/50
Modbus
01
The contents of input register 9 are shown as the two byte values of 00 0A hex, or 10 decimal.
ENTRY
MB Server receives mb_req_pdu
NO
Function code
supported
YES
ExceptionCode = 01
NO
YES
ExceptionCode = 03
NO
Starting Address == OK
AND
Starting Address + Quantity of Registers == OK
YES
ExceptionCode = 02
Request Processing
NO
ReadInputRegisters
== OK
YES
ExceptionCode = 04
EXIT
Figure 14:
6.5
This function code is used to write a single output to either ON or OFF in a remote device.
The requested ON/OFF state is specified by a constant in the request data field. A value of FF
00 hex requests the output to be ON. A value of 00 00 requests it to be OFF. All other values
are illegal and will not affect the output.
The Request PDU specifies the address of the coil to be forced. Coils are addressed starting
at zero. Therefore coil numbered 1 is addressed as 0. The requested ON/OFF state is
specified by a constant in the Coil Value field. A value of 0XFF00 requests the coil to be ON.
A value of 0X0000 requests the coil to be off. All other values are illegal and will not affect the
coil.
The normal response is an echo of the request, returned after the coil state has been written.
Request
Functio n code
Output Address
Output Value
1 Byte
2 Bytes
2 Bytes
0x05
0x0000 to 0xFFFF
0x0000 or 0xFF00
1 Byte
2 Bytes
0x05
0x0000 to 0xFFFF
Response
Functio n code
Output Address
http://www.modbus.org
17/50
2 Bytes
0x0000 or 0xFF00
Error code
Exceptio n code
1 Byte
1 Byte
0x85
01 or 02 or 03 or 04
Modbus
Error
(Hex)
05
00
AC
FF
00
Response
Field Name
Functio n
Output Address Hi
Output Address Lo
Output Value Hi
Output Value Lo
http://www.modbus.org
(Hex)
05
00
AC
FF
00
18/50
Modbus
ENTRY
MB Server receives mb_req_pdu
NO
Function code
supported
YES
ExceptionCode = 01
NO
ExceptionCode = 03
NO
Output Address == OK
YES
ExceptionCode = 02
Request Processing
NO
WriteSingleOutput
== OK
YES
ExceptionCode = 04
MB Server Sends mb_rsp
Figure 15:
6.6
EXIT
This function code is used to write a single holding register in a remote device.
The Request PDU specifies the address of the register to be written. Registers are addressed
starting at zero. Therefore register numbered 1 is addressed as 0.
The normal response is an echo of the request, returned after the register contents have been
written.
Request
Functio n code
Register Address
Register Value
1 Byte
2 Bytes
2 Bytes
0x06
0x0000 to 0xFFFF
0x0000 to 0xFFFF
Functio n code
Register Address
Register Value
1 Byte
2 Bytes
2 Bytes
0x06
0x0000 to 0xFFFF
0x0000 to 0xFFFF
Error code
Exceptio n code
1 Byte
1 Byte
0x86
01 or 02 or 03 or 04
Response
Error
(Hex)
06
00
01
00
03
Response
Field Name
Functio n
Register Address Hi
Register Address Lo
Register Value Hi
Register Value Lo
http://www.modbus.org
(Hex)
06
00
01
00
03
19/50
Modbus
ENTRY
MB Server receives mb_req_pdu
NO
Function code
supported
YES
ExceptionCode = 01
NO
YES
ExceptionCode = 03
NO
Register Address == OK
YES
ExceptionCode = 02
Request Processing
NO
WriteSingleRegister
== OK
YES
ExceptionCode = 04
MB Server Sends mb_rsp
EXIT
Figure 16:
6.7
This function code is used to read the contents of eight Exception Status outputs in a remote
device.
The function provides a simple method for accessing this information, because the Exception
Output references are known (no output reference is needed in the function).
The normal response contains the status of the eight Exception Status outputs. The outputs
are packed into one data byte, with one bit per output. The status of the lowest output
reference is contained in the least significant bit of the byte.
The contents of the eight Exception Status outputs are device specific.
Request
Functio n code
1 Byte
0x07
Functio n code
Output Data
1 Byte
1 Byte
0x07
0x00 to 0xFF
Error code
Exceptio n code
1 Byte
1 Byte
0x87
01 or 04
Response
Error
(Hex)
07
Response
Field Name
Functio n
Output Data
http://www.modbus.org
(Hex)
07
6D
20/50
Modbus
In this example, the output data is 6D hex (0110 1101 binary). Left to right, the outputs are
OFFONONOFFONONOFFON. The status is shown from the highest to the lowest
addressed output.
ENTRY
MB Server receives mb_req_pdu
NO
Function code
supported
YES
ExceptionCode = 01
Request Processing
NO
ReadExceptionStatus == OK
YES
ExceptionCode = 04
MB Server Sends mb_rsp
EXIT
Figure 17:
6.8
MODBUS function code 08 provides a series of tests for checking the communication system
between a client device and a server, or for checking various internal error conditions within a
server.
The function uses a twobyte sub-function code field in the query to define the type of test to
be performed. The server echoes both the function code and sub-function code in a normal
response. Some of the diagnostics cause data to be returned from the remote device in the
data field of a normal response.
In general, issuing a diagnostic function to a remote device does not affect the running of the
user program in the remote device. User logic, like discrete and registers, is not accessed by
the diagnostics. Certain functions can optionally reset error counters in the remote device.
A server device can, however, be forced into Listen Only Mode in which it will monitor the
messages on the communications system but not respond to them. This can affect the
outcome of your application program if it depends upon any further exchange of data with the
remote device. Generally, the mode is forced to remove a malfunctioning remote device from
the communications system.
The following diagnostic functions are dedicated to serial line devices.
The normal response to the Return Query Data request is to loopback the same data. The
function code and sub-function codes are also echoed.
Request
Functio n code
Sub-f unctio n
Data
1 Byte
2 Bytes
N x 2 Bytes
0x08
http://www.modbus.org
21/50
Modbus
Response
Functio n code
Sub-f unctio n
Data
1 Byte
2 Bytes
N x 2 Bytes
0x08
Error code
Exceptio n code
1 Byte
1 Byte
0x88
01 or 03 or 04
Error
6.8.1
Here the list of sub-function codes supported by the serial line devices. Each sub-function
code is then listed with an example of the data field contents that would apply for that
diagnostic.
Sub-f unctio n code
Hex
Dec
00
00
01
01
02
02
03
03
04
04
05.. 09
0A
10
0B
11
0C
12
0D
13
0E
14
0F
15
10
16
11
17
12
18
13
19
14
20
N.A.
21 ... 65535
Name
Return Query Data
Restart Communications Optio n
Return Diagnostic Register
Change ASCII Input Delimiter
Force Listen Only Mode
RESERVED
Clear Co unters and Diagnostic Register
Return B us Message Co unt
Return B us Communicatio n Error Co unt
Return B us E xceptio n E rror Co unt
Return Derver Message Co unt
Return Derver No Respo nse Count
Return Server NAK Co unt
Return Server B usy Co unt
Return B us Character Overrun Co unt
RESERVED
Clear Overrun Co unter and Flag
RESERVED
http://www.modbus.org
22/50
Modbus
http://www.modbus.org
23/50
Modbus
The response data field returns the quantity of messages addressed to the remote device for
which it has returned no response (neither a normal response nor an exception response),
since its last restart, clear counters operation, or powerup.
Sub-function
Data Field (Request)
Data Field (Response)
00 0F
00 00
Server No Response Count
16 (10 Hex) Return Server NAK Count
The response data field returns the quantity of messages addressed to the remote device for
which it returned a Negative Acknowledge (NAK) exception response, since its last restart,
clear counters operation, or powerup. Exception responses are described and listed in
section 7 .
Sub-function
Data Field (Request)
Data Field (Response)
00 10
00 00
Server NAK Count
17 (11 Hex) Return Server Busy Count
The response data field returns the quantity of messages addressed to the remote device for
which it returned a Server Device Busy exception response, since its last restart, clear
counters operation, or powerup.
Sub-function
Data Field (Request)
Data Field (Response)
00 11
00 00
Server Device Busy Count
18 (12 Hex) Return Bus Character Overrun Count
The response data field returns the quantity of messages addressed to the remote device that
it could not handle due to a character overrun condition, since its last restart, clear counters
operation, or powerup. A character overrun is caused by data characters arriving at the port
faster than they can be stored, or by the loss of a character due to a hardware malfunction.
Sub-function
Data Field (Request)
Data Field (Response)
00 12
00 00
Server Character Overrun Count
20 (14 Hex) Clear Overrun Counter and Flag
Clears the overrun error counter and reset the error flag.
Sub-function
Data Field (Request)
Data Field (Response)
00 14
00 00
Echo Request Data
6.8.2
Here is an example of a request to remote device to Return Query Data. This uses a subfunction code of zero (00 00 hex in the twobyte field). The data to be returned is sent in the
twobyte data field (A5 37 hex).
Request
Field Name
Functio n
Sub-f unctio n Hi
Sub-f unctio n Lo
Data Hi
Data Lo
(Hex)
08
00
00
A5
37
Response
Field Name
Functio n
Sub-f unctio n Hi
Sub-f unctio n Lo
Data Hi
Data Lo
(Hex)
08
00
00
A5
37
The data fields in responses to other kinds of queries could contain error counts or other data
requested by the sub-function code.
http://www.modbus.org
24/50
Modbus
ENTRY
MB Server receives mb_req_pdu
NO
ExceptionCode = 01
NO
Data Value == OK
YES
ExceptionCode = 03
Request Processing
NO
Diagnostic == OK
YES
ExceptionCode = 04
Figure 18:
6.9
EXIT
This function code is used to get a status word and an event count from the remote device's
communication event counter.
By fetching the current count before and after a series of messages, a client can determine
whether the messages were handled normally by the remote device.
The devices event counter is incremented once for each successful message completion. It is
not incremented for exception responses, poll commands, or fetch event counter commands.
The event counter can be reset by means of the Diagnostics function (code 08), with a subfunction of Restart Communications Option (code 00 01) or Clear Counters and Diagnostic
Register (code 00 0A).
The normal response contains a twobyte status word, and a twobyte event count. The
status word will be all ones (FF FF hex) if a previouslyissued program command is still being
processed by the remote device (a busy condition exists). Otherwise, the status word will be
all zeros.
Request
Functio n code
1 Byte
0x0B
Functio n code
Status
Event Co unt
1 Byte
2 Bytes
2 Bytes
0x0B
0x0000 to 0xFFFF
0x0000 to 0xFFFF
Error code
Exceptio n code
1 Byte
1 Byte
0x8B
01 or 04
Response
Error
Here is an example of a request to get the communications event counter in remote device:
Request
Response
http://www.modbus.org
25/50
(Hex)
0B
Modbus
Field Name
Functio n
Status Hi
Status Lo
Event Co unt Hi
Event Co unt Lo
(Hex)
0B
FF
FF
01
08
In this example, the status word is FF FF hex, indicating that a program function is still in
progress in the remote device. The event count shows that 264 (01 08 hex) events have been
counted by the device.
ENTRY
MB Server receives mb_req_pdu
NO
Function code
supported
YES
ExceptionCode = 01
Request Processing
NO
GetCommEventCounter == OK
YES
ExceptionCode = 04
Figure 19:
6.10
EXIT
This function code is used to get a status word, event count, message count, and a field of
event bytes from the remote device.
The status word and event counts are identical to that returned by the Get Communications
Event Counter function (11, 0B hex).
The message counter contains the quantity of messages processed by the remote device
since its last restart, clear counters operation, or powerup. This count is identical to that
returned by the Diagnostic function (code 08), sub-function Return Bus Message Count (code
11, 0B hex).
The event bytes field contains 0-64 bytes, with each byte corresponding to the status of one
MODBUS send or receive operation for the remote device. The remote device enters the
events into the field in chronological order. Byte 0 is the most recent event. Each new byte
flushes the oldest byte from the field.
http://www.modbus.org
26/50
Modbus
The normal response contains a twobyte status word field, a twobyte event count field, a
twobyte message count field, and a field containing 0-64 bytes of events. A byte count field
defines the total length of the data in these four fields.
Request
Functio n code
1 Byte
0x0C
Response
Functio n code
Byte Co unt
Status
Event Co unt
Message Co unt
Events
1 Byte
1 Byte
2 Bytes
2 Bytes
2 Bytes
(N-6) x 1 Byte
0x0C
N*
0x0000 to 0xFFFF
0x0000 to 0xFFFF
0x0000 to 0xFFFF
*N = Quantity of Events + 3 x 2 Bytes, (Length of Status, Event Count and Message Count)
Error
Error code
Exceptio n code
1 Byte
1 Byte
0x8C
01 or 04
Here is an example of a request to get the communications event log in remote device:
Request
Field Name
Functio n
(Hex)
0C
Response
Field Name
Functio n
Byte Co unt
Status Hi
Status Lo
Event Co unt Hi
Event Co unt Lo
Message Co unt Hi
Message Co unt Lo
Event 0
Event 1
(Hex)
0C
08
00
00
01
08
01
21
20
00
In this example, the status word is 00 00 hex, indicating that the remote device is not
processing a program function. The event count shows that 264 (01 08 hex) events have been
counted by the remote device. The message count shows that 289 (01 21 hex) messages
have been processed.
The most recent communications event is shown in the Event 0 byte. Its content (20 hex)
show that the remote device has most recently entered the Listen Only Mode.
The previous event is shown in the Event 1 byte. Its contents (00 hex) show that the remote
device received a Communications Restart.
The layout of the responses event bytes is described below.
What the Event Bytes Contain
An event byte returned by the Get Communications Event Log function can be any one of four
types. The type is defined by bit 7 (the highorder bit) in each byte. It may be further defined
by bit 6. This is explained below.
Remote device MODBUS Receive Event
The remote device stores this type of event byte when a query message is received. It
is stored before the remote device processes the message. This event is defined by bit
7 set to logic 1. The other bits will be set to a logic 1 if the corresponding condition
is TRUE. The bit layout is:
Bit
0
1
2
3
4
April 26, 2012
Contents
Not Used
Communication Error
Not Used
Not Used
Character Overrun
http://www.modbus.org
27/50
Modbus
Contents
Read Exception Sent (Exception Codes 1-3)
Server Abort Exception Sent (Exception Code 4)
Server Busy Exception Sent (Exception Codes 5-6)
Server Program NAK Exception Sent (Exception Code 7)
Write Timeout Error Occurred
Currently in Listen Only Mode
1
0
NO
Function code
supported
YES
ExceptionCode = 01
Request Processing
NO
GetCommEventLog == OK
YES
ExceptionCode = 04
EXIT
http://www.modbus.org
28/50
Modbus
This function code is used to force each coil in a sequence of coils to either ON or OFF in a
remote device. The Request PDU specifies the coil references to be forced. Coils are
addressed starting at zero. Therefore coil numbered 1 is addressed as 0.
The requested ON/OFF states are specified by contents of the request data field. A logical '1'
in a bit position of the field requests the corresponding output to be ON. A logical '0' requests
it to be OFF.
The normal response returns the function code, starting address, and quantity of coils forced.
Request PDU
Functio n code
Starting Address
Quantity of Outputs
Byte Co unt
Outputs Value
1 Byte
2 Bytes
2 Bytes
1 Byte
N* x 1 B yte
0x0F
0x0000 to 0xFFFF
0x0001 to 0x07B0
N*
1 Byte
2 Bytes
2 Bytes
0x0F
0x0000 to 0xFFFF
0x0001 to 0x07B0
Error code
Exceptio n code
1 Byte
1 Byte
0x8F
01 or 02 or 03 or 04
Error
29 28
The first byte transmitted (CD hex) addresses outputs 27-20, with the least significant bit
addressing the lowest output (20) in this set.
The next byte transmitted (01 hex) addresses outputs 29-28, with the least significant bit
addressing the lowest output (28) in this set. Unused bits in the last data byte should be zero
filled.
Request
Field Name
Functio n
Starting Address Hi
Starting Address Lo
Quantity of Outputs Hi
Quantity of Outputs Lo
Byte Co unt
Outputs Value Hi
Outputs Value Lo
(Hex)
0F
00
13
00
0A
02
CD
01
Response
Field Name
Functio n
Starting Address Hi
Starting Address Lo
Quantity of Outputs Hi
Quantity of Outputs Lo
http://www.modbus.org
(Hex)
0F
00
13
00
0A
29/50
Modbus
ENTRY
MB Server receives mb_req_pdu
NO
Function code
supported
YES
ExceptionCode = 01
NO
ExceptionCode = 03
NO
Starting Address == OK
AND
Starting Address + Quantity of Outputs == OK
YES
ExceptionCode = 02
Request Processing
NO
WriteMultipleOutputs
== OK
YES
ExceptionCode = 04
Figure 21:
6.12
EXIT
This function code is used to write a block of contiguous registers (1 to 123 registers) in a
remote device.
The requested written values are specified in the request data field. Data is packed as two
bytes per register.
The normal response returns the function code, starting address, and quantity of registers
written.
Request
Functio n code
Starting Address
Quantity of Registers
Byte Co unt
Registers V alue
1 Byte
2 Bytes
2 Bytes
1 Byte
N* x 2 B ytes
0x10
0x0000 to 0xFFFF
0x0001 to 0x007B
2 x N*
value
1 Byte
2 Bytes
2 Bytes
0x10
0x0000 to 0xFFFF
1 to 123 (0x7B )
*N = Quantity of Registers
Response
Functio n code
Starting Address
Quantity of Registers
Error
Error code
1 Byte
0x90
Exceptio n code
1 Byte
01 or 02 or 03 or 04
(Hex)
10
00
01
Response
Field Name
Functio n
Starting Address Hi
Starting Address Lo
http://www.modbus.org
(Hex)
10
00
01
30/50
00
02
04
00
0A
01
02
Modbus
Quantity of Registers Hi
Quantity of Registers Lo
00
02
ENTRY
MB Server receives mb_req_pdu
NO
Function code
supported
YES
ExceptionCode = 01
NO
ExceptionCode = 03
NO
Starting Address == OK
AND
Starting Address + Quantity of Registers == OK
YES
ExceptionCode = 02
Request Processing
NO
WriteMultipleRegisters
== OK
YES
ExceptionCode = 04
EXIT
Figure 22:
6.13
This function code is used to read the description of the type, the current status, and other
information specific to a remote device.
The format of a normal response is shown in the following example. The data contents are
specific to each type of device.
Request
Functio n code
1 Byte
0x11
1 Byte
1 Byte
device
specific
1 Byte
0x11
1 Byte
1 Byte
0x91
01 or 04
Response
Functio n code
Byte Co unt
Server ID
Run Indicator Status
Additio nal Data
Error
Error code
Exceptio n code
http://www.modbus.org
31/50
(Hex)
11
Modbus
Response
Field Name
Functio n
Byte Co unt
Server ID
Run Indicator Status
Additio nal Data
(Hex)
11
Device
Specific
Device
Specific
0x00 or 0xFF
Device
Specific
ENTRY
MB Server receives mb_req_pdu
NO
Function code
supported
YES
ExceptionCode = 01
Request Processing
NO
ReportSlaveID == OK
YES
ExceptionCode = 04
MB Server Sends mb_rsp
Figure 23:
6.14
EXIT
This function code is used to perform a file record read. All Request Data Lengths are
provided in terms of number of bytes and all Record Lengths are provided in terms of
registers.
A file is an organization of records. Each file contains 10000 records, addressed 0000 to 9999
decimal or 0X0000 to 0X270F. For example, record 12 is addressed as 12.
The function can read multiple groups of references. The groups can be separating (noncontiguous), but the references within each group must be sequential.
Each group is defined in a separate sub-request field that contains 7 bytes:
The reference type: 1 byte (must be specified as 6)
The File number: 2 bytes
The starting record number within the file: 2 bytes
The length of the record to be read: 2 bytes.
The quantity of registers to be read, combined with all other fields in the expected response,
must not exceed the allowable length of the MODBUS PDU : 253 bytes.
The normal response is a series of sub-responses, one for each sub-request. The byte
count field is the total combined count of bytes in all sub-responses. In addition, each subresponse contains a field that shows its own byte count.
Request
Functio n code
Byte Co unt
Sub-Req. x, Reference Type
Sub-Req. x, File Number
1
1
1
2
Byte
Byte
Byte
Bytes
0x14
0x07 to 0xF5 bytes
06
0x0001 to 0xFFFF
http://www.modbus.org
32/50
2 Bytes
2 Bytes
Modbus
0x0000 to 0x270F
N
Response
Functio n code
Resp. data Length
Sub-Req. x, File Resp. length
Sub-Req. x, Reference Type
Sub-Req. x, Record Data
Sub-Req. x+1, ...
1 Byte
1 Byte
1 Byte
1 Byte
N x 2 Bytes
0x14
0x07 to 0xF5
0x07 to 0xF5
6
Error
Error code
Exceptio n code
1 Byte
1 Byte
0x94
01 or 02 or 03 or 04 or
08
While it is allowed for the File Number to be in the range 1 to 0xFFFF, it should be noted that
interoperability with legacy equipment may be compromised if the File Number is greater than
10 (0x0A).
Here is an example of a request to read two groups of references from remote device:
Group 1 consists of two registers from file 4, starting at register 1 (address 0001).
Group 2 consists of two registers from file 3, starting at register 9 (address 0009).
Request
Field Name
Functio n
Byte Co unt
Sub-Req. 1,
Sub-Req. 1,
Sub-Req. 1,
Sub-Req. 1,
Sub-Req. 1,
Sub-Req. 1,
Sub-Req. 1,
Sub-Req. 2,
Sub-Req. 2,
Sub-Req. 2,
Sub-Req. 2,
Sub-Req. 2,
Sub-Req. 2,
Sub-Req. 2,
Ref. Type
File Number Hi
File Number Lo
Record number Hi
Record number Lo
Record Length Hi
Record Length Lo
Ref. Type
File Number Hi
File Number Lo
Record number Hi
Record number Lo
Record Length Hi
Record Length Lo
(Hex)
14
0E
06
00
04
00
01
00
02
06
00
03
00
09
00
02
Response
Field Name
Functio n
Resp. Data length
Sub-Req. 1, File resp. length
Sub-Req. 1, Ref. Type
Sub-Req. 1, Register.Data Hi
Sub-Req. 1, Register.DataLo
Sub-Req. 1, Register.Data Hi
Sub-Req. 1, Register.DataLo
Sub-Req. 2, File resp. length
Sub-Req. 2, Ref. Type
Sub-Req. 2, Register.Data H
Sub-Req. 2, Register.DataLo
Sub-Req. 2, Register.Data Hi
Sub-Req. 2, Register.DataLo
http://www.modbus.org
(Hex)
14
0C
05
06
0D
FE
00
20
05
06
33
CD
00
40
33/50
Modbus
ENTRY
MB Server receives mb_req_pdu
NO
Function code
supported
YES
ExceptionCode = 01
NO
0x07 Byte Count 0xF5
For each Sub-Req
YES
ExceptionCode = 03
Reference Type == OK
AND
File Number == OK
AND
Record number == OK
AND
Starting Address + Register length == OK
NO
YES
ExceptionCode = 02
Request Processing
NO
ReadGeneralReference
== OK
YES
ExceptionCode = 04
EXIT
Figure 24:
6.15
This function code is used to perform a file record write. All Request Data Lengths are
provided in terms of number of bytes and all Record Lengths are provided in terms of the
number of 16-bit words.
A file is an organization of records. Each file contains 10000 records, addressed 0000 to 9999
decimal or 0X0000 to 0X270F. For example, record 12 is addressed as 12.
The function can write multiple groups of references. The groups can be separate, i.e. non
contiguous, but the references within each group must be sequential.
Each group is defined in a separate sub-request field that contains 7 bytes plus the data:
The reference type: 1 byte (must be specified as 6)
The file number: 2 bytes
The starting record number within the file: 2 bytes
The length of the record to be written: 2 bytes
The data to be written: 2 bytes per register.
The quantity of registers to be written, combined with all other fields in the request, must not
exceed the allowable length of the MODBUS PDU : 253bytes.
The normal response is an echo of the request.
Request
Functio n code
Request data length
Sub-Req. x, Reference Type
Sub-Req. x, File Number
Sub-Req. x, Record Number
1
1
1
2
2
Byte
Byte
Byte
Bytes
Bytes
0x15
0x09 to 0xFB
06
0x0001 to 0xFFFF
0x0000 to 0x270F
http://www.modbus.org
34/50
2 Bytes
N x 2 Bytes
Modbus
N
Response
Functio n code
1 Byte
0x15
1 Byte
0x09 to 0xFB
1 Byte
06
2 Bytes
0x0001 to 0xFFFF
2 Bytes
0x0000 to 0x270F
2 Bytes
N x 2 Bytes
Error
Error code
1 Byte
0x95
Exceptio n code
1 Byte
01 or 02 or 03 or 04 or 08
While it is allowed for the File Number to be in the range 1 to 0xFFFF, it should be noted that
interoperability with legacy equipment may be compromised if the File Number is greater than
10 (0x0A).
Here is an example of a request to write one group of references into remote device:
The group consists of three registers in file 4, starting at register 7 (address 0007).
Request
Field Name
Functio n
Request Data length
Sub-Req. 1, Ref. Type
Sub-Req. 1, File Number Hi
Sub-Req. 1, File Number Lo
Sub-Req. 1, Record number Hi
Sub-Req. 1, Record number Lo
Sub-Req.
Sub-Req.
Sub-Req.
Sub-Req.
Sub-Req.
Sub-Req.
Sub-Req.
Sub-Req.
1,
1,
1,
1,
1,
1,
1,
1,
Record length Hi
Record length Lo
Register Data Hi
Register Data Lo
Register Data Hi
Register Data Lo
Register Data Hi
Register Data Lo
(Hex)
15
0D
06
00
04
00
07
00
03
06
AF
04
BE
10
0D
Response
Field Name
Functio n
Request Data length
Sub-Req. 1, Ref. Type
Sub-Req. 1, File Number Hi
Sub-Req. 1, File Number Lo
Sub-Req. 1, Record number Hi
Sub-Req. 1, Record number
Lo
Sub-Req. 1, Record length Hi
Sub-Req. 1, Record length Lo
Sub-Req. 1, Register Data Hi
Sub-Req. 1, Register Data Lo
Sub-Req. 1, Register Data Hi
Sub-Req. 1, Register Data Lo
Sub-Req. 1, Register Data Hi
Sub-Req. 1, Register Data Lo
http://www.modbus.org
(Hex)
15
0D
06
00
04
00
07
00
03
06
AF
04
BE
10
0D
35/50
Modbus
ENTRY
MB Server receives mb_req_pdu
NO
Function code
supported
YES
ExceptionCode = 01
NO
0x07 Byte Count 0xF5
For each Sub-Req
YES
ExceptionCode = 03
Reference Type == OK
AND
File Number == OK
AND
Record number == OK
AND
Starting Address + Register length == OK
NO
YES
ExceptionCode = 02
Request Processing
NO
WriteGeneralReference
== OK
YES
ExceptionCode = 04
Figure 25:
6.16
EXIT
This function code is used to modify the contents of a specified holding register using a
combination of an AND mask, an OR mask, and the register's current contents. The function
can be used to set or clear individual bits in the register.
The request specifies the holding register to be written, the data to be used as the AND mask,
and the data to be used as the OR mask. Registers are addressed starting at zero. Therefore
registers 1-16 are addressed as 0-15.
The functions algorithm is:
Result = (Current Contents AND And_Mask) OR (Or_Mask AND (NOT And_Mask))
For example:
Hex
Binary
Current Contents=
12
0001 0010
And_Mask =
F2
1111 0010
Or_Mask =
25
0010 0101
(NOT And_Mask)=
0D
0000 1101
Result =
17
0001 0111
Note:
If the Or_Mask value is zero, the result is simply the logical ANDing of the current contents and
And_Mask. If the A nd_Mask value is zero, the result is equal to the Or_Mask value.
The co ntents of the register can be read with the Read Ho lding Registers f unctio n (f unction code 03).
They co uld, ho wever, be changed subsequently as the co ntro ller scans its user logic program.
http://www.modbus.org
36/50
Modbus
The normal response is an echo of the request. The response is returned after the register
has been written.
Request
Functio n code
Reference Address
And_Mask
Or_Mask
1
2
2
2
Byte
Bytes
Bytes
Bytes
0x16
0x0000 to 0xFFFF
0x0000 to 0xFFFF
0x0000 to 0xFFFF
1
2
2
2
Byte
Bytes
Bytes
Bytes
0x16
0x0000 to 0xFFFF
0x0000 to 0xFFFF
0x0000 to 0xFFFF
Response
Functio n code
Reference Address
And_Mask
Or_Mask
Error
Error code
Exceptio n code
1 Byte
1 Byte
0x96
01 or 02 or 03 or 04
Here is an example of a Mask Write to register 5 in remote device, using the above mask
values.
Request
Field Name
Functio n
Reference address Hi
Reference address Lo
And_Mask Hi
And_Mask Lo
Or_Mask Hi
Or_Mask Lo
Response
Field Name
Functio n
Reference address Hi
Reference address Lo
And_Mask Hi
And_Mask Lo
Or_Mask Hi
Or_Mask Lo
(Hex)
16
00
04
00
F2
00
25
(Hex)
16
00
04
00
F2
00
25
ENTRY
MB Server receives mb_req_pdu
NO
Function code
supported
YES
ExceptionCode = 01
NO
Reference Address == OK
YES
ExceptionCode = 02
NO
AND_Mask == OK
AND
OR_Mask == OK
YES
ExceptionCode = 03
Request Processing
NO
MaskWriteRegister
ExceptionCode = 04
== OK
YES
MB Server Sends mb_rsp
Figure 26:
April 26, 2012
EXIT
37/50
6.17
Modbus
This function code performs a combination of one read operation and one write operation in a
single MODBUS transaction. The write operation is performed before the read.
Holding registers are addressed starting at zero. Therefore holding registers 1-16 are
addressed in the PDU as 0-15.
The request specifies the starting address and number of holding registers to be read as well
as the starting address, number of holding registers, and the data to be written. The byte
count specifies the number of bytes to follow in the write data field.
The normal response contains the data from the group of registers that were read. The byte
count field specifies the quantity of bytes to follow in the read data field.
Request
Functio n code
Read Starting Address
Quantity to Read
Write Starting A ddress
Quantity to W rite
Write Byte Co unt
Write Registers Value
1 Byte
2 Bytes
2 Bytes
2 Bytes
2 Bytes
1 Byte
N*x 2 Bytes
0x17
0x0000
0x0001
0x0000
0x0001
2 x N*
to
to
to
to
0xFFFF
0x007D
0xFFFF
0X 0079
*N = Quantity to Write
Response
Functio n code
Byte Co unt
Read Registers value
1 Byte
1 Byte
N'* x 2 Bytes
0x17
2 x N'*
1 Byte
1 Byte
0x97
01 or 02 or 03 or 04
Here is an example of a request to read six registers starting at register 4, and to write three
registers starting at register 15:
Request
Field Name
Functio n
Read Starting Address Hi
Read Starting Address Lo
Quantity to Read Hi
Quantity to Read Lo
Write Starting A ddress Hi
Write Starting address Lo
Quantity to W rite Hi
Quantity to W rite Lo
Write Byte Co unt
Write Registers Value Hi
Write Registers Value Lo
Write Registers Value Hi
Write Registers Value Lo
Write Registers Value Hi
Write Registers Value Lo
(Hex)
17
00
03
00
06
00
0E
00
03
06
00
FF
00
FF
00
FF
Response
Field Name
Functio n
Byte Co unt
Read Registers
Read Registers
Read Registers
Read Registers
Read Registers
Read Registers
Read Registers
Read Registers
Read Registers
Read Registers
Read Registers
Read Registers
value
value
value
value
value
value
value
value
value
value
value
value
http://www.modbus.org
Hi
Lo
Hi
Lo
Hi
Lo
Hi
Lo
Hi
Lo
Hi
Lo
(Hex)
17
0C
00
FE
0A
CD
00
01
00
03
00
0D
00
FF
38/50
Modbus
ENTRY
MB Server receives mb_req_pdu
NO
Function code
supported
YES
ExceptionCode = 01
0x0001 Quantity of Read 0x007D
AND
0x0001 Quantity of Write 0x0079
AND
Byte Count == Quantity of Write x 2
NO
YES
ExceptionCode = 03
Read Starting Address == OK
AND
Read Starting Address + Quantity of Read == OK
AND
Write Starting Address == OK
AND
Write Starting Address + Quantity of Write == OK
NO
YES
ExceptionCode = 02
Request Processing
Write operation before read operation
NO
Read/WriteMultipleRegisters == OK
YES
ExceptionCode = 04
Figure 27:
EXIT
http://www.modbus.org
39/50
Modbus
This function code allows to read the contents of a First-In-First-Out (FIFO) queue of register
in a remote device. The function returns a count of the registers in the queue, followed by the
queued data. Up to 32 registers can be read: the count, plus up to 31 queued data registers.
The queue count register is returned first, followed by the queued data registers.
The function reads the queue contents, but does not clear them.
In a normal response, the byte count shows the quantity of bytes to follow, including the
queue count bytes and value register bytes (but not including the error check field).
The queue count is the quantity of data registers in the queue (not including the count
register).
If the queue count exceeds 31, an exception response is returned with an error code of 03
(Illegal Data Value).
Request
Functio n code
FIFO Pointer Address
1 Byte
2 Bytes
0x18
0x0000 to 0xFFFF
1 Byte
2 Bytes
2 Bytes
N* x 2 B ytes
0x18
Response
Functio n code
Byte Co unt
FIFO Co unt
FIFO V alue Register
31
*N = FIFO Count
Error
Error code
Exceptio n code
1 Byte
1 Byte
0x98
01 or 02 or 03 or 04
Here is an example of Read FIFO Queue request to remote device. The request is to read the
queue starting at the pointer register 1246 (0x04DE):
Request
Field Name
Functio n
FIFO Pointer Address Hi
FIFO Pointer Address Lo
(Hex)
18
04
DE
Response
Field Name
Functio n
Byte Co unt Hi
Byte Co unt Lo
FIFO Co unt Hi
FIFO Co unt Lo
FIFO V alue Register
FIFO V alue Register
FIFO V alue Register
FIFO V alue Register
Hi
Lo
Hi
Lo
(Hex)
18
00
06
00
02
01
B8
12
84
In this example, the FIFO pointer register (1246 in the request) is returned with a queue count
of 2. The two data registers follow the queue count. These are:
1247 (contents 440 decimal -- 0x01B8); and 1248 (contents 4740 -- 0x1284).
http://www.modbus.org
40/50
Modbus
ENTRY
MB Server receives mb_req_pdu
NO
Function code
supported
YES
ExceptionCode = 01
NO
YES
ExceptionCode = 02
NO
FIFO Count 31
YES
ExceptionCode = 03
Request Processing
NO
ReadFIFOQueue
== OK
YES
ExceptionCode = 04
Figure 28:
6.19
EXIT
Informative Note: The user is asked to refer to Annex A (Informative) MODBUS RESERVED
FUNCTION CODES, SUBCODES AND MEI TYPES.
Function Code 43 and its MEI Type 14 for Device Identification is one of two Encapsulated
Interface Transport currently available in this Specification. The following function codes and
MEI Types shall not be part of this published Specification and these function codes and MEI
Types are specifically reserved: 43/0-12 and 43/15-255.
The MODBUS Encapsulated Interface (MEI)Transport is a mechanism for tunneling service
requests and method invocations, as well as their returns, inside MODBUS PDUs.
The primary feature of the MEI Transport is the encapsulation of method invocations or
service requests that are part of a defined interface as well as method invocation returns or
service responses.
http://www.modbus.org
41/50
Figure 29:
Modbus
The Network Interface can be any communication stack used to send MODBUS PDUs, such
as TCP/IP, or serial line.
A MEI Type is a MODBUS Assigned Number and therefore will be unique, the value between
0 to 255 are Reserved according to Annex A (Informative) except for MEI Type 13 and MEI
Type 14.
The MEI Type is used by MEI Transport implementations to dispatch a method invocation to
the indicated interface.
Since the MEI Transport service is interface agnostic, any specific behavior or policy required
by the interface must be provided by the interface, e.g. MEI transaction processing, MEI
interface error handling, etc.
Request
Functio n code
MEI Type*
MEI type specific data
1 Byte
1 Byte
n Bytes
0x2B
0x0D or 0x0E
1 Byte
1 byte
n Bytes
Functio n code
1 Byte
Exceptio n code
1 Byte
0x2B
echo of
Request
MEI
Type
in
Error
0xAB :
Fc 0x2B + 0x80
01 or 02 or 03 or 04
The CANopen General reference Command is an encapsulation of the services that will be
used to access (read from or write to) the entries of a CAN-Open Device Object Dictionary as
well as controlling and monitoring the CANopen system, and devices.
The MEI Type 13 (0x0D) is a MODBUS Assigned Number licensed to CiA for the CANopen
General Reference.
The system is intended to work within the limitations of existing MODBUS networks.
Therefore, the information needed to query or modify the object dictionaries in the system is
April 26, 2012
http://www.modbus.org
42/50
Modbus
mapped into the format of a MODBUS message. The PDU will have the 253 Byte limitation in
both the Request and the Response message.
Informative: Please refer to Annex B for a reference to a specification that provides
information on MEI Type 13.
6.21
This function code allows reading the identification and additional information relative to the
physical and functional description of a remote device, only.
The Read Device Identification interface is modeled as an address space composed of a set
of addressable data elements. The data elements are called objects and an object Id identifies
them.
The interface consists of 3 categories of objects :
Basic Device Identification. All objects of this category are mandatory : VendorName,
Product code, and revision number.
Regular Device Identification. In addition to Basic data objects, the device provides
additional and optional identification and description data objects. All of the objects of
this category are defined in the standard but their implementation is optional .
Extended Device Identification. In addition to regular data objects, the device provides
additional and optional identification and description private data about the physical
device itself. All of these data are device dependent.
Object
Id
0x00
0x01
0x02
0x03
0x04
0x05
0x06
0x07
0x7F
0x80
0xFF
Type
VendorName
ProductCode
MajorMinorRevision
VendorUrl
ProductName
ModelName
UserApplicationName
Reserved
ASCII
ASCII
ASCII
ASCII
ASCII
ASCII
ASCII
String
String
String
String
String
String
String
M/O
category
Mandatory
Mandatory
Mandatory
Optional
Optional
Optional
Optional
Optional
Basic
device
dependant
Optional
Regular
Extended
Request
Functio n code
MEI Type*
Read Device ID code
Object Id
1
1
1
1
Byte
Byte
Byte
Byte
0x2B
0x0E
01 / 02 / 03 / 04
0x00 to 0xFF
1
1
1
1
Byte
byte
Byte
Byte
More Fo llows
Next Object Id
Number of objects
List Of
Object ID
Object length
Object V alue
1 Byte
1 Byte
1 Byte
1 Byte
1 Byte
Object length
0x2B
0x0E
01 / 02 / 03 / 04
0x01 or 0x02 or 0x03 or
0x81 or 0x82 or 0x83
00 / FF
Object ID number
Error
Functio n code
1 Byte
0xAB :
Fc 0x2B + 0x80
http://www.modbus.org
43/50
1 Byte
Modbus
01 or 02 or 03 or 04
http://www.modbus.org
44/50
Modbus
Object0.Length
Length of the first Object in byte
Object0.Value
Value of the first Object (Object0.Length bytes)
ObjectN.Id
Identification of the last Object (within the response)
ObjectN.Length
Length of the last Object in byte
ObjectN.Value
Value of the last Object (ObjectN.Length bytes)
Example of a Read Device Identification request for "Basic device identification" : In this
example all information are sent in one response PDU.
Request
Field Name
Functio n
MEI Type
Read Dev Id code
Object Id
Value
2B
0E
01
00
Response
Field Name
Functio n
MEI Type
Read Dev Id Code
Conformity Level
More Fo llows
NextObjectId
Number Of Objects
Object Id
Object Length
Object V alue
Object Id
Object Length
Object V alue
Object Id
Object Length
Object V alue
Value
2B
0E
01
01
00
00
03
00
16
" Company identification"
01
0D
" P roduct code XX"
02
05
"V2.11"
In case of a device that required several transactions to send the response the following
transactions is intiated.
First transaction :
Request
Field Name
Functio n
MEI Type
Read Dev Id code
Object Id
Value
2B
0E
01
00
Response
Field Name
Functio n
MEI Type
Read Dev Id Code
Conformity Level
More Fo llows
NextObjectId
Number Of Objects
Object Id
Object Length
Object V alue
Object Id
Object Length
Object V alue
Value
2B
0E
01
01
FF
02
03
00
16
" Company identification"
01
1C
" P roduct code
XXXXXXXXXXXXXXXX"
Second transaction :
Request
Field Name
Functio n
MEI Type
Read Dev Id code
Object Id
Value
2B
0E
01
02
Response
Field Name
Functio n
MEI Type
Read Dev Id Code
Conformity Level
More Fo llows
NextObjectId
Number Of Objects
Object Id
Object Length
Object V alue
http://www.modbus.org
Value
2B
0E
01
01
00
00
03
02
05
"V2.11"
45/50
Modbus
ENTRY
MB Server receives mb_req_pdu
NO
Function code
supported
YES
NO
Object Id OK
ExceptiCode = 01
YES
NO
Except.Code = 02
Request Processing
NO
More follows = FF
Next Object ID = XX
More follows = 00
Next Object ID = 00
MB Server Sends
mb_exception_rsp
Figure 30:
EXIT
http://www.modbus.org
46/50
Modbus
When a client device sends a request to a server device it expects a normal response. One
of four possible events can occur from the clients query:
If the server device receives the request without a communication error, and can
handle the query normally, it returns a normal response.
If the server does not receive the request due to a communication error, no response
is returned. The client program will eventually process a timeout condition for the
request.
If the server receives the request, but detects a communication error (parity, LRC,
CRC, ...), no response is returned. The client program will eventually process a
timeout condition for the request.
If the server receives the request without a communication error, but cannot handle it
(for example, if the request is to read a nonexistent output or register), the server
will return an exception response informing the client of the nature of the error.
The exception response message has two fields that differentiate it from a normal response:
Function Code Field: In a normal response, the server echoes the function code of the
original request in the function code field of the response. All function codes have a most
significant bit (MSB) of 0 (their values are all below 80 hexadecimal). In an exception
response, the server sets the MSB of the function code to 1. This makes the function code
value in an exception response exactly 80 hexadecimal higher than the value would be for a
normal response.
With the function codes MSB set, the client's application program can recognize the
exception response and can examine the data field for the exception code.
Data Field: In a normal response, the server may return data or statistics in the data field
(any information that was requested in the request). In an exception response, the server
returns an exception code in the data field. This defines the server condition that caused the
exception.
Example of a client request and server exception response
Request
Field Name
Functio n
Starting Address Hi
Starting Address Lo
Quantity of Outputs Hi
Quantity of Outputs Lo
(Hex)
01
04
A1
00
01
Response
Field Name
Functio n
Exceptio n Code
(Hex)
81
02
In this example, the client addresses a request to server device. The function code (01) is for
a Read Output Status operation. It requests the status of the output at address 1185 (04A1
hex). Note that only that one output is to be read, as specified by the number of outputs field
(0001).
If the output address is nonexistent in the server device, the server will return the exception
response with the exception code shown (02). This specifies an illegal data address for the
server.
A listing of exception codes begins on the next page.
http://www.modbus.org
47/50
Modbus
Name
02
03
04
05
ACKNOWLEDGE
06
08
ILLEGAL FUNCTION
Meaning
The function code received in the query is not an
allowable action for the server. This may be
because the function code is only applicable to
newer devices, and was not implemented in the
unit selected. It could also indicate that the server
is in the wrong state to process a request of this
type, for example because it is unconfigured and
is being asked to return register values.
The data address received in the query is not an
allowable address for
the server.
More
specifically, the combination of reference number
and transfer length is invalid. For a controller with
100 registers, the PDU addresses the first register
as 0, and the last one as 99. If a request is
submitted with a starting register address of 96
and a quantity of registers of 4, then this request
will successfully operate (address-wise at least)
on registers 96, 97, 98, 99. If a request is
submitted with a starting register address of 96
and a quantity of registers of 5, then this request
will fail with Exception Code 0x02 Illegal Data
Address since it attempts to operate on registers
96, 97, 98, 99 and 100, and there is no register
with address 100.
A value contained in the query data field is not an
allowable value for server. This indicates a fault in
the structure of the remainder of a complex
request, such as that the implied length is
incorrect. It specifically does NOT mean that a
data item submitted for storage in a register has a
value outside the expectation of the application
program, since the MODBUS protocol is unaware
of the significance of any particular value of any
particular register.
An unrecoverable error occurred while the server
was attempting to perform the requested action.
Specialized use in conjunction with programming
commands.
The server has accepted the request and is
processing it, but a long duration of time will be
required to do so. This response is returned to
prevent a timeout error from occurring in the
client. The client can next issue a Poll Program
Complete message to determine if processing is
completed.
Specialized use in conjunction with programming
commands.
The server is engaged in processing a long
duration program command. The client should
retransmit the message later when the server is
free.
Specialized use in conjunction with function codes
20 and 21 and reference type 6, to indicate that
the extended file area failed to pass a consistency
check.
The server attempted to read record file, but
detected a parity error in the memory. The client
can retry the request, but service may be required
http://www.modbus.org
48/50
0A
0B
GATEWAY
TARGET
FAILED TO RESPOND
DEVICE
Modbus
http://www.modbus.org
49/50
Modbus
http://www.modbus.org
50/50