"Transmission mode" of Modbus communication protocol

Source: Internet
Author: User

1. Two modes of transmission

The controller can be set to two modes of transmission (ASCII or RTU) in any one of the standard Modbus network communications. Users choose the desired mode, including serial communication parameters (baud rate, check mode, etc.), in the configuration of each controller, all devices on a Modbus network must choose the same transmission mode and serial port parameters.

The selected ASCII or RTU mode only applies to the standard Modbus network, which defines each bit of the message segment that is continuously transmitted on those networks, and determines how the information is packaged into a message domain and how it is decoded.

Modbus messages on other networks (like map and Modbus Plus) are transferred to frames unrelated to the serial transfer.

  

ASCII mode

: Address function code Number of data Data 1 ... Data n LRC High-byte LRC Low byte Enter Line break

RTU mode

Address function code Number of data Data 1 ... Data n CRC Low Byte CRC High Byte

(1) ASCII mode

When the controller is set to communicate in ASCII (US standard Information Interchange Code) mode on the Modbus network, each 8Bit byte in the message is sent as two ASCII characters. The main advantage of this approach is that the character can be sent at a time interval of 1 seconds without generating an error.

Code system

Hexadecimal, ASCII character 0 ... 9,a ... F

Each ASCII character in the message is made up of a hexadecimal character

 Bits per byte

    • 1 Starting Bits
    • 7 data bits, the smallest significant bit is sent first
    • 1 parity bits, no checksum
    • 1 stop bit (with checksum), 2 bit (no calibration)

 Error detection Domain

LRC (longitudinal lengthy inspection)

(2) RTU mode

When the controller is set to communicate in RTU (remote terminal Unit) mode on the Modbus network, each 8Bit byte in the message contains two hexadecimal characters of 4Bit. The main advantage of this approach is that, at the same baud rate, more data can be transmitted than the ASCII method.

Code system

8-bit binary, hexadecimal number 0 ... 9,a ... F

Each 8-bit field in the message is made up of two hexadecimal characters

Bits per byte

    • 1 Starting Bits
    • 8 data bits, the smallest significant bit is sent first
    • 1 parity bits, no checksum
    • 1 stop bit (with checksum), 2 bit (no calibration)

Error detection Domain

CRC (cyclic verbose detection)

2. Modbus Message Frame

(1) ASCII Frames

Using ASCII mode, the message starts with a colon (:) character (ASCII 3AH), ending with a carriage return line break (ASCII 0dh,0ah).

Other domains can use the transmission characters that are hexadecimal 0 ... 9,a ... F. Devices on the network constantly detect " : " characters, and when a colon is received, each device decodes the next domain (address field) to determine whether it is sent to itself.

The time interval between characters in a message cannot be longer than 1 seconds, or the receiving device will be considered a transmission error. A typical message frame is as follows:

Starting bit Device Address function code Data LRC Verification Terminator
1 characters 2 characters 2 characters n Characters 2 characters 2 characters

(2) RTU frame

With RTU mode, the message is sent at least at a 3.5-character-time pause interval. In the network baud rate under a variety of character time, which is easiest to implement (as shown in T1-T2-T3-T4). The first domain that is transferred is the device address. The transmission characters that can be used are hex 0 ... 9,a ... F. Network devices constantly detect the network bus, including the time between pauses. When the first domain (address field) is received, each device is decoded to determine if it is destined for itself. After the last transmission character, a pause of at least 3.5 character time is calibrated to the end of the message. A new message can begin after this pause.

The entire message frame must be lost as a continuous flow. If there is a pause time of more than 1.5 characters before the frame completes, the receiving device refreshes the incomplete message and assumes that the next byte is the Address field of a new message. Similarly, if a new message starts in less than 3.5 character time and then the previous message begins, the receiving device considers it to be a continuation of the previous message. This will result in an error because the value in the last CRC field cannot be correct. A typical message frame is as follows:

Starting bit Device Address function code Data CRC check Terminator
T1-t2-t3-t4 8Bit 8Bit n x 8Bit 16Bit T1-t2-t3-t4

3. Address domain

The Address field of the message frame contains two characters (ASCII) or 8Bit(RTU). Possible from the device address is 0 ... 247 (decimal). The address range for a single device is 1 ... 247. The master device is selected through the Address field in the message from the address of the device that will be contacted. When a response message is sent from the device, it puts its own address in the address field of the response so that the master device knows which device is responding.

Address 0 is used as a broadcast address so that all the devices can be recognized. When the Modbus protocol is used for higher-level networks, broadcasts may not be allowed or otherwise replaced.

4. Functional domain

The function Code field in the message frame contains two characters (ASCII) or 8Bits(RTU). The possible code range is 1 decimal ... 255. Of course, some of the code is for all controllers, there are some controllers that are applied to it, and some are reserved for later use.

When a message is sent from the main device to the slave device, the feature code domain tells you what behavior to perform from the device. For example, to read the input switch state, read the data contents of a set of registers, read the diagnostic status from the device, allow to transfer, record, check the program from the device.

When responding from a device, it uses a functional code field to indicate whether it is a normal response (error) or if there is some kind of error (called an objection response). For a normal response, the device responds only to the corresponding function code. For the objection response, return a code equivalent to the normal code from the device, but the most important position is logic 1.

For example: A message from the main device to the slave device requires reading a set of hold registers, which will produce the following function code: 0 0 0 0 0 0 1 1 (hex 03H)

For normal response, only the same function code is responded to from the device. Responding to objections, it returns: 1 0 0 0 0 0 1 1 (hex 83H)

In addition to the function code modified by the objection error, the device puts a unique code into the data field of the response message, which tells the main device what error occurred.

After the main device application gets an objection to the response, the typical process is to re-send the message, or to diagnose the message sent from the device and report it to the operator.

5. Data domain

Data fields are composed of two hexadecimal number sets, Range 00 ... Ff. Depending on the network transfer mode, this can consist of a pair of ASCII characters or an RTU character.

The data field that is sent from the main device message from the device contains additional information: The device must be used to perform the execution defined by the function code. This includes such as a discontinuous register address, the number of entries to be processed, and the actual number of bytes of data in the domain.

For example, if the primary device needs to read a set of hold registers (function code 03) from the device, the data field specifies the starting register and the number of registers to read. If the main device writes a set of registers from the device (function code 106), the data field indicates the starting register to write and the number of registers to write, the number of data bytes in the data field, and the data to be written to the register.

If no error occurs, the data field returned from the device contains the requested data. If an error occurs, this field contains an objection code that the master device application can use to determine the next action to take.

In some messages, the data field can be nonexistent (0 length). For example, the master device requires that the Communication event record (function code 0B hex) from the device be responded to, and no additional information is required from the device.

6. Error detection Domain

The standard Modbus network has two methods of error detection. The contents of the error detection domain are dependent on the detection method selected.

(1) ASCII

When ASCII mode is used for word frame, the error detection field contains two ASCII characters. This is calculated using the LRC (longitudinal verbose detection) method for the content of the message, excluding the starting colon character and carriage return line feed. The LRC character is appended to the carriage return line feed.

(2) RTU

When using RTU mode for Word frame, the error detection domain contains a 16Bits value (implemented with two 8-bit characters). The content of the error detection domain is obtained by looping the content of the message through a lengthy loop detection method. The CRC domain is appended to the end of the message and is added first in low-byte and then high-byte. So the high byte of the CRC is the last byte of the message being sent.

(3) Error detection method

The standard Modbus serial network uses two methods of error detection. Parity is available for each character, and frame detection (LRC or CRC) is applied to the entire message. They are all generated by the main device before the message is sent, and each character and the entire message frame are detected by the device during the reception process.

The user configures a pre-defined time-out interval for the primary device, which is long enough to allow any slave device to function as a normal response. If a transmission error is detected from the device, the message will not be received and will not respond to the primary device. Such a timeout event will trigger the main device to handle the error. A timeout is also generated for addresses that are destined for nonexistent slave devices.

① parity Check

The user can configure the controller to be either odd or even parity, or no checksum. This determines how the parity bits in each character are set.

If you specify odd or even parity, the number of bits for "1" is counted in bits per character (ASCII mode 7 data bits, 8 data bits in RTU). For example, the RTU character frame contains the following 8 data bits: 1 1 0 0 0 1 0 1

The total number of "1" is 4. If the parity is used, the parity bit of the frame will be 0, and the total number of "1" is still 4. If the odd check is used, the parity bit of the frame will be 1, so the total number of "1" is 5.

If no parity bit is specified, no parity bit is transmitted and no checksum is detected. Fills in the character frame to be transferred instead of an additional stop bit.

②LRC Detection

Using ASCII mode, the message includes an error detection domain based on the LRC method. The LRC domain detects the contents of the Message field in addition to the beginning colon and the end of the carriage return.

The LRC field is a byte that contains a 8-bit binary value. The LRC value is computed by the transport device and placed in the message frame, and the receiving device computes the LRC during the receipt of the message and compares it to the value in the LRC domain received in the message, indicating an error if the two values are not equal.

The LRC method is to accumulate the bytes of 8Bit in the message continuously, discarding the carry.

The LRC simple functions are as follows:

StaticUnsignedCharLRC (auchmsg,usdatalen) unsignedChar*auchmsg;/*the message to be evaluated*/unsigned ShortUsdatalen;/*number of bytes to be processed by the LRC*/{unsignedCharUCHLRC =0;/*LRC byte initialization*/ while(usdatalen--)/*Send Message*/UCHLRC+ = *auchmsg++;/*Accumulate*/return((unsignedChar)(-((CHAR_UCHLRC)));}
LRC Simple function

③CRC detection

Using RTU mode, the message includes an error detection domain based on the CRC method. The CRC domain detects the contents of the entire message.

The CRC field is two bytes and contains a 116-bit binary value. It is calculated by the transport device and added to the message. The receiving device recalculates the CRC of the received message and compares it to the value in the received CRC domain, if the two values are different.

A CRC is a 16-bit register that first calls into a value that is full "1", and then invokes a procedure that processes the values in the current registers of successive 8-bit bytes in the message. Only 8Bit data in each character is valid for the CRC, and the start and stop bits and parity bits are not valid.

In the process of CRC generation, each 8-bit character nonalphanumeric is separated from the Register content or (or), the result moves toward the least significant bit direction, the most significant bit is filled with 0. The LSB is extracted to detect if the LSB is 1, the register is separate and the preset value or a bit, if the LSB is 0, does not proceed. The whole process should be repeated 8 times. After the last (8th bit) completion, the next 8 bytes are separated from the current value of the register or both. The value in the final register, which is the CRC value after all the bytes in the message have been executed.

When the CRC is added to the message, the low byte is first added, and then the high byte.

7. Continuous transmission of characters

When messages are transmitted in the standard Modbus series network, each character or byte is sent as follows (from left to right):

Least significant bit ... Most significant bit

When using ASCII word frame, the sequence of bits is:

There are parity checks

Start position 1 2 3 4 5 6 7 Odd and even bit Stop bit

No parity check

Start position 1 2 3 4 5 6 7 Stop bit Stop bit

Figure 4. Bit order (ASCII)

When using the RTU character frame, the sequence of bits is:

There are parity checks

Start position 1 2 3 4 5 6 7 8 Odd and even bit Stop bit

No parity check

Start position 1 2 3 4 5 6 7 8 Stop bit Stop bit

Figure 4. Bit order (RTU)

"Transmission mode" of Modbus communication protocol

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.