Introduction to Modbus Protocol
The following only discusses the content of Modbus protocol related to this article.
1.1 Data transmission mode of Modbus protocol
Modbus protocol defines two data transmission modes, namely ASCII mode and RTU mode (Table 1, Table 2). The controller can be set to any of the two transmission modes (ASCII or RTU) to communicate in a standard Modbus network. The user can select the desired mode, including serial port communication parameters (baud rate, verification mode, etc.); when configuring each controller, all devices on a Modbus network must select the same transmission mode and serial port parameters. 1.2 Modbus message frame In both transmission modes (ASCII or RTU), the transmission device converts the Modbus message into a frame with a start and end point, which allows the receiving device to start working at the beginning of the message, read the address allocation information, determine which device is selected (broadcast mode is transmitted to all devices), and determine when the message is completed. 1.2.1 ASCII frame Using ASCII mode, the message starts with a colon (:) character (ASCII code 3AH) and ends with a carriage return line feed character (ASCII code 0DH, 0AH). The characters that can be used for transmission in other fields are hexadecimal 0…9, A…F. Devices on the network continuously detect the ":" character. When a colon is received, each device decodes the next field (address field) to determine whether it is sent to itself. The time interval between characters in the message cannot exceed 1s, otherwise the receiving device will consider the transmission error. A typical message frame is shown in Table 3. Using RTU mode, the message transmission must start with a pause interval of at least 3.5 characters. The first field transmitted is the device address. The characters that can be used for transmission are hexadecimal 0…9, A…F. When the first field (address field) is received, each device decodes to determine whether it is sent to itself. After the last transmission character, a pause of at least 3.5 characters marks the end of the message. A new message can start after this pause. The entire message frame must be transmitted as a continuous stream. If there is a pause of more than 1.5 characters before the frame is completed, the receiving device will refresh the incomplete message and assume that the next byte is the address field of a new message. Similarly, if a new message starts within 3.5 characters of the previous message, the receiving device will think it is a continuation of the previous message. This will cause an error because the value of the CRC field at the end cannot be correct. A typical message frame is shown in Table 4. 1.3 Error detection field 1.3.1 ASCII mode The ASCII mode is selected as the character frame, and the error detection field contains two ASCII characters. This is calculated using the LRC (longitudinal redundancy check) method on the message content, excluding the initial colon and carriage return line feed characters. The LRC character is attached before the carriage return line feed character. 1.3.2 RTU mode The RTU mode is selected as the character frame, and the error detection field contains a 16-bit value (implemented by two 8-bit characters). The content of the error detection field is obtained by performing a cyclic redundancy check on the message content. The CRC field is attached to the end of the message, with the low byte first and the high byte next. Therefore, the CRC high byte is the last byte of the message sent. 1.4 Modbus data verification method 1.4.1 CRC-16 (Cyclic Redundancy Error Check) The redundant cyclic code (CRC) contains 2 bytes, that is, 16 bits of binary. The CRC code is calculated by the sending device and placed at the end of the sent information. The receiving device recalculates the CRC code of the received information and compares whether the calculated CRC code is consistent with the received one. If the two do not match, it indicates an error. Only 8 data bits are used when calculating the CRC code. The start bit, stop bit, and parity bit are not involved in the CRC code calculation. The steps to calculate the CRC code are as follows: ① Preset the 16-bit register to hexadecimal FFFF (that is, all 1). This register is called the CRC register. ② XOR the first 8-bit data with the low bit of the 16-bit CRC register and place the result in the CRC register. ③ Shift the content of the register right by one bit, fill the highest bit with 0, and check the lowest bit. ④ If the lowest bit is 0: repeat step 3 and shift right one bit again; if the lowest bit is 1: perform XOR operation on the CRC register and the polynomial A001 (1010 0000 0000 0001). ⑤ Repeat steps ③ and ④ until the right shift is 8 times, so that the entire 8-bit data is processed. ⑥ Repeat steps ② to ⑤ to process the next 8-bit data. ⑦ The final value of the CRC register is the CRC code. ⑧ Divide the CRC code into the upper 8 bits and the lower 8 bits, and add them to the transmitted data in the order of the lower bits first and the higher bits last. 1.4.2LRC (Longitudinal Redundancy Error Check) LRC error check is used in ASCII mode. This error check code is an 8-bit binary number that can be transmitted as 2 ASCII hexadecimal bytes. When calculating the LRC code, only the device address, function code, and data block bytes participate in the operation, while the colon (:), carriage return symbol (CR), and line feed character (LF) do not participate in the operation. The specific steps to calculate the LRC code are: ① Add all bytes to be calculated, and discard the carry from the sum. ② Invert the 8-bit byte calculated in the previous step or subtract the 8-bit byte from FFH. ③ Add 1 to the inverted value to get the LRC code. A simple way for the receiver to determine whether the received information is correct is to add all bytes except the colon (:), carriage return symbol (CR), and line feed character (LF), including the LRC code, and discard the carry. If the result is 0, it means that the information is transmitted correctly, otherwise it is an error. 2 Implementation method of two checksums of Modbus protocol in LabVIEW 2.1 CRC code implementation method Follow the steps of calculating CRC code in Section 1.4.1 to design the software as shown in Figure 1. 2.2 LRC code implementation method Follow the steps of calculating LRC code in Section 1.4.2 to design the software as shown in Figure 2. LabVIEW is a graphical development language that is easy to understand and has high development efficiency. The implementation program of two checksums of Modbus protocol is well completed. The key is to understand the generation method of CRC code and LRC code. 3 Conclusion Developing the implementation program of two check codes of Modbus protocol in LabVIEW is simple, convenient and efficient. The two implementation programs given in this paper are completed in LabVIEW7.1 and can be applied in the actual measurement and control program based on PC. References [1] MODICON, Inc.. Modbus Protocol Reference Guide [Z]. 1996. [2] Yang Leping, Li Haitao, Xiao Xiangsheng. LabVIEW Program Design and Application [M]. Beijing: Electronic Industry Press, 2001.
Keywords:LabVIEW
Reference address:Implementation methods of two check codes of Modbus protocol based on LabVIEW
The following only discusses the content of Modbus protocol related to this article.
Modbus protocol defines two data transmission modes, namely ASCII mode and RTU mode (Table 1, Table 2). The controller can be set to any of the two transmission modes (ASCII or RTU) to communicate in a standard Modbus network. The user can select the desired mode, including serial port communication parameters (baud rate, verification mode, etc.); when configuring each controller, all devices on a Modbus network must select the same transmission mode and serial port parameters. 1.2 Modbus message frame In both transmission modes (ASCII or RTU), the transmission device converts the Modbus message into a frame with a start and end point, which allows the receiving device to start working at the beginning of the message, read the address allocation information, determine which device is selected (broadcast mode is transmitted to all devices), and determine when the message is completed. 1.2.1 ASCII frame Using ASCII mode, the message starts with a colon (:) character (ASCII code 3AH) and ends with a carriage return line feed character (ASCII code 0DH, 0AH). The characters that can be used for transmission in other fields are hexadecimal 0…9, A…F. Devices on the network continuously detect the ":" character. When a colon is received, each device decodes the next field (address field) to determine whether it is sent to itself. The time interval between characters in the message cannot exceed 1s, otherwise the receiving device will consider the transmission error. A typical message frame is shown in Table 3. Using RTU mode, the message transmission must start with a pause interval of at least 3.5 characters. The first field transmitted is the device address. The characters that can be used for transmission are hexadecimal 0…9, A…F. When the first field (address field) is received, each device decodes to determine whether it is sent to itself. After the last transmission character, a pause of at least 3.5 characters marks the end of the message. A new message can start after this pause. The entire message frame must be transmitted as a continuous stream. If there is a pause of more than 1.5 characters before the frame is completed, the receiving device will refresh the incomplete message and assume that the next byte is the address field of a new message. Similarly, if a new message starts within 3.5 characters of the previous message, the receiving device will think it is a continuation of the previous message. This will cause an error because the value of the CRC field at the end cannot be correct. A typical message frame is shown in Table 4. 1.3 Error detection field 1.3.1 ASCII mode The ASCII mode is selected as the character frame, and the error detection field contains two ASCII characters. This is calculated using the LRC (longitudinal redundancy check) method on the message content, excluding the initial colon and carriage return line feed characters. The LRC character is attached before the carriage return line feed character. 1.3.2 RTU mode The RTU mode is selected as the character frame, and the error detection field contains a 16-bit value (implemented by two 8-bit characters). The content of the error detection field is obtained by performing a cyclic redundancy check on the message content. The CRC field is attached to the end of the message, with the low byte first and the high byte next. Therefore, the CRC high byte is the last byte of the message sent. 1.4 Modbus data verification method 1.4.1 CRC-16 (Cyclic Redundancy Error Check) The redundant cyclic code (CRC) contains 2 bytes, that is, 16 bits of binary. The CRC code is calculated by the sending device and placed at the end of the sent information. The receiving device recalculates the CRC code of the received information and compares whether the calculated CRC code is consistent with the received one. If the two do not match, it indicates an error. Only 8 data bits are used when calculating the CRC code. The start bit, stop bit, and parity bit are not involved in the CRC code calculation. The steps to calculate the CRC code are as follows: ① Preset the 16-bit register to hexadecimal FFFF (that is, all 1). This register is called the CRC register. ② XOR the first 8-bit data with the low bit of the 16-bit CRC register and place the result in the CRC register. ③ Shift the content of the register right by one bit, fill the highest bit with 0, and check the lowest bit. ④ If the lowest bit is 0: repeat step 3 and shift right one bit again; if the lowest bit is 1: perform XOR operation on the CRC register and the polynomial A001 (1010 0000 0000 0001). ⑤ Repeat steps ③ and ④ until the right shift is 8 times, so that the entire 8-bit data is processed. ⑥ Repeat steps ② to ⑤ to process the next 8-bit data. ⑦ The final value of the CRC register is the CRC code. ⑧ Divide the CRC code into the upper 8 bits and the lower 8 bits, and add them to the transmitted data in the order of the lower bits first and the higher bits last. 1.4.2LRC (Longitudinal Redundancy Error Check) LRC error check is used in ASCII mode. This error check code is an 8-bit binary number that can be transmitted as 2 ASCII hexadecimal bytes. When calculating the LRC code, only the device address, function code, and data block bytes participate in the operation, while the colon (:), carriage return symbol (CR), and line feed character (LF) do not participate in the operation. The specific steps to calculate the LRC code are: ① Add all bytes to be calculated, and discard the carry from the sum. ② Invert the 8-bit byte calculated in the previous step or subtract the 8-bit byte from FFH. ③ Add 1 to the inverted value to get the LRC code. A simple way for the receiver to determine whether the received information is correct is to add all bytes except the colon (:), carriage return symbol (CR), and line feed character (LF), including the LRC code, and discard the carry. If the result is 0, it means that the information is transmitted correctly, otherwise it is an error. 2 Implementation method of two checksums of Modbus protocol in LabVIEW 2.1 CRC code implementation method Follow the steps of calculating CRC code in Section 1.4.1 to design the software as shown in Figure 1. 2.2 LRC code implementation method Follow the steps of calculating LRC code in Section 1.4.2 to design the software as shown in Figure 2. LabVIEW is a graphical development language that is easy to understand and has high development efficiency. The implementation program of two checksums of Modbus protocol is well completed. The key is to understand the generation method of CRC code and LRC code. 3 Conclusion Developing the implementation program of two check codes of Modbus protocol in LabVIEW is simple, convenient and efficient. The two implementation programs given in this paper are completed in LabVIEW7.1 and can be applied in the actual measurement and control program based on PC. References [1] MODICON, Inc.. Modbus Protocol Reference Guide [Z]. 1996. [2] Yang Leping, Li Haitao, Xiao Xiangsheng. LabVIEW Program Design and Application [M]. Beijing: Electronic Industry Press, 2001.
Previous article:LabVIEW interface design - Modify the decoration color
Next article:Structure alignment issues encountered using LabVIEW
- Popular Resources
- Popular amplifiers
Recommended Content
Latest Test Measurement Articles
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Seizing the Opportunities in the Chinese Application Market: NI's Challenges and Answers
- Tektronix Launches Breakthrough Power Measurement Tools to Accelerate Innovation as Global Electrification Accelerates
- Not all oscilloscopes are created equal: Why ADCs and low noise floor matter
- Enable TekHSI high-speed interface function to accelerate the remote transmission of waveform data
- How to measure the quality of soft start thyristor
- How to use a multimeter to judge whether a soft starter is good or bad
- What are the advantages and disadvantages of non-contact temperature sensors?
- In what situations are non-contact temperature sensors widely used?
MoreSelected Circuit Diagrams
MorePopular Articles
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
MoreDaily News
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
Guess you like
- ATK-HC05 Bluetooth Serial Port Module
- LIS3DE triaxial accelerometer package and code
- Please help analyze the power circuit
- EEWORLD University Hall - Planting the latest and hottest LED driver products from TI
- [Rawpixel RVB2601 development board trial experience] External IO port chip PCF8574 test
- Share MSP430F149 and PC serial communication programming example
- CC3200 LaunchPad Out of Box Test demos-appliances
- Test category top
- Technical article: Why does Ka-band need more bandwidth?
- Online weather clock based on ESP32