k8¿­·¢ÌìÉúÓ®¼ÒÒ»´¥¼´·¢

Ê×Ò³
²úÆ·
׿Խ ? Ò×Óà ? ¿É¿¿
ËÑË÷
ÀúÊ·ËÑË÷
ËÑË÷·¢ÏÖ
HOME > NEWS > Industry news

Application of ModbusRTU communication protocol in intelligent instrument

2022-03-07 17:36:41| Source£ºÍøÂç| views:| 0

¡¡¡¡introduction

¡¡¡¡With the progress of the times, more and more enterprises begin to change to production and management automation, and all kinds of intelligent instruments are constantly applied to all fields of production and life. In the aspect of industrial control, RS-485 bus is very suitable for industrial multi machine communication system because of its balanced differential transmission characteristics, strong anti-interference, long transmission distance and large cascade capacity. ModbusRTU communication protocol is one of the mainstream communication protocols widely used in international intelligent instruments. Today, when various industrial instruments are widely used, ModbusRTU communication protocol and RS-485 bus have been widely used. This paper mainly introduces the specific scheme of realizing this technology in industrial control system from the perspective of application.


¡¡¡¡1. Modbus RTU communication protocol

¡¡¡¡Modbus RTU is a communication protocol developed by Modicon company. It works in the master-slave response mode, and its specification has been published on the Internet. It is a truly open standard network communication protocol widely used in the industrial field. Because it is free of license fees, easy to integrate different equipment, simple and easy to use, low development cost and extensive knowledge resource support, it has become a recognized general industrial standard. With this standard, the control equipment produced by different manufacturers can be connected into an industrial network for centralized monitoring. At present, many domestic manufacturers have followed the agreement standard in their products and systems. Modbus mingu protocol has two transmission modes, namely RTU mode and ASCII mode. The 8bit data in RTU mode information frame includes two 4-bit hexadecimal characters. Compared with ASCII mode, RTU mode needs less bits to express the same information, and has greater data flow at the same communication rate. Therefore, generally, the general industrial intelligent instruments adopt the Modbus protocol of RTU mode.

¡¡¡¡2. Communication information transmission process

¡¡¡¡The information transmission is asynchronous and in bytes. The communication information transmitted between master station and slave station is in 11 bit word format, as shown in Table 1.

¡¡¡¡361.jpg

The information frame format III of communication data is shown in Table 2.¡¡¡¡

362.jpg

¡¡¡¡Table 2 data format

¡¡¡¡When the transmitting device (host) sends a communication command to the receiving device (slave), the slave that meets the corresponding address code receives the communication command and reads the information according to the function code and relevant requirements. If the CRC check is correct, execute the corresponding task, and then return the execution result to the host. The function code in the CRC and the returned data include the function code in the CRC. If the CRC check is wrong, no information will be returned. The address code is the first byte of each communication information frame (8 bits: 0 ~ 255). This byte indicates that the slave with the address set by the user will receive the information sent by the host. Each slave must have a unique address code, and only the slave that meets the address code can respond to the loopback information. When the slave returns information, the returned data starts with their respective address codes. The function code is the second word transmitted in each communication information frame

¡¡¡¡Festival. The function codes that can be defined by Modbus communication protocol are 1 ~ 127.

¡¡¡¡It is sent as a master request and tells the slave what action to perform through the function code. As a slave response, the function code returned by the slave is the same as the function code sent by the host, indicating that the slave has responded to the host and has carried out relevant operations. Part of Modbus function codes are shown in Table 3.

363.jpg

¡¡¡¡Table 3 partial MODBUS function codes

¡¡¡¡The data area includes what information needs to be returned or what actions need to be performed by the slave. This information can be data, reference address, etc. For example, if the master tells the slave to return the value of the register (including the starting address of the register to be read and the length of the register to be read) through function code 03, the returned data includes the data length and data content of the register. Intelligent instruments generally use single chip microcomputer with solidified communication protocol and work instructions. The measurement and control system mostly adopts the communication mechanism of single sending and multiple receiving. Therefore, in the intelligent instrument, the user should be allowed to preset the address code of the machine and ensure that it cannot be repeated. In addition, it should be noted that the communication baud rate must match the communication baud rate of the upper computer. In the software development of intelligent instrument, following the communication frame structure of Modbus protocol, it is necessary to analyze the commands transmitted by the upper computer. The non calling object should be able to recover in time and wait for the receiving state. The instrument responding to the call should analyze the command code and carry out corresponding function processing. The illegal command operation should be able to send back error information.


¡¡¡¡3. Host computer software design

¡¡¡¡The communication software of upper computer adopts VB6 0 development. In VB6 0 can use MSComm control to program the serial port. The control is mainly designed for the general serial port of RS-232. Therefore, in order to use the existing RS-232 interface on PC, RS-232 / 485 converter is usually used. The converter generally controls RXD, TXD and GND signals through logic gate circuit, so as to automatically control the half duplex RS-485 serial port. Through this converter, the host computer communication software based on RS-485 serial port can be developed quickly like developing general RS-232 serial port. The upper computer communication software mainly includes initialization program, command word sending program and upper computer data receiving and processing program.


¡¡¡¡3.1 initialization procedure

¡¡¡¡In the initialization program, the whole frame structure is mainly set

¡¡¡¡Command array, transmission baud rate and some necessary serial ports

¡¡¡¡Start setting. The specific codes are as follows:

¡¡¡¡ctrMSComm. Inbuffersize = 1024 / / open data buffer

¡¡¡¡ctrMSComm. Inbuffercount = 0 / / clear the characters in the receive buffer

¡¡¡¡ctrMSComm. Outbuffersize = 512 / / sets the size of the transmission buffer

¡¡¡¡ctrMSComm. Inputmode = 1 / / set to binary data flow mode

¡¡¡¡ctrMSComm. Inputlen = 0 / / read all data at one time

¡¡¡¡ctrMSComm. Rthreshold = 51 / / OnComm event is triggered after receiving the required number of characters

¡¡¡¡ctrMSComm. Commport = 1 / / setting required

¡¡¡¡Serial port

¡¡¡¡etrMSComm. Settings = "9, 600, N, 8, 1" / / set baud rate and verification method

¡¡¡¡ctrMSComm. Portopen = true / / open the communication port

¡¡¡¡The above codes are generally several parts that must be set for serial port initialization. According to personal needs, it can be in VB6 0 makes an initialization interface to freely set various parameters (such as string slogan, baud rate, etc.) to improve the flexibility and universality of initialization.


¡¡¡¡3.2 command word sending program

¡¡¡¡The command word shall be sent in strict accordance with the frame format designed by Modbus protocol and the communication protocol defined by upper and lower computers on the basis of this protocol. The purpose of sending this command is to return the effective value, frequency, active power, reactive power, power factor and other parameters of three-phase phase current and line voltage to the intelligent distribution monitoring unit.


¡¡¡¡3.3 data receiving and processing program of upper computer

¡¡¡¡MSComm control provides two ways to deal with communication: event driven mode and query mode. This paper adopts an event driven approach. This method is a very effective method to deal with the interaction of serial ports. In many cases, you need to be notified when events occur. For example, when there are characters in the serial port receiving buffer, you can use the OnComm event of MSComm control to capture and process these communication events; OnComm events can also check and handle communication errors. Developers can add their own processing code to the OnComm event processing function. The advantage of event driven mode is that the program responds in time and has high reliability. Generally, the data sent from the lower computer has a certain frame degree, especially for some solidified intelligent instruments. Therefore, a good method is to open up two arrays of the same length in the host computer sequence according to the frame length of the communication return. One is used as a receiving array to receive the data in the serial port buffer at one time; One is a safe array, which is used to copy the data in the receiving array for analysis. This can improve the fault tolerance of the whole system. The specific code is as follows:

¡¡¡¡Private Sub

¡¡¡¡ctrMSComm¡ªOnComm()

¡¡¡¡Dim

¡¡¡¡bytInput()As Byte

¡¡¡¡Dim

¡¡¡¡bytInSafe()As Byte

¡¡¡¡Dim

¡¡¡¡intlnputlen

¡¡¡¡As

¡¡¡¡Integer

¡¡¡¡Select CasectrMSComm. COmmEvent

¡¡¡¡Case comevreceive / / ctrm received -

¡¡¡¡SComm. This event will be triggered after rthreshold characters

¡¡¡¡in

¡¡¡¡tlnputlen=ctrMSComm. InBuffer-

¡¡¡¡Count

¡¡¡¡bytInput=ctrMSComm. Input

¡¡¡¡//Read data from receive buffer

¡¡¡¡ReDimPreserve

¡¡¡¡bytlnput(intlnputlen)

¡¡¡¡Fori=0to

¡¡¡¡intInputlen

¡¡¡¡bytlnSafe(i)=bytInput(i)

¡¡¡¡Nexti

¡¡¡¡End Select

¡¡¡¡End Sub

¡¡¡¡Bytinput () in the code is a receiving array, and bytin - safe () is another safe array. In the receiving array, bytlnput (0) is the slave address "& H01", bytlnput (1) is the function code "& H03", bytlnput (2) is the returned data length, and bytlnput (3) and bytlnput (4) are the upper 8 bits and lower 8 bits of register address 0103 respectively. In pdm-810plm intelligent distribution monitoring unit, the register stores the effective value h of AB line voltage. Through the following procedure, the effective value of AB line voltage can be expressed in decimal form.

¡¡¡¡DimValuel As

¡¡¡¡Single

¡¡¡¡DimValue2 As

¡¡¡¡Single

¡¡¡¡Value = bytinput (3) / / convert binary data to

¡¡¡¡Replace it with a decimal number

¡¡¡¡Value2=bytlnput(4)

¡¡¡¡Text. Text = format (fvaluel wood 256 + value-ue2) wood 0.1, "000.0") / / synthesize the high and low bits into a decimal number and keep one decimal place. The position of the decimal point can be set flexibly according to actual needs (corresponding to "0x06" function code)


¡¡¡¡3.4 implementation method of CRC check code

¡¡¡¡CRC (cyclic redundancy check) is cyclic redundancy code check, which uses the generated polynomial to generate check bits for coding. At present, CRC16 (American Standard) check code is widely used in intelligent instruments, and its calculation method is as follows:

¡¡¡¡(1) Preset a 16bit register as hexadecimal ffff (i.e. all are 1). Call this register CRC register.

¡¡¡¡(2) The first 8-bit binary data is different or different from the 8-bit of the 16 bit CRC register, and the result is placed in the inner gate of the CRC register;

¡¡¡¡(3) Shift the contents of the CRC register to the right by one bit, fill the highest bit with 0, and check the shifted bit after the shift to the right.

¡¡¡¡(4) If the shift out bit is 0, repeat step (3); If the shift out bit is 1, the CRC register is XOR with the polynomial & ha001.

¡¡¡¡(5) Repeat steps (3) and (4) until it is shifted to the right 8 times, so that the whole 8bit data is processed.

¡¡¡¡(6) Repeat steps (2) to (5) to process the next byte of the communication information frame.

¡¡¡¡(7) After all bytes of the communication information frame are calculated according to the above steps, the high and low bytes of the obtained 16bit CRC register are exchanged. The final CRC register content is the CRC check code.


¡¡¡¡4 Conclusion

¡¡¡¡In the actual measurement and control system project development, through VB6 0 combined with SQL database programming, the measurement and control management software of upper computer based on Modbus protocol is developed, and the monitoring and data processing of lower computer are realized by RS-485 bus. Making full use of the existing, mature and standardized communication protocol can save the development cost, reduce the development risk, and improve the compatibility and portability of the system. As an international communication protocol widely used in the field of intelligent instruments, ModbusRTU protocol will be more widely used.


ÁªÏµÏúÊÛ
ÏúÊÛÍõ¾­Àí΢ÐÅ ÏúÊÛÍõ¾­Àí
΢ÐŹ«ÖںŠ΢ÐŹ«ÖÚºÅ
·þÎñÈÈÏß
400-6688-400

k8¿­·¢ÌìÉúÓ®¼ÒÒ»´¥¼´·¢