Views: 0 Author: Site Editor Publish Time: 2023-12-05 Origin: Site
Modbus protocol is a universal language applied to electronic controllers.
Through this protocol, controllers can communicate with each other, through networks such as Ethernet, and other devices, which has become an industry standard. With it, control equipment produced by different manufacturers can be connected to an industrial network for centralized monitoring.
This protocol defines a type of controller that can recognize the data structures used, regardless of the network they communicate through.
It describes the process of a controller requesting access to other devices, how to respond to requests from other devices, and how to detect error records.
It establishes a common format for the pattern and content of communication data.
When conducting multi machine communication, the Modbus protocol stipulates that each controller must know their device address, recognize the data sent according to the address, decide whether to generate an action, and what action to generate. If a response is required, the controller will send the generated feedback information using the Modbus protocol.
The Modbus protocol allows for simple communication within various network architectures, with each device (PLC, human-machine interface, control)
Modbus protocol can be used to initiate remote operations for control panels, drivers, input/output devices, etc. Some gateways allow communication between several buses or networks that use Modbus protocol, as shown in the following figure.
RTU protocol frame data
Modbus has two communication transmission modes, one is ASCII mode and the other is RTU mode. Due to the fact that the data bytes in ASCII mode are 7 bit data bits, which cannot be implemented by 51 microcontrollers, and there are relatively few practical applications, we only use RTU mode here. The two modes are similar, and if one is used, the other will be used. A typical RTU data frame is shown in the following:
Start bit | Device Address | Function Code | Data | CRC Check | End |
T1-T2-T3-T4 | 8 Bit | 8 Bit | n*8 Bit | 16 Bit | T1-T2-T3-T4 |
The Modbus communication protocol is when the host issues different instructions, and the slave executes different operations based on the judgment of the instructions. Due to the fact that our development board does not have as many corresponding functions as Modbus function codes, we have defined an array called regGroup in the program, which is equivalent to 5 registers.
In addition, there is defined a 6th register to control the buzzer. By issuing different instructions, we can change the data of the register group or change the switch status of the buzzer. In the Modbus protocol, the address and value of the register are both 16 bits, which is 2 bytes. By default, the high byte is 0x00, and the low byte is the value corresponding to the array regGroup. The addresses 0x0000 to 0x0004 correspond to the elements in the regGroup array. When we write them, the numbers are displayed on the 1602 LCD. For the address 0x0005, if we write it to 0x00, the buzzer will not sound.
If write any other values, the buzzer will sound an alarm. The main job of our microcontroller is to parse the data received by the serial port and perform different operations.