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
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.
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.
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. ?
Previous article:Development of CO2 Arc Welding Analyzer Based on Virtual Instrument
Next article:LabVIEW Programming Real-time Control of KEITHLEY 6517A Electrometer
- Popular Resources
- Popular amplifiers
- High signal-to-noise ratio MEMS microphone drives artificial intelligence interaction
- Advantages of using a differential-to-single-ended RF amplifier in a transmit signal chain design
- ON Semiconductor CEO Appears at Munich Electronica Show and Launches Treo Platform
- ON Semiconductor Launches Industry-Leading Analog and Mixed-Signal Platform
- Analog Devices ADAQ7767-1 μModule DAQ Solution for Rapid Development of Precision Data Acquisition Systems Now Available at Mouser
- Domestic high-precision, high-speed ADC chips are on the rise
- Microcontrollers that combine Hi-Fi, intelligence and USB multi-channel features – ushering in a new era of digital audio
- Using capacitive PGA, Naxin Micro launches high-precision multi-channel 24/16-bit Δ-Σ ADC
- Fully Differential Amplifier Provides High Voltage, Low Noise Signals for Precision Data Acquisition Signal Chain
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- GigaDevice GD32307E-START development board recommendation
- SIMterix-Simplis~6~
- Experience in using MQTT protocol
- AD13 duplication pad package problem
- SSD1306 Chinese character mobile demonstration
- Using Low Noise Modules in Satellite Applications
- Some misunderstandings in FPGA learning.zip
- MSP430F5438A supports communication scheme verification based on COAP protocol
- I want to design a sine wave generator.
- [RT-Thread reading notes] Part 1 Simple principles of the kernel