History of The Modbus Protocol
History of The Modbus Protocol
Some communication standards just emerge. Not because they are pushed by a large group of vendors or a special standards organisation. These standardslike the Modbus interface emerge because they are good, simple to implement and are therefore adapted by many manufacturers. Because of this, Modbus became the first widely accepted fieldbus standard. Modbus has its roots in the late seventies of the previous century. It is 1979 when PLC manufacturer Modiconnow a brand of Schneider Electric's Telemecaniquepublished the Modbus communication interface for a multidrop network based on a master/client architecture. Communication between the Modbus nodes was achieved with messages. It was an open standard that described the messaging structure. The physical layer of the Modbus interface was free to choose. The original Modbus interface ran on RS-232, but most later Modbus implementations used RS-485 because it allowed longer distances, higher speeds and the possibility of a true multi-drop network. In a short time hunderds of vendors implemented the Modbus messaging system in their devices and Modbus became the de facto standard for industrial communication networks. The nice thing of the Modbus standard is the flexibility, but at the same time the easy implementation of it. Not only intelligent devices like microcontrollers, PLCs etc. are able to communicate with Modbus, also many intelligent sensors are equiped with a Modbus interface to send their data to host systems. While Modbus was previously mainly used on wired serial communication lines, there are also extensions to the standard for wireless communications and TCP/IP networks.
Each Modbus message has the same structure. Four basic elements are present in each message. The sequence of these elements is the same for all messages, to make it easy to parse the content of the Modbus message. A conversation is always started by a master in the Modbus network. A Modbus master sends a message anddepending of the contents of the messagea slave takes action and responds to it. There can be more masters in a Modbus network. Addressing in the message header is used to define which device should respond to a message. All other nodes on the Modbus network ignore the message if the address field doesn't match their own address.
Modbus message structure Description Address of the receiver Code defining message type Data block with additional information Numeric check value to test for communication errors
Properties of Modbus/ASCII and Modbus/RTU Modbus/ASCII Modbus/RTU ASCII 0...9 and A..F Binary 0...255 Characters LRC Longitudinal Redundancy Check CRC Cyclic Redundancy Check Error check character ':' 3.5 chars silence Frame start characters CR/LF 3.5 chars silence Frame end 1.5 times char length Gaps in message 1 sec Start bit 1 1 Data bits 7 8 even/odd none even/odd none Parity Stop bits 1 2 1 2
Modbus addressing
The first information in each Modbus message is the address of the receiver. This parameter contains one byte of information. In Modbus/ASCII it is coded with two hexadecimal characters, in Modbus/RTU one byte is used. Valid addresses are in the range 0..247. The values 1..247 are assigned to individual Modbus devices and 0 is used as a broadcast address. Messages sent to the latter address will be accepted by all slaves. A slave always responds to a Modbus message. When responding it uses the same address as the master in the request. In this way the master can see that the device is actually responding to the request. Within a Modbus device, the holding registers, inputs and outputs are assigned a number between 1 and 10000. One would expect, that the same addresses are used in the Modbus messages to read or set values. Unfortunately this is not the case. In the Modbus messages addresses are used with a value between 0 and 9999. If you want to read the value of output (coil) 18 for example, you have to specify the value 17 in the Modbus query message. More confusing is even, that for input and holding registers an offset must be substracted from the device address to get the proper address to put in the Modbus message structure. This leads to common mistakes and should be taken care of when designing applications with Modbus. The following table shows the address ranges for coils, inputs and holding registers and the way the address in the Modbus message is calculated given the actual address of the item in the slave device.
Device and Modbus address ranges Device address Modbus address Description Coils (outputs) 110000* address 1 1000120000* address 10001 Inputs 4000150000* address 40001 Holding registers
Common Modbus function codes Code Description Read coil status 01 Read input status 02 Read holding registers 03 Read input registers 04 Force single coil 05 Preset single register 06 Read exception status 07 Force multiple coils 15 Preset multiple registers 16 Report slave ID 17
Byte 1 2 3
Function 01 query structure Value Description 1...247 Slave device address Function code 1 0...255 Starting address, high byte
4 5 6 7(...8)
Starting address, low byte Number of coils, high byte Number of coils, low byte Error check value
When receiving a Modbus query message with function 01, the slave collects the necessary output values and constructs an answer message. The length of this message is dependent on the number of values that have to be returned. In general, when N values are requested, a number of ((N+7) mod 8) bytes are necessary to store these values. The actual number of databytes in the datablock is put in the first byte of the data field. Therefore the general structure of an answer to a Modbus function 01 query is: Function 01 answer structure Byte Value Description 1...247 Slave device address 1 Function code 2 1 0...255 Number of data bytes N 3 0...255 Bit pattern of coil values 4...N+3 N+4(...N+5) LRC/CRC Error check value
Function 02 query structure Byte Value Description 1...247 Slave device address 1 Function code 2 2 0...255 Starting address, high byte 3 0...255 Starting address, low byte 4 0...255 Number of inputs, high byte 5 0...255 Number of inputs, low byte 6 7(...8) LRC/CRC Error check value
After receiving a query message with Modbus function 02, the slave puts the requested input values in a message structure and sends this message back to the Modbus master. The length of the message depends on the number of input values returned. This causes the length of the output
message to vary. The number of databytes in the data field that contain the input values is passed as the first byte in the data field. Each Modbus answering message has the following general structure.
Function 02 answer structure Byte Value Description 1...247 Slave device address 1 Function code 2 2 0...255 Number of data bytes N 3 0...255 Bit pattern of input values 4...N+3 N+4(...N+5) LRC/CRC Error check value
Byte 1 2 3 4 5 6 7(...8)
Function 03 query structure Value Description 1...247 Slave device address Function code 3 0...255 Starting address, high byte 0...255 Starting address, low byte 0...255 Number of registers, high byte 0...255 Number of registers, low byte LRC/CRC Error check value
After processing the query, the Modbus slave returns the 16 bit values of the requested holding registers. Because of the size of the holding registers, every register is coded with two bytes in the answering message. The first data byte contains the high byte, and the second the low byte of the register. The Modbus answer message starts with the slave device address and the function code 03. The next byte is the number of data bytes that follow. This value is two times the number of registers returned. An error check is appended for the host to check if a communication error occured. Literature
- The Modbus technical resources on the Modbus-IDA site are a good starting point Modbus-IDA for those who need the latest information about implementing and using the Modbus interface. Modbus-IDA is the current driving force behind the promotion and implementation of the Modbus protocol.