Modbus Protocol
Modbus Protocol
Modbus Protocol
html
modbus tools
For test, simulation and programming.
Protocol Description
MODBUS© Protocol is a messaging structure, widely used to establish
master-slave
communication between intelligent devices. A MODBUS message sent
from a master to a slave
contains the address of the slave, the 'command' (e.g.
'read register' or 'write register'), the
data, and a check sum (LRC or CRC).
The Request
The function code in the request tells the addressed slave device what kind of
action to
perform. The data bytes contains any additional information that the
slave will need to
perform the function. For example, function code 03 will
request the slave to read holding
registers and respond with their contents. The
data field must contain the information telling
the slave which register to
start at and how many registers to read. The error check field
provides a method
for the slave to validate the integrity of the message contents.
The Response
If the slave makes a normal response, the function code in the response is an
echo of the
function code in the request. The data bytes contain the data
collected by the slave, such as
register values or status. If an error occurs,
the function code is modified to indicate that the
response is an error
response, and the data bytes contain a code that describes the error. The
error
check field allows the master to confirm that the message contents are valid.
ASCII Mode
Coding System
1 start bit
Error Checking
RTU Mode
When controllers are setup to communicate on a Modbus network using RTU (Remote
Terminal
Unit) mode, each eight-bit byte in a message contains two four-bit
hexadecimal characters.
The main advantage of this mode is that its greater
character density allows better data
throughput than ASCII for the same baud
rate. Each message must be transmitted in a
continuous stream.
Coding System
1 start bit
The allowable characters transmitted for all other fields are hexadecimal 0 ...
9, A ... F.
Networked devices monitor the network bus continuously for the colon
character. When one is
received, each device decodes the next field (the address
field) to find out if it is the
addressed device.
Intervals of up to one second can elapse between characters within the message.
If a greater
interval occurs, the receiving device assumes an error has
occurred. A typical message frame
is shown below.
RTU Framing
In RTU mode, messages start with a silent interval of at least 3.5 character
times. This is most
easily implemented as a multiple of character times at the
baud rate that is being used on the
network (shown as T1-T2-T3-T4 in the figure
below). The first field then transmitted is the
device address.
The allowable characters transmitted for all fields are hexadecimal 0 ... 9, A
... F. Networked
devices monitor the network bus continuously, including during
the silent intervals. When the
first field (the address field) is received, each
device decodes it to find out if it is the
addressed device.
Similarly, if a new message begins earlier than 3.5 character times following a
previous
message, the receiving device will consider it a continuation of the
previous message. This will
set an error, as the value in the final CRC field
will not be valid for the combined messages. A
typical message frame is shown
below.
Address Field
The address field of a message frame contains two characters (ASCII) or eight
bits (RTU). The
individual slave devices are assigned addresses in the range of
1 ... 247.
Function Field
The Function Code field tells the addressed slave what function to perform.
Two kinds of error-checking methods are used for standard Modbus networks. The
error
checking field contents depend upon the method that is being used.
ASCII
When ASCII mode is used for character framing, the error-checking field contains
two ASCII
characters. The error check characters are the result of a
Longitudinal Redundancy Check
(LRC) calculation that is performed on the message
contents, exclusive of the beginning colon
and terminating CRLF characters.
The LRC characters are appended to the message as the last field preceding the
CRLF
characters.
RTU
When RTU mode is used for character framing, the error-checking field contains a
16-bit value
implemented as two eight-bit bytes. The error check value is the
result of a Cyclical
Redundancy Check calculation performed on the message
contents.
The CRC field is appended to the message as the last field in the message. When
this is done,
the low-order byte of the field is appended first, followed by the
high-order byte. The CRC
high-order byte is the last byte to be sent in the
message.
Request
The request message specifies the starting coil and quantity of coils to be read.
Example of a request to read 10...22 (Coil 11 to 23) from slave device address 4:
Response
The coil status response message is packed as one coil per bit of the data field. Status is
indicated as: 1 is the value ON, and 0 is the value OFF. The LSB of the first data byte contains
the coil addressed in the
request. 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 coil quantity is not a
multiple of eight, the remaining bits in the final data byte will be padded with zeroes (toward
the high-order end of the byte). The byte count field specifies the quantity of complete bytes
of data.
Request
The request message specifies the starting input and quantity of inputs to be read.
Example of a request to read 10...22 (input 10011 to 10023) from slave device address 4:
Response
The input status response message is packed as one input per bit of the data field. Status is
indicated as: 1 is the value ON, and 0 is the value OFF. The LSB of the first data byte contains
the input addressed in the
request. 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 zeroes
(toward the high-order end of the byte). The byte count field specifies the quantity of
complete bytes of data.
Request
The request message specifies the starting register and quantity of registers to be read.
Example of a request to read 0...1 (register 40001 to 40002) from slave device 1:
Field Name RTU (hex) ASCII Characters
Header None : (Colon)
Slave Address 01 01
Function 03 03
Starting Address Hi 00 00
Starting Address Lo 00 00
Quantity
of Registers Hi 00 00
Quantity
of Registers Lo 02 02
Error Check Lo C4 LRC (F A)
Error Check Hi 0B
Trailer None CR LF
Total Bytes 8 17
Response
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
The request message specifies the starting register and quantity of registers to be read.
Example of a request to read 0...1 (register 30001 to 30002) from slave device 1:
Response
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
The request message specifies the coil reference to be written. Coils are addressed starting at
zero-coil 1 is addressed as 0.
The requested ON / OFF state is specified by a constant in the request data field. A value of FF
00 hex requests the coil to be ON. A value of 00 00 requests it to be OFF. All other values are
illegal and will not affect the coil.
Response
The normal response is an echo of the request, returned after the coil state has been written.
Request
The request message specifies the register reference to be Written. Registers are addressed
starting at zero-register 1 is addressed as 0.
The requested Write value is specified in the request data field. Here is an example of a
request to Write register 40002 to 00 03 hex in slave device 17.
Response
The normal response is an echo of the request, returned after the register contents have been
written.
Request
The request message specifies the coil references to be written. Coils are addressed starting at
zero-coil 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 coils to be ON. A logical 0 requests it to
be OFF.
Below is an example of a request to write a series of ten coils starting at coil 20 (addressed as
19, or 13 hex) in slave device 17.
The request data contents are two bytes: CD 01 hex (1100 1101 0000 0001 binary). The
binary bits correspond to the coils in the following way:
Bit: 1 1 0 0 1 1 0 1 0 0 0 0 0 0 0 1
Coil: 27 26 25 24 23 22 21 20 - - - - - - 29 28
The first byte transmitted (CD hex) addresses coils 27 ... 20, with the least significant bit
addressing the lowest coil (20) in this set.
The next byte transmitted (01 hex) addresses coils 29 and 28, with the least significant bit
addressing the lowest coil (28) in this set. Unused bits in the last data byte should be zero-
filled.
Response
The normal response returns the slave address, function code, starting address, and number
of coils written. Here is an example of a response to the
request shown above
Request
The request message specifies the register references to be written. Registers are addressed
starting at zero-register 1 is addressed as 0.
The requested write values are specified in the request data field. Data is packed as two bytes
per register.
Response
The normal response returns the slave address, function code, starting address, and quantity
of registers written. Here is an example of a response to the
request shown above.
This function is an example how to calculate a LRC BYTE using the C language.
return (BYTE)(-nLRC);
} // End: LRC
This function is an example how to calculate a CRC word using the C language.
BYTE nTemp;
while (wLength--)
wCRCWord >>= 8;
wCRCWord ^= wCRCTable[nTemp];
return wCRCWord;
} // End: CRC16