Implementation methods of two check codes of Modbus protocol based on LabVIEW

Publisher:RadiantBlossomLatest update time:2015-10-26 Source: eefocusKeywords:LabVIEW Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
 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.
   Implementation methods of two check codes of Modbus protocol based on LabVIEW
  





    Implementation methods of two check codes of Modbus protocol based on LabVIEW


   Implementation methods of two check codes of Modbus protocol based on LabVIEW
 
 

 

 
 











  





 

 

Implementation methods of two check codes of Modbus protocol based on LabVIEW
 

Implementation methods of two check codes of Modbus protocol based on LabVIEW

 





Keywords:LabVIEW Reference address:Implementation methods of two check codes of Modbus protocol based on LabVIEW

Previous article:LabVIEW interface design - Modify the decoration color
Next article:Structure alignment issues encountered using LabVIEW

Latest Test Measurement Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号