Modbus - Manual
Modbus - Manual
Modbus - Manual
Table of Contents
Table of Contents.................................................................................................................2 Chapter 1 Modbus Protocol.................................................................................................3 1.1 Introducing Modbus Protocol ....................................................................................3 1.2 Two Serial Transmission Modes ...............................................................................5 1.3 Modbus Message Framing.........................................................................................7 1.4 Error Checking Methods .........................................................................................10 Chapter 2 Data and Control Functions ..............................................................................13 2.1 Modbus Function Formats.......................................................................................13 Chapter 4 Exception Responses ........................................................................................18 4.1 Exception Responses ...............................................................................................18 4.2 Exception Codes ......................................................................................................20 Chapter 6 LRC / CRC Generation.....................................................................................21 6.1 LRC Generation.......................................................................................................21 6.2 CRC Generation ......................................................................................................22
On these networks, the controllers communicate using a peer-to-peer technique, in which any controller can initiate transactions with the other controllers. Thus a controller may operate either as a slave or as a master in separate transactions. Multiple internal paths are frequently provided to allow concurrent processing of master and slave transactions. At the message level, the Modbus protocol still applies the master-slave principle even though the network communication method is peer-to-peer. If a controller originates a message, it does so as a master device, and expects a response from a slave device. Similarly, when a controller receives a message it constructs a slave response and returns it to the originating controller.
The Response
If the slave makes a normal response, the function code in the response is an echo of the function code in the query. 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.
The selection of ASCII or RTU mode pertains only to standard Modbus networks. It defines the bit contents of message fields transmitted serially on those networks. It determines how information will be packed into the message fields and decoded. On other networks like MAP and Modbus Plus, Modbus messages are placed into frames that are not related to serial tranasmission. For example, a request to read holding registers can be handled between two controllers on Modbus Plus without regard to the current setup of either controller's serial Modbus port.
Coding System
V Hexadecimal, ASCII characters 0 ... 9, A ... F V One hexadecimal character contained in each ASCII character of the message
Coding System
V Eight-bit binary, hexadecimal 0 ... 9, A ... F V Two hexadecimal characters contained in each eight-bit field of the message
Following the last transmitted character, a similar interval of at least 3.5 character times marks the end of the message. A new message can begin after this interval. The entire message frame must be transmitted as a continuous stream. If a silent interval of more than 1.5 character times occurs before completion of the frame, the receiving device flushes the incomplete message and assumes that the next byte will be the address field of a new message. 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.
Figure 4 RTU Message Frame 1.3.3 How the Address Field is Handled
The address field of a message frame contains two characters (ASCII) or eight bits (RTU). Valid slave device addresses are in the range of 0 ... 247 decimal. The individual slave devices are assigned addresses in the range of 1 ... 247. A master addresses a slave by placing the slave address in the address field of the message. When the slave sends its response, it places its own address in this address field of the response to let the master know which slave is responding. Address 0 is used for the broadcast address, which all slave devices recognize. When Modbus protocol is used on higher level networks, broadcasts may not be allowed or may be replaced by other methods. For example, Modbus Plus uses a shared global database that can be updated with each token rotation.
If the slave device takes the requested action without error, it returns the same code in its response. If an exception occurs, it returns:
1000 0011 (Hexadecimal 83)
In addition to its modification of the function code for an exception response, the slave places a unique code into the data field of the response message. This tells the master what kind of error occurred, or the reason for the exception. The master device's application program has the responsibility of handling exception responses. Typical processes are to post subsequent retries of the message, to try diagnostic messages to the slave, and to notify operators.
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 loworder 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. Additional information about error checking is contained later in this chapter. Detailed steps for generating LRC and CRC fields can be found in Chapter .
Figure 5 Bit Order (ASCII) With RTU character framing, the bit sequence is:
10
Note: A message addressed to a nonexistent slave device will also cause a timeout. Other networks such as MAP or Modbus Plus use frame checking at a level above the Modbus contents of the message. On those networks, the Modbus message LRC or CRC check field does not apply. In the case of a transmission error, the communication protocols specific to those networks notify the originating device that an error has occurred, and allow it to retry or abort according to how it has been setup. If the message is delivered, but the slave device cannot respond, a timeout error can occur which can be detected by the master's program.
The total quantity of 1 bits in the frame is four. If Even Parity is used, the frame's parity bit will be a 0, making the total quantity of 1 bits still an even number (four). If Odd Parity is used, the parity bit will be a 1, making an odd quantity (five). When the message is transmitted, the parity bit is calculated and applied to the frame of each character. The receiving device counts the quantity of 1 bits and sets an error if they are not the same as configured for that device (all devices on the Modbus network must be configured to use the same parity check method). Note that parity checking can only detect an error if an odd number of bits are picked up or dropped in a character frame during transmission. For example, if Odd Parity checking is employed, and two 1 bits are dropped from a character containing three 1 bits, the result is still an odd count of 1 bits. If No Parity checking is specified, no parity bit is transmitted and no parity check can be made. An additional stop bit is transmitted to fill out the character frame.
11
In ladder logic, the CKSM function calculates a LRC from the message contents. For applications using host computers, a detailed example of LRC generation is contained in Appendix C.
12
2.1.2 Field Contents in Modbus Messages The following tables show examples of a Modbus query and normal response. Both examples show the field contents in hexadecimal, and also show how a message could be framed in ASCII or in RTU mode.
13
Query
Response
Note: The message specifies the starting register address as 0107 (006B hex). The slave response echoes the function code, indicating this is a normal response. The Byte Count field specifies how many eight-bit data items are being returned. It shows the count of eight-bit bytes to follow in the data, for either ASCII or RTU. With ASCII, this value is half the actual count of ASCII characters in the data. In ASCII, each four-bit hexadecimal value requires one ASCII character, therefore two ASCII characters must follow in the message to contain each eight-bit data item. For example, the value 63 hex is sent as one eight-bit byte in RTU mode (01100011). The same value sent in ASCII mode requires two bytes, for ASCII 6 (0110110) and 3 (0110011). The Byte Count field counts this data as one eight-bit item, regardless of the character framing method (ASCII or RTU).
14
2.2.3 03 Read Holding Registers Reads the binary contents of holding registers (4x references) in the slave. Broadcast is not supported. The maximum parameters supported by various controller models are listed on page .
Query
The query message specifies the starting register and quantity of registers to be read. Registers are addressed starting at zero- registers 1 ... 16 are addressed as 0 ... 15. Here is an example of a request to read registers 40108 ... 40110 from slave device 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. Data is scanned in the slave at the rate of 125 registers per scan for 984-X8X controllers (984-685, etc), and at the rate of 32 registers per scan for all other controllers. The response is returned when the data is completely assembled. Here is an example of a response to the query:
15
The contents of register 40108 are shown as the two byte values of 02 2B hex, or 555 decimal. The contents of registers 40109 ... 40110 are 00 00 and 00 64 hex, or 0 and 100 decimal.
Note: The function will override the controller's memory protect state and the coil's disable state. The forced state will remain valid until the controller's logic next solves the coil. The coil will remain forced if it is not programmed in the controller's logic.
Query
The query message specifies the coil reference to be forced. Coils are addressed starting at zero-coil 1 is addressed as 0. The reguested ON / OFF state is specified by a constant in the query 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. Here is an example of a request to force coil 173 ON in slave device 17:
Response The normal response is an echo of the query, returned after the coil state has been forced. Here is an example of a response to the query:
16
Note: The function will override the controller's memory protect state. The preset value will remain valid in the register until the controller's logic next solves the register contents. The register's value will remain if it is not programmed in the controller's logic.
Query
The query message specifies the register reference to be preset. Registers are addressed starting at zeroregister 1 is addressed as 0. The reguested preset value is specified in the query data field. M84 and 484 controllers use a 10-bit binary value, with the six high order bits set to zeros. All other controllers use 16-bit values. Here is an example of a request to preset register 40002 to 00 03 hex in slave device 17:
Response
The normal response is an echo of the query, returned after the register contents have been preset. Here is an example of a response to the query:
17
18
In this example, the master addresses a query to slave device 10 (0A hex). The function code (01) is for a Read Coil Status operation. It requests the status of the coil at address 1245 (04A1 hex).
Note: Only one coil is to be read, as specified by the number of coils field (0001). If the coil address is nonexistent in the slave device, the slave will return the exception response with the exception code shown (02). This specifies an illegal data address for the slave. For example, if the slave is a 984-385 with 512 coils, this code would be returned.
19
20
Generating an LRC
Step 1 Add all bytes in the message, excluding the starting colon and ending CRLF. Add them into an eight-bit field, so that carries will be discarded. Step 2 Subtract the final field value from FF hex (all 1's), to produce the ones-complement. Step 3 Add 1 to produce the two's-complement. Placing the LRC into the Message When the the eight-bit LRC (two ASCII characters) is transmitted in the message, the high order character will be transmitted first, followed by the low order character-e.g., if the LRC value is 61 hex (0110 0001):
21
22
Result The final contents of the CRC register is the CRC value. Step 7 When the CRC is placed into the message, its upper and lower bytes must be swapped as described below.
Note: This function performs the swapping of the high/low CRC bytes internally. The bytes are already swapped in the CRC value that is returned from the function. Therefore the CRC value returned from the function can be directly placed into the message for transmission. The function takes two arguments:
unsigned char *puchMsg ; A pointer to the message buffer containing binary data to be used for generating the CRC unsigned short usDataLen ; The quantity of bytes in the message buffer.
23
while (usDataLen--) /* pass through message buffer */ { uIndex = uchCRCHi ^ *puchMsgg++ ; /* calculate the CRC */ uchCRCHi = uchCRCLo ^ auchCRCHi[uIndex} ; uchCRCLo = auchCRCLo[uIndex] ; } return (uchCRCHi << 8 | uchCRCLo) ; }
24
25