Technical characteristics of MODBUS bus control system
MODBUS communication protocol is an industrial fieldbus communication protocol that defines an information frame structure that device controllers can recognize and use, independent of physical layer media, and can be carried in multiple network types.
The MODBUS protocol defines communication participants as "Master" and "Slave", and data and information communication follows a master/slave mode. When applied to a standard MODBUS network, information is directly transmitted.
The various intelligent devices in the MODBUS bus network are connected through an asynchronous serial bus, allowing only one controller to act as the master station and the remaining intelligent devices to act as slave stations.
Using command/response communication, the master station sends requests, the slave station responds to the requests and sends back data or status information, and the slave station cannot send information on its own.
The various information frame formats defined by the MODBUS protocol describe the process of the master controller accessing the slave devices, specify how the slave responds, and check and report transmission errors. Each slave device in the network must be assigned a unique address, and only slave devices that meet the address requirements will respond to commands issued by the master device.
Due to the low development cost, simplicity, and ease of use of the MODBUS bus system, as well as the availability of many industrial control devices PLC、 The frequency converter, display screen, etc. all have MODBUS communication interfaces, so it has become a recognized communication standard.
Through the MODBUS bus, it is easy to connect control devices produced by different manufacturers into an industrial network for centralized monitoring.
MODBUS was originally designed for PLC communication, which enables information exchange between PLC and the outside world through 24 bus commands. The communication functions corresponding to these bus commands mainly include data transmission of AI/AO and DI/DO.
But not many MODBUS devices only use a few commands for control and do not respond to the rest of the commands.
MODBUS communication format
The MODBUS protocol defines two transmission modes, namely RTU (Remote Terminal Unit) and ASCII.
In RTU mode, 1 byte of information is sent as an 8-bit character, while in ASCII mode it is sent as two ASCII characters. For example, when sending the character "20", it is "00100000" in RTU mode, while in ASCII mode it is "00110010"+"00110000" (the "2" and "0" of ASCII characters).
It can be seen that when sending the same data, the efficiency of RTU mode is approximately twice that of ASCII mode.
Generally speaking, when the amount of data is small and mainly text, ASCII is used; When there is a large amount of communication data and it is binary values, RTU mode is often used.
The master station can send communication requests (or instructions) to one or all slave stations at once, and the master device selects the slave devices through the address field of the message frame.
The content and sequence of message frames sent by the master station are: slave station address, function code, data domain (data starting address, data volume, data content), CRC check code; The content and sequence of the information replied by the slave station are basically the same as the information frame of the master station.
In addition to defining communication function codes, MODBUS also defines error codes to indicate error messages. After receiving the error code, the main station will
Take corresponding measures for the cause of the error. The data content of the slave station response is based on the function code, for example, function code 03 requires reading the content of the register held in the slave station device.
Implementation of CRC Check
In the RTU mode of MODBUS communication, the last two bytes of the information frame are designated for transmitting CRC (Cyclic Redundancy Check) codes.
The sender shifts all bytes in the address field, function code, and data field of the information frame according to the specified method and performs XOR calculation to obtain a 2-byte CRC code. The information frame containing the CRC check code is transmitted as a continuous stream.
When the receiver receives the information frame, it calculates in the same way and compares the result with the double byte of the received CRC code. If they match, it is considered that the communication is correct. Otherwise, it is considered that the communication is incorrect, and the slave station will send a CRC error response.
The RTU mode generally adopts the CRC-16 redundancy check method, and the CRC-16 check code is 16 bits (2 bytes), with the low byte at the beginning and the high byte at the end.
There are two methods to implement CRC check: calculating according to the definition formula of CRC check, or establishing a CRC check value table in the program.
Using the former in the program is easier to implement, but here we need to use CRC to generate polynomial X16+X15+X2+1.
The code group coefficient corresponding to this polynomial is 18005H (hexadecimal). After removing the highest bit, the corresponding 16 bit remainder is 8005H, which is the CRC-16 constant.
The CRC-16 verification process is as follows:
Preset each bit of the CRC register to 1; XOR the register value with 8-bit information frame data and store the result in the register; Shift the CRC register from high to low, zero padding at the highest bit (MSB) position, while the lowest bit (LSB) has already been shifted after shifting
If the CRC register is set to 1, XOR the register with the CRC-16 constant. Otherwise, if the LSB is zero, XOR is not required. Repeat the above shift from high to low 8 times. After processing the first 8-bit data, XOR the value of the register with the next 8-bit data and perform the same 8 shifts as before.
After all character processing is completed, the value in the CRC register is the final CRC value. When adding CRC to a message, the low byte is added first, followed by the high byte.
Link characteristics
The physical layer of the MODBUS standard can use RS-232 serial communication, but RS-422 or RS-485 is often used instead in long-distance communication. In multi-point communication, only RS-485 is used, so the MODBUS system in RTU mode uses shielded twisted pair cables, with a communication distance of up to 1000m. A maximum of 31 slave devices can be configured on one bus.
The information exchange on the transmission line is half duplex, which means that only one device is allowed to send information at a time. The master station waits for a response from the slave station before sending the next instruction, thus avoiding line conflicts.
The transmission format of RTU mode is 1 data bit, 2 stop bits, and no parity bits. The security of communication data is ensured by the control parameter CRC-16 code. The RTU receiving device determines the start of a frame based on the time elapsed between received characters. If there are still no new characters or the frame is not completed after 3 and a half character times, the receiving device will abandon the frame and set the next character as the start of a new frame.
发表回复