More into Modbus message structure
Address field
Address field contains two ASCII characters or 8 bits
(RTU).Also we know that the valid slave addresses are in 0-247 range decimal.
·
In the response slave puts its own address
in the address field to let the master know which slave is responding.
Function
filed
The function field consists of two ASCII characters
(ASCII) or 8 bits (RTU).In error free response the function code is similar to
receive query. But if there is some error, the most significant bit of query
function code is set to 1 in the response function field.
Ø Example
Query function code=0000 0011
Error free response function
code=0000 0011
If there is an error, response
function code=1000 0011.
In erroneous situation slave puts
unique code in to data field which describes the error occurred.
Data
field
The data field is constructed using sets of two hexadecimal
digits in the range of 00 to FF hexadecimal (pair of ASCII characters or one
RTU character).In query, this data field contains discrete and register addresses,
the quantity of items to be handled, the count of data bytes to follow in the
data field and the data to be written in to registers.
If no error occurs response data field contains the
requested data else it contains unique code which describes the error.
Error
checking field
How Modbus does detect errors?
Standard Modbus serial networks
use two kinds of error checking.
1.
Parity
checking (even or odd) can be optionally applied to each character.
2.
Frame
checking (LRC or CRC) is applied to the entire message.
Both the
character check and message frame check are generated in the master device and
applied to the message contents before transmission. The slave device checks
each character and the entire message frame during receipt.
1.
Parity Checking
Users can configure controllers
for Even or Odd Parity checking, or for No Parity checking. This will determine
how the parity bit will be set in each character. If either Even or Odd Parity
is specified, the quantity of 1 bits will be counted in the data portion of
each character (seven data bits for ASCII mode, or eight for RTU).
The parity bit will then be set to a 0 or 1 to
result in an Even or Odd total of 1 bit.
2.
Frame
checking
IN ASCII mode there are two ASCII characters in the
error checking field. 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.
What is LRC?
The LRC field checks the contents
of the message, exclusive of the beginning ‘colon’ and ending CRLF pair. It is
applied regardless of any parity check method used for the individual characters
of the message. The LRC field is one byte, containing an 8–bit binary value.
The LRC value is calculated by the transmitting device, which appends the LRC
to the message. The receiving device calculates an LRC during receipt of the
message, and compares the calculated value to the actual value it received in
the LRC field. If the two values are not equal, an error results.
In RTU mode error checking field
has two 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.
For more details refer Modbus official site.
Thanks for sharing very helpful information on Modbus protocol and how it work. Very useful post.
ReplyDelete