首页
搜索
历史搜索
HOME > NEWS > Industry news

Implementation of serial port debugging software based on Modbus RTU

2022-03-07 18:04:54| Source:网络| views:| 0

This paper introduces the characteristics of Modbus RTU communication protocol, and expounds the specific implementation method of application debugging software under vc2005 programming environment. The program adopts the modular idea, has clear structure and simple operation, realizes a good ModbusRTU message storage and management mechanism, uses multithreading, takes into account interface display and data communication, is fully compatible with modbuartu communication protocol, and can be used for the debugging of Modbus compatible equipment with self-defined function code, It overcomes the deficiency that ordinary MODBUS debugging software can only be used for Modbus standard function code debugging. Practice has proved that the programming idea is reasonable, the operation is stable and easy to operate, which brings convenience to the debugging of Modbus equipment in industrial automation.


1、 Introduction to ModbusRTU communication protocol

Modbus communication protocol is a communication protocol developed by modieon company. It works in the way of master-slave question and answer. It is a truly open, standard and license free network communication protocol. Widely used in automatic controllers and measurement and control instruments, it has become a recognized general industrial standard. Nowadays, MODBUS protocol has become one of the national standards of industrial automation network protocol specification in China. The control equipment produced by different manufacturers can be connected into an industrial network. Conduct centralized monitoring. The protocol has two transmission modes, namely RTU mode and ASCII mode. For ASCII mode, every 8-bit byte in an information frame is transmitted as 2 ASCII characters; For RTU mode. The 8-bit data in the information frame is regarded as two 4-bit hexadecimal characters. Compared with ASCII mode, RTU mode needs fewer 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、 ModbusRTU transmission process

Information transmission is asynchronous and in bytes. The information frame format of communication message transmitted between master station and slave station is shown in Table 1.

ModbusRTU adopts the master-slave mode. If the host device sends a message, it can return a response from a slave device. Similarly, when a slave device receives the message, it organizes the response information of a slave device and returns it to the host device that originally sent the message.

When the communication command is sent from the host to the 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 is verified to be correct, execute the corresponding task, and then return the execution result to the host.

372.jpg

Figure 1 query response cycle of Modbus RTU

373.jpg


3、 Implementation of serial port debugging software based on ModbusRTU

3.1 main structure

The software uses two threads. The main thread displays the main interface, which is used to set data. The message processing thread is used to monitor the message frame, analyze the message, take out the data set in the main interface and package it to respond [6l.

The functions of each part are encapsulated with classes to make the program concise, easy to understand and easy to transplant. Various modules used in the program, such as

Down [7, 8]:

(1) Thread data transmission module: it is used to transmit the data set in the main interface between the main thread and the sub thread, use global variables for inter thread communication, and use cmutex for thread synchronization.

(2) Serial port data transceiver module: it is used for common operations such as opening and closing of serial port and serial port data transceiver. In order to ensure the flexibility of serial port transceiver module, the serial port data transceiver program in the module adopts Windows API functions related to serial port operation.

(3) Modbus protocol module: the serial port data transceiver module is encapsulated again and various message specifications of modbusr and Ru are realized. This paper defines two structures of message format - PDU (protocol data unit) and Adu (application data unit), and the method of mutual transformation between them. And in

On this basis, it realizes the judgment and analysis of the received message and the organization and packaging of the message to be sent.

(4) CRC check module: it is used to generate 16 bit CRC check code of the sent message and generate CRC check code again for the received message to facilitate comparison with the original check code.

374.jpg

Fig. 2 flow chart of message processing thread

3.2 storage and management of message data

In order to store the data in the message, a message buffer with the size of 256 bytes is divided in the memory. The received message and packaged message are temporarily stored in this area. In other words, the parsing and packaging of the message becomes the operation of the message buffer [9, 10]. In order to better manage the message buffer, two structures are defined. PDU handle simulates PDU (message data unit), in which PDU handle simulates PDU (message data unit) and Adu control simulates Adu (protocol data unit) [11L. The two are defined as follows:

struet PDU—HANDLE

{

unsignedchar*PDUBuffPtr;// PUD data pointer

urlsigned

char FunctionCode;// Request function code

umigrled$l'92rt PD‰h;// PDU byte length

unsigned

Char exeeptiong// Exception code

};

Sta"uct ADU_CONTROL,

{

unsigned char*ADUBuffPtr;// ADU buffer pointer, pointing to message buffer

unsigned char ADULength;// Device address

unsigned short ADULength;// ADU byte length

};

For PDU_ Handie and ADU_ The functions of control operation mainly include packdu2pdu(), clearpdubuf(), and packp_ DU2ADU()。 The main processes are:

1) After receiving the legal message, fill the structure adu-control. The message is stored in the memory unit pointed to by the ad switch 3ufptr of adu-cantrol. Other relevant information of the message (slave address, message length) is filled into adu-controi Among the remaining members of.

2) Execute packadu2pdu (). Complete the conversion from Adu to PDU. This step mainly completes the comparison of crcl6 check codes, and completes the comparison of PDU according to the members of adu-controi_ Fill handle to facilitate subsequent analysis and processing of PDU content.

3) Execute clearpdubuf() to empty the contents of PDU data buffer. At this time, the analysis of the received message has been completed, and the contents of the PDU data buffer should be emptied To load the PDU data to be sent.

4) Execute packpdu2adu(). Add the device address to the head of the PDU and the crcl6 check code to the tail. At this time, it is encapsulated into a complete message for sending.

3.3 function code and corresponding processing function this program sets different processing functions for different function codes.

Some standard function codes and corresponding processing functions are given. Modbus debugging software is generally used as a slave. Analyze and respond to the message conforming to Modbus protocol sent by the host. Ordinary MODBUS debugging software can only analyze and respond to the above four standard function codes. But for the actual MODBUS device. These four function codes are far from enough. More often, you need to customize the function code to complete the relevant functions. In order to complete the debugging of user-defined function code. User defined function code processing functions can be added to the debugging software. This will make the program more targeted [7].

Table 2 some standard MODBUS function codes and their processing functions

Function description corresponding to function code

0x01

ReadCoils

Reading coil

0x02

ReadDiscretlnputs

Read discrete input

0x03

ReadHoldReg

Read hold relay

0x04 readinputreg read input relay

3.4 program interface

Figure 3 shows the main interface of ModbusRTU serial day debugging software. Figure 4 shows the communication interface between the upper computer software and modbosrtu serial port debugging software. It can be seen that the Modbus RTU debugging software works normally.

375.jpg

Figure 4 communication interface between upper computer software and ModbusRTU serial port debugging software

376.jpg

4、 Concluding remarks

Using the above methods, the serial port debugging software based on ModbusRTU compiled in vc2005 environment has been applied in the communication debugging between upper computer and lower computer of a small PLC R & D project of an automation enterprise. Both standard MODBUS function codes and user-defined function codes can be debugged. Practice has proved that the program is stable, reliable, simple and easy to operate. It provides a simple and feasible solution for Modbus RTU serial port debugging of user-defined function code.


联系销售
销售王经理微信 销售王经理
微信公众号 微信公众号
服务热线
400-6688-400