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

Publisher:MusicLover123Latest update time:2011-11-30 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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

This paper introduces the implementation method of two check codes of Modbus protocol based on LabVIEW. This method can be applied in the measurement and control system based on PC.
Keywords: Modbus protocol; LabVIEW; CRC check; LRC check

The following only discusses the content of the Modbus protocol related to this article.
1.1 Data transmission mode of the Modbus protocol
The Modbus protocol defines two data transmission modes, namely ASCII mode and RTU mode (Table 1, Table 2). The controller can be set to either of the two transmission modes (ASCII or RTU) to communicate in a standard Modbus network. The user can select the desired mode, including serial communication parameters (baud rate, verification method, etc.); when configuring each controller, all devices on a Modbus network must select the same transmission mode and serial port parameters.
?
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 transmission characters that can be used in other fields are hexadecimal 0...9, A...F. The 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 think that the transmission is wrong. A typical message frame is shown in Table 3.
?
?
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.2RTU 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, first the low byte and then the high byte. Therefore, the CRC high byte is the last byte of the sent message.
1.4Modbus data verification method
1.4.1CRC-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 device receiving the information recalculates the CRC code of the received information and compares whether the calculated CRC code matches 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:
① 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 put the result in the CRC register.
③ Shift the contents 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 by one bit again; if the lowest bit is 1: XOR the CRC register with 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 value of the CRC register obtained at the end is the CRC code.
⑧ Divide the CRC code into high 8 bits and low 8 bits, and add them to the transmitted data in the order of low bits first and high 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 calculation, while the colon (:), carriage return symbol (CR), and line feed character (LF) do not participate in the calculation. 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. ?
The software design is performed according to the steps of calculating the CRC code in Section 1.4.1 as shown in Figure 1.
Reference address:Implementation methods of two check codes of Modbus protocol based on LabVIEW

Previous article:Development of CO2 Arc Welding Analyzer Based on Virtual Instrument
Next article:LabVIEW Programming Real-time Control of KEITHLEY 6517A Electrometer

Latest Analog Electronics 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号