Gateway machine equipment Modbus protocol data access processing method

By stacking the sensor information points in the gateway device and generating Modbus messages, the problem of reframe Modbus messages under TCP communication mode is solved, and the Modbus data access processing is simplified and unified, and the processing of different communication modes is supported.

CN119544815AActive Publication Date: 2025-02-28NANJING SHENDA ENG TECH CO LTD
View PDF 5 Cites 0 Cited by

Patent Information

Application Number
CN202411837894.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-12-13
Publication Date
2025-02-28
Estimated Expiration
2044-12-13

AI Technical Summary

Technical Problem

When the prior art uses TCP communication method to transmit serial Modbus messages, it is necessary to reframe the Modbus messages, resulting in a complex data access processing process and lack of unified or standardized methods.

Method used

By implementing the Modbus data access processing method in the gateway device, the sensor information points are divided into stacking according to the sensor communication address, function code and register address, and corresponding Modbus messages are generated, the mapping relationship between the message and the information points is established, the acquisition period is set, and the message is stored in a fixed message queue, and the Modbus processing thread base class is processed and sent.

Benefits of technology

It solves the problem of separation of Modbus RTU protocol and Modbus TCP protocol, simplifies the Modbus data access processing process, avoids the problem of reframe before each message is sent, and supports unified processing of different communication methods.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119544815A_ABST
    Figure CN119544815A_ABST
Patent Text Reader

Abstract

The invention discloses a gateway machine equipment Modbus data access processing method, which comprises the following steps: piling sensor information points through a communication protocol type, framing Modbus messages according to a communication address, a function code and a register address in advance, putting the Modbus messages into a fixed message queue, and setting an acquisition period and an associated sensor information point for each Modbus message; creating different processing threads for different communication modes; and the processing process after the Modbus message in the message sending queue is taken out is completed in the Modbus communication thread base class. According to the invention, the problem of separation of the Modbus RTU protocol and the Modbus TCP protocol can be solved, and code development does not need to be separately carried out on each combination of the Modbus RTU protocol and the Modbus TCP protocol; meanwhile, due to the design mode of the Modbus message, the problem that the Modbus message needs to be re-framed before being sent each time is easily solved; besides, if a communication node of a new communication type is added, only a Modbus processing thread base class needs to be inherited, a processing thread class is added, and a Modbus protocol processing part does not need to be concerned.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The invention relates to a gateway device Modbus data access processing method, belonging to the sensor information point technology in a smart substation. Background Art

[0002] Modbus protocol is a serial communication protocol, which was published by Modicon in 1979 for communication with programmable logic controllers (PLCs). Today, Modbus protocol has become the industry standard for communication protocols in the industrial field and is a common connection method for industrial power equipment. Modbus protocol currently has versions for serial ports, Ethernet, and other networks that support Internet protocols, supporting multiple communication methods, including Modbus RTU, Modbus ASCII, Modbus TCP, etc.

[0003] Modbus RTU is the most common communication method of Modbus protocol. It adopts binary data transmission, has high transmission efficiency and stability, supports RS232 and RS484 interfaces, and transmits two hexadecimal characters per byte. The message frame format of ModbusRTU is shown in Table 1:

[0004] Table 1 Modbus RTU message frame format

[0005] Additional Address Function code data CRC 1 character 1 character 0 to 255 characters 2 characters

[0006] Modbus ASCII uses ASCII code to transmit text information. Compared with Modbus RTU, the transmission efficiency is lower, but the readability is better. Both Modbus ASCII and Modbus RTU use serial communication and support RS232 and RS485 interfaces, which are suitable for shorter distance communication. The message frame formats of Modbus ASCII and Modbus RTU are different. In Modbus ASCII, the message frame starts with an English colon ":" and ends with a carriage return and line feed (CRLF) symbol. The message frame format of Modbus ASCII is shown in Table 2:

[0007] Table 2 Modbus ASCII message frame format

[0008] start address Function code data LRC Finish 1 character: 2 characters 2 characters 0 to 2 × 252 characters 2 characters 2 characters CR or LF

[0009] Modbus TCP is an extension of the Modbus protocol. It uses the TCP / IP protocol for communication and can transmit data between LANs and WANs. It is suitable for remote monitoring and control and is often used in industrial networks and IoT applications. The message frame formats of ModbusTCP and Modbus RTU are different. Compared with Modbus RTU, Modbus TCP removes the additional address and checksum and adds a message header. The message frame format of Modbus TCP is shown in Table 3:

[0010] Table 3 Modbus RTU and Modbus TCP message frame formats

[0011]

[0012] In the Modbus TCP message frame: the transaction identifier occupies 2 bytes, which is used to identify the uniqueness of the transaction. The transaction identifiers of the message frames of the master and slave need to be consistent during each communication process, and the transaction identifier of each new message initiated by the master must be incremented; the protocol identifier occupies 2 bytes and is fixed to 0x0000; the length occupies 2 bytes, indicating the total length of the unit identifier, function code and data part; the unit identifier occupies 1 byte, indicating the target slave address, which is consistent with the target slave address of Modbus RTU.

[0013] From the above, it can be seen that although the Modbus protocol supports multiple communication methods, these communication methods all support the same Modbus function code, and the framing methods of the data part based on the same function code are also consistent; therefore, in some usage scenarios, TCP communication method is also used to transmit data in the Modbus RTU message frame format, but because the Modbus RTU message frame adopts the serial communication method, it is necessary to solve the problem of separating the serial communication protocol from the serial communication method; the existing technology usually adopts the method of separately developing code for each combination of serial communication protocol and serial communication method, and each Modbus message frame involves the problem of reframing before sending, and lacks a unified or standardized Modbus data access processing method. Summary of the invention

[0014] Purpose of the invention: In order to overcome the deficiencies in the prior art, the present invention provides a gateway device Modbus data access processing method to solve the problem of needing to reframe Modbus messages when transmitting serial port Modbus messages using TCP communication, thereby simplifying the Modbus data access processing process.

[0015] Technical solution: To achieve the above purpose, the technical solution adopted by the present invention is:

[0016] A method for accessing and processing Modbus data of a gateway device comprises the following steps: for a communication node based on the Modbus RTU protocol and the Modbus TCP protocol in a gateway device, the sensor information points on the communication node are divided into piles according to the sensor communication address, register address and function code, and the sensor information points with the same sensor communication address, the same function code and the continuous register address are divided into one data pile; according to the sensor communication address, the function code, the register address and the communication protocol type, the Modbus message of each data pile is generated, a mapping relationship between the Modbus message and the sensor information point is established, the collection cycle is set and the Modbus message is stored in a fixed message queue of the communication node to be processed; a message sending queue is constructed for each communication node, and the Modbus message in the fixed message queue is added to the message sending queue according to the collection cycle; a Modbus processing thread base class is constructed to process the Modbus message, and a Modbus serial port processing thread class and a Modbus are created based on the Modbus processing thread base class. TCP processing thread class, for communication nodes using serial communication mode, use the Modbus serial port processing thread class to create a Modbus serial port processing thread object, for communication nodes using TCP communication mode, use the Modbus TCP processing thread class to create a Modbus TCP processing thread object; start the processing thread object, the Modbus processing thread base class takes out the Modbus message from the message sending queue and processes it, each processing thread object has its own fixed message queue, timer, message sending queue and processing thread, each performs its own duties and will not interfere with each other.

[0017] Specifically, when generating the Modbus message of each data pile: if the communication protocol type is Modbus RTU protocol, a complete Modbus message is directly generated; if the communication protocol type is Modbus TCP protocol, a semi-complete Modbus message including protocol identifier, length, unit identifier, function code and data part is first generated; the complete Modbus message and the semi-complete Modbus message are placed in the fixed message queue of each communication node for processing; the Modbus message is taken out and processed using the Modbus processing thread base class: if it is Modbus RTU protocol, the processed Modbus message is directly sent, and then the read and write ports are monitored, and a data message of the specified length is read within the read timeout period, and the data message read is analyzed in combination with the sensor data point corresponding to the Modbus message, and translated into the value of the sensor data point; if it is Modbus For TCP protocol, a self-incrementing transaction identifier is first added to the processed Modbus message to obtain a complete Modbus message before sending it, and then the read and write ports are monitored. After a data message of a specified length is read within the read timeout period, the data message is parsed in combination with the sensor data point corresponding to the Modbus message and translated into the value of the sensor data point.

[0018] Specifically, when constructing a Modbus processing thread base class, add a virtual interface function named create, a virtual interface function named open, a virtual interface function named write, a virtual interface function named read and a timer to the Modbus processing thread base class; the create virtual interface function is inherited by the derived class to implement the process of creating a communication node, the open virtual interface function is inherited by the derived class to implement the communication connection establishment and communication connection maintenance of the communication node, the write virtual interface function is inherited by the derived class to implement the data message writing operation, and the read virtual interface function is inherited by the derived class to implement the data message reading operation; after the processing thread object is started, first call the open virtual interface function, then loop read the message sending queue, take out the first Modbus message and process it, then: if it is the Modbus RTU protocol, then call the write virtual interface function in turn to write the message data, call the read virtual interface function to read the message data, read the data message of the specified length within the read timeout, and perform data analysis on the read data message in combination with the sensor data point corresponding to the Modbus message, and translate it into the value of the sensor data point; if it is Modbus For TCP protocol, a self-incrementing transaction identifier is first added to the front of the Modbus message, and then the write virtual interface function is called in sequence to write the message data and the read virtual interface function is called to read the message data. A data message of the specified length is read within the read timeout period, and the data message is parsed in combination with the sensor data point corresponding to the Modbus message and translated into the value of the sensor data point.

[0019] Specifically, the Modbus serial port processing thread class and the Modbus TCP processing thread class are both inherited from the Modbus processing thread base class. The Modbus serial port processing thread class and the Modbus TCP processing thread class only need to inherit the open virtual interface function, write virtual interface function and read virtual interface function of the Modbus processing thread base class, and the two processes of periodically taking Modbus messages that meet the sending requirements from the fixed message queue and taking out and reading and writing data messages from the message sending queue are inherited from the Modbus processing thread base class and do not need to be rewritten.

[0020] Specifically, all sensor information points are divided into piles according to the sensor communication address, register address and function code, and the sensor information points with the same sensor communication address, the same function code and the continuous register address are grouped into one data pile and sorted according to the register address; if the register address range of a data pile exceeds the Modbus message length limit, the register address range is fragmented based on the Modbus message length limit to form more than two data piles.

[0021] Specifically, configuring a fixed message queue through the Modbus data acquisition module includes the following steps:

[0022] (a1) Loading the records of the communication protocol type of Modbus RTU protocol and Modbus TCP protocol in the communication node configuration table, the loaded content includes the communication node ID, communication node name, communication parameters, communication mode and communication protocol type;

[0023] (a2) Load the sensor configuration under each communication node and record the sensor communication address;

[0024] (a3) loading the sensor information point configuration under each sensor, parsing the sensor information point parameters, obtaining the register address and function code of the sensor information point, as well as the data type, data length, data parsing method, data offset value and data scale factor;

[0025] (a4) grouping all sensor information points into piles according to the sensor communication address, register address and function code, grouping sensor information points with the same sensor communication address, the same function code and continuous register address into one data pile, and sorting them according to the register address;

[0026] (a5) If the register address range of a data pile exceeds the Modbus message length limit, the register address range is fragmented based on the Modbus message length limit to form two or more data piles;

[0027] (a6) Generate a Modbus message for the data stack according to the sensor communication address, function code, register address and communication protocol type of each data stack, and establish a mapping relationship between the Modbus message and the sensor information point; if the communication protocol type is the Modbus RTU protocol, directly generate a complete Modbus message; if the communication protocol type is the Modbus TCP protocol, first generate a semi-complete Modbus message including a protocol identifier, length, unit identifier, function code and data part, and then perform self-increment processing on the transaction processing identifier when the message is sent to form a complete Modbus message; set the collection period of the Modbus message according to the collection period of the sensor, and set a list of all sensor information points associated with the Modbus message.

[0028] More specifically, a communication node configuration table is added to the gateway machine, and the configuration information includes the communication node ID, communication node name, communication parameters, communication mode and communication protocol type; the communication node is the communication interface of the sensor on the gateway machine, and the communication node configuration is the communication interface configuration of the sensor on the gateway machine; the communication parameters are in a string format, and the parameter format is determined by the communication mode and the communication protocol type. For the serial communication mode, it includes the serial port number, baud rate, data bit, stop bit and parity bit; for the TCP communication mode, it includes the IP address and port number.

[0029] More specifically, a sensor library is constructed in the database of the gateway machine. The sensor library is used to store sensor models of various sensors connected to the gateway machine. The sensor model includes a sensor module and a sensor information point module. Sensors and sensor parameters are defined in the sensor module. Sensor information points and sensor information point parameters are defined in the sensor information point module. The sensor parameters include a sensor communication address. For the Modbus protocol, the sensor communication address is the sensor communication address.

[0030] More specifically, a model library is constructed in the database of the gateway machine, and data models of different types of sensors are preset in the model library. The data model includes a model module and a model information point module. The model and model parameters are defined in the model module, and the model information point and model information point parameters are defined in the model information point module.

[0031] The sensor module inherits from the model module, the sensor information point module inherits from the model information point module, and the sensor information point parameters inherit from the model information point parameters;

[0032] When configuring sensors on the gateway, first select the communication node of the sensor, then select the data model according to the sensor type, then add the sensor and sensor parameters, sensor information points and sensor information point parameters, and finally store the configured sensor model in the sensor library.

[0033] More specifically, the model information point parameters are in a string format, and the parameter format is determined by the communication method and the communication protocol type. For the Modbus protocol, it includes the register address and function code, as well as the data type, data length, data parsing method, data offset value and data scaling factor.

[0034] Beneficial effects: The Modbus data access processing method for gateway equipment provided by the present invention performs stacking processing on sensor information points according to the communication protocol type, pre-frames the Modbus message according to the communication address, function code and register address and puts it into a fixed message queue, sets a collection period and associated sensor information point for each Modbus message; creates different processing threads for different communication nodes; at the code level, the processing process after the Modbus message in the message sending queue is taken out is completed in the Modbus communication thread base class; according to the different communication protocol types, the processed Modbus message is also subjected to different processing methods before being sent to the slave station; after the master station receives the response message from the slave station, it will take out the matching data for the sensor information point for data analysis. The processing method of the present invention can solve the problem of separation of the Modbus RTU protocol and the Modbus TCP protocol, and there is no need to separately develop codes for each combination of the Modbus RTU protocol and the Modbus TCP protocol; at the same time, the design method of the Modbus message easily solves the problem of needing to reframe each Modbus message before sending; in addition, if a communication node of a new communication method is added, it only needs to inherit the Modbus processing thread base class and add a processing thread class at the code level, and there is no need to pay attention to the Modbus protocol processing part. BRIEF DESCRIPTION OF THE DRAWINGS

[0035] Figure 1 The process of generating the data pile in the present invention;

[0036] Figure 2 This is a schematic diagram of the creation process of the Modbus serial port processing thread class and the Modbus TCP processing thread class;

[0037] Figure 3 A schematic diagram of the Modbus serial port processing thread class and the Modbus TCP processing thread class inheriting the Modbus processing thread base class;

[0038] Figure 4 The following is a schematic diagram of the process of processing Modbus messages through the Modbus processing thread base class. DETAILED DESCRIPTION

[0039] The present invention is described in detail below with reference to the accompanying drawings and specific embodiments.

[0040] A method for accessing and processing Modbus data of a gateway device comprises the following steps: for a communication node based on the Modbus RTU protocol and the Modbus TCP protocol in a gateway device, the sensor information points on the communication node are divided into piles according to the sensor communication address, register address and function code, and the sensor information points with the same sensor communication address, the same function code and the continuous register address are divided into one data pile; according to the sensor communication address, the function code, the register address and the communication protocol type, the Modbus message of each data pile is generated, a mapping relationship between the Modbus message and the sensor information point is established, the collection cycle is set and the Modbus message is stored in a fixed message queue of the communication node to be processed; a message sending queue is constructed for each communication node, and the Modbus message in the fixed message queue is added to the message sending queue according to the collection cycle; a Modbus processing thread base class is constructed to process the Modbus message, and a Modbus serial port processing thread class and a Modbus are created based on the Modbus processing thread base class. TCP processing thread class, for communication nodes using serial communication mode, use the Modbus serial port processing thread class to create a Modbus serial port processing thread object, for communication nodes using TCP communication mode, use the Modbus TCP processing thread class to create a Modbus TCP processing thread object; start the processing thread object, the Modbus processing thread base class takes out the Modbus message from the message sending queue and processes it, each processing thread object has its own fixed message queue, timer, message sending queue and processing thread, each performs its own duties and will not interfere with each other.

[0041] The present invention is further described below in conjunction with specific implementation steps.

[0042] Step 1: Preset the communication method in the dictionary table of the gateway machine

[0043] The communication mode is preset in the dictionary table of the gateway machine, including the serial communication mode and the TCP communication mode. Table 4 shows an example of preset communication mode, including but not limited to the table content.

[0044] Table 4 Preset communication mode configuration in the database dictionary (expandable)

[0045] Dictionary code Dictionary item ID name describe commode 1 Serial communication Serial communication mode commode 2 TCP communication TCP communication method commode 3 UDP communication commode 4 MQTT communication

[0046] Step 2: Preset the communication protocol type in the dictionary table of the gateway machine

[0047] The communication protocol types are preset in the dictionary table of the gateway machine, including Modbus RTU protocol and Modbus TCP protocol. Table 5 shows an example of preset communication protocol types, including but not limited to the table contents.

[0048] Table 5 Preset communication protocol type in database dictionary (expandable)

[0049] Dictionary code Dictionary item ID name describe protocoltype 1 MODBUS RTU MODBUS RTU Protocol protocoltype 2 MODBUS TCP MODBUS TCP Protocol

[0050] Step 3. Add the communication node configuration table in the gateway machine

[0051] Add a communication node configuration table in the gateway machine. The configuration information includes the communication node ID, communication node name, communication parameters, communication mode and communication protocol type. The communication node is the communication interface of the sensor on the gateway machine, and the communication node configuration is the communication interface configuration of the sensor on the gateway machine. Table 6 shows an example of the communication node configuration, including but not limited to the table content.

[0052] Table 6 Adding communication node configuration table in gateway machine (expandable)

[0053] Fields type illustrate Id Number Communication node ID, non-null, * Name String Communication node name Commaddr String Communication parameters Commmode Number Communication Protocol type Number Communication protocol type

[0054] The communication parameters are in character string format, which is determined by the communication mode and communication protocol type. Different communication protocol types and communication modes will configure different communication parameters. For serial communication mode, the communication parameters include serial port number, baud rate, data bit, stop bit and parity bit. For TCP communication mode, the communication parameters include IP address and port number.

[0055] Step 4: Build a model library in the database of the gateway machine

[0056] A model library is built in the database of the gateway machine, and data models of different types of sensors are preset in the model library. The data model includes a model module and a model information point module. The model and model parameters are defined in the model module, and the model information point and model information point parameters are defined in the model information point module. The data model is designed to model the sensor for easy use by users; when it is necessary to preset a sensor model in the gateway machine, first select the corresponding type of data model as the basis, and then add the relevant parameters of the sensor to achieve the access of the sensor model.

[0057] The data model, model module and model information point module in this example are all designed as tables, which are respectively recorded as model table and model data point table.

[0058] Table 7 shows an example of a model table, including but not limited to the table content.

[0059] Table 7 Model table example

[0060] Fields Data Types illustrate Id Number Model ID, not null, * Name String Model Name samename String Model unique code

[0061] Table 8 is an example of a model information point table, including but not limited to the table content.

[0062] Table 8 Example of model information point table

[0063]

[0064]

[0065] Note: The value of the modelid field in Table 8 is equal to the Id field in Table 7.

[0066] The model information point parameters are in string format, and the parameter format is determined by the communication method and communication protocol type. For the Modbus protocol, it includes the register address and function code, as well as the data type, data length, data parsing method, data offset value and data scale factor. Model information point parameters are used to help data parsing.

[0067] Step 5: Build a sensor library in the gateway machine’s database

[0068] A sensor library is constructed in the database of the gateway machine. The sensor library is used to store sensor models of various sensors connected to the gateway machine. The sensor model includes a sensor module and a sensor information point module. Sensors and sensor parameters are defined in the sensor module, and sensor information points and sensor information point parameters are defined in the sensor information point module.

[0069] In the sensor model of this example, the sensor module and the sensor information point module are designed as tables, which are respectively recorded as sensor table and sensor information point table.

[0070] Table 9 shows an example of a sensor table, including but not limited to the table contents.

[0071] Table 9 Sensor table example

[0072] Fields type illustrate Id Number Sensor ID, non-null, * Name String Sensor Name commnodeid Number Communication Node ID modelid Number Model ID commaddr String Sensor communication address

[0073] Note: In Table 9, the value of the modelid field is equal to the Id field in Table 7.

[0074] The sensor parameters in this case include the sensor communication address, which usually varies according to the communication method and communication protocol type of the communication node; for the Modbus protocol, the sensor communication address is the sensor communication address; for wireless communication, the sensor communication address is the SN code of the sensor; for IO communication, the sensor communication address is the IO port number.

[0075] Table 10 is an example of a sensor data point table, including but not limited to the table content.

[0076] Table 10 Sensor data point table example

[0077]

[0078]

[0079] Note: In Table 10, the value of the field sensorid is equal to the field Id in Table 9, and the value of the field modelpointid is equal to the field Id in Table 8.

[0080] The sensor module inherits from the model module, the sensor information point module inherits from the model information point module, and the sensor information point parameters inherit the model information point parameters (which can also be modified according to the actual sensor communication address).

[0081] Step 6: Configure the sensor on the gateway

[0082] When configuring sensors on the gateway, first select the communication node of the sensor, then select the data model according to the sensor type, then add the sensor and sensor parameters, sensor information points and sensor information point parameters, and store the configured sensor model in the sensor library.

[0083] Step 7: Configure a fixed message queue through the Modbus data acquisition module

[0084] Configure a fixed message queue through the Modbus data acquisition module, such as Figure 1 As shown, the following steps are included:

[0085] (71) Load the records of the communication protocol type of Modbus RTU protocol and Modbus TCP protocol in the communication node configuration table, and the loaded content includes the communication node ID, communication node name, communication parameters, communication mode and communication protocol type.

[0086] (72) Load the sensor configuration under each communication node and record the sensor communication address.

[0087] (73) Load the sensor information point configuration under each sensor, parse the sensor information point parameters, obtain the register address and function code of the sensor information point, as well as the data type, data length, data parsing method, data offset value and data scaling factor.

[0088] (74) All sensor information points are grouped into piles according to the sensor communication address, register address and function code. Sensor information points with consistent sensor communication addresses, consistent function codes and continuous register addresses are grouped into one data pile and sorted according to the register address.

[0089] (75) If the register address range of a data stack exceeds the Modbus message length limit, the register address range is segmented based on the Modbus message length limit to form two or more data stacks.

[0090] The data part of the Modbus RTU protocol is up to 255 bytes, and the data part of the Modbus TCP protocol is up to 65535 bytes. Since the length of each register address is 2 bytes, the register address is equal to the data length divided by 2; if the difference between the maximum register address and the minimum register address of the data stack is greater than the specified maximum register length, fragmentation is required.

[0091] (76) Generate a Modbus message for the data stack according to the sensor communication address, function code, register address and communication protocol type of each data stack, and establish a mapping relationship between the Modbus message and the sensor information point; if the communication protocol type is the Modbus RTU protocol, directly generate a complete Modbus message; if the communication protocol type is the Modbus TCP protocol, first generate a semi-complete Modbus message including a protocol identifier, length, unit identifier, function code and data part, and then perform self-increment processing on the transaction processing identifier when the message is sent to form a complete Modbus message; set the collection period of the Modbus message according to the collection period of the sensor, and set a list of all sensor information points associated with the Modbus message.

[0092] Step 8. Create a Modbus processing thread base class to process Modbus messages

[0093] The Modbus processing thread base class is created to process Modbus messages, including the following steps:

[0094] (81) Add a virtual interface function named create to the Modbus processing thread base class, which is used for derived classes to inherit and implement the process of creating a communication node. The function parameters are the parameter information of the communication node.

[0095] (82) Add a virtual interface function named open to the Modbus processing thread base class for the derived class to inherit and implement the communication connection establishment and maintenance of the communication node. The function parameters are the communication parameters of the communication node.

[0096] (83) Add a virtual interface function named write to the Modbus processing thread base class for the derived class to inherit and implement the data message writing operation. The function parameter is the content of the message to be sent.

[0097] (84) Add a virtual interface function named read to the Modbus processing thread base class for derived classes to inherit and implement data message reading operations. The function return value is the read message content.

[0098] (85) Add a timer to the Modbus processing thread base class to filter Modbus messages from the fixed message queue according to the collection period of each Modbus message.

[0099] (86) A message sending queue is created for each communication node, and the filtered Modbus messages are added to the message sending queue of the same communication node.

[0100] (87) After the processing thread object is started, the open virtual interface function is called first, and then the message sending queue is read in a loop, the first Modbus message is taken out and processed, and then: if it is the Modbus RTU protocol, the write virtual interface function is called first to perform data write operations (send messages, from the master station to the slave station), and then the read virtual interface function is called to perform data read operations (read messages), and a data message of the specified length is read within the read timeout period (the response message sent by the slave station to the master station after receiving the Modbus message), and the data message read is analyzed in combination with the sensor data point corresponding to the Modbus message, and translated into the value of the sensor data point; if it is Modbus For TCP protocol, a self-incrementing transaction identifier is first added to the front of the Modbus message, and then the write virtual interface function is called to write data (send the message), and then the read virtual interface function is called to read data (read the message). The data message of the specified length is read within the read timeout period, and the data message is parsed in combination with the sensor data point corresponding to the Modbus message and translated into the value of the sensor data point.

[0101] Step 9. Create Modbus serial port processing thread class and Modbus TCP processing thread class

[0102] like Figure 2 As shown, for the serial communication mode, after parsing the communication parameters, a Modbus serial port processing thread class is created based on the Modbus processing thread base class; for the TCP communication mode, after parsing the communication parameters, a Modbus TCP processing thread class is created based on the Modbus processing thread base class.

[0103] like Figure 3As shown, the Modbus serial port processing thread class and the Modbus TCP processing thread class are both inherited from the Modbus processing thread base class. The Modbus serial port processing thread class and the Modbus TCP processing thread class only need to inherit the open virtual interface function, write virtual interface function and read virtual interface function of the Modbus processing thread base class, and the two processes of periodically taking Modbus messages that meet the sending requirements from the fixed message queue and taking out and reading and writing data messages from the message sending queue are inherited from the Modbus processing thread base class and do not need to be rewritten.

[0104] Step 10. Select the corresponding processing thread class for each communication node to create a processing thread object

[0105] For a certain communication node, according to the communication mode and communication parameters of the communication node, select the corresponding processing thread class to create a processing thread object: if the communication mode of the communication node is serial communication mode, use the Modbus serial port processing thread class to create a Modbus serial port processing thread object; if the communication mode of the communication node is TCP communication mode, use the Modbus TCP processing thread class to create a Modbus TCP processing thread object.

[0106] Step 11. Start the processing thread object

[0107] like Figure 4 As shown in the figure, the master station adds the Modbus message in the fixed message queue to the message sending queue according to the acquisition cycle; after starting the processing thread object, the Modbus processing thread base class takes out the Modbus message from the message sending queue and processes it; after the processing thread object is started, the open virtual interface function is called first, and then the message sending queue is read cyclically, the first Modbus message is taken out and processed, and then: if it is the Modbus RTU protocol, the write virtual interface function is called first to send the message, and then the read virtual interface function is called to read the message, and the data message of the specified length is read within the reading timeout period, and the data message read is analyzed in combination with the sensor data point corresponding to the Modbus message, and translated into the value of the sensor data point; if it is Modbus TCP protocol, first add a self-incrementing transaction processing identifier in front of the Modbus message, then call the write virtual interface function to send the message, and then call the read virtual interface function to read the message. Read the data message of the specified length within the read timeout period, and parse the read data message in combination with the sensor data point corresponding to the Modbus message and translate it into the value of the sensor data point.

[0108] Each processing thread object has its own fixed message queue, timer, message sending queue and processing thread, each performing its own function without interfering with each other.

[0109] The above shows and describes the basic principles, main features and advantages of the present invention. Those skilled in the art should understand that the above embodiments do not limit the present invention in any form, and any technical solution obtained by equivalent replacement or equivalent transformation falls within the protection scope of the present invention.

Claims

1. A gateway device Modbus data access processing method, characterized in that: For the communication nodes based on ModbusRTU protocol and Modbus TCP protocol in the gateway machine, the sensor information points on the communication node are divided into piles according to the sensor communication address, register address and function code, and the sensor information points with the same sensor communication address, function code and continuous register address are divided into one data pile; according to the sensor communication address, function code, register address and communication protocol type, the Modbus message of each data pile is generated, the mapping relationship between the Modbus message and the sensor information point is established, the collection cycle is set and the Modbus message is stored in the fixed message queue of the communication node to which it belongs for processing; a message sending queue is constructed for each communication node, and the Modbus message in the fixed message queue is added to the message sending queue according to the collection cycle; a Modbus processing thread base class is constructed to process Modbus messages, and a Modbus serial port processing thread class and a Modbus TCP processing thread class are created based on the Modbus processing thread base class. For the communication node using the serial communication mode, a Modbus serial port processing thread object is created using the Modbus serial port processing thread class, and for the communication node using the TCP communication mode, a Modbus is created using the Modbus TCP processing thread class. TCP processing thread object; start the processing thread object, the Modbus processing thread base class takes out the Modbus message from the message sending queue and processes it.

2. The gateway device Modbus data access processing method according to claim 1, characterized in that: When generating the Modbus message of each data pile: if the communication protocol type is Modbus RTU protocol, a complete Modbus message is directly generated; if the communication protocol type is Modbus TCP protocol, a semi-complete Modbus message including the protocol identifier, length, unit identifier, function code and data part is first generated; the complete Modbus message and the semi-complete Modbus message are placed in the fixed message queue of each communication node to wait for processing; After using the Modbus processing thread base class to take out the Modbus message and process it: if it is the Modbus RTU protocol, the processed Modbus message is sent directly, and then the read-write port is monitored. Within the read timeout period, a data message of the specified length is read, and the data message read is analyzed in combination with the sensor data point corresponding to the Modbus message, and translated into the value of the sensor data point; if it is the Modbus TCP protocol, a self-incrementing transaction processing identifier is first added to the front of the processed Modbus message to obtain a complete Modbus message and then sent, and then the read-write port is monitored. After the data message of the specified length is read within the read timeout period, the data message read is analyzed in combination with the sensor data point corresponding to the Modbus message, and translated into the value of the sensor data point.

3. The gateway device Modbus data access processing method according to claim 1, characterized in that: When constructing the Modbus processing thread base class, add a virtual interface function named create, a virtual interface function named open, a virtual interface function named write, a virtual interface function named read and a timer to the Modbus processing thread base class; the create virtual interface function is inherited by the derived class to implement the process of creating a communication node, the open virtual interface function is inherited by the derived class to implement the communication connection establishment and communication connection maintenance of the communication node, the write virtual interface function is inherited by the derived class to implement the data message writing operation, and the read virtual interface function is inherited by the derived class to implement the data message reading operation; After the processing thread object is started, the open virtual interface function is called first, and then the message sending queue is read cyclically, the first Modbus message is taken out and processed, and then: if it is the Modbus RTU protocol, the write virtual interface function is called in sequence to write data and the read virtual interface function is called in sequence to read data, and a data message of a specified length is read within the read timeout period, and the data message read is analyzed in combination with the sensor data point corresponding to the Modbus message, and translated into the value of the sensor data point; if it is the Modbus TCP protocol, a self-incrementing transaction processing identifier is first added to the front of the Modbus message, and then the write virtual interface function is called in sequence to write data and the read virtual interface function is called in sequence to read data, and a data message of a specified length is read within the read timeout period, and the data message read is analyzed in combination with the sensor data point corresponding to the Modbus message, and translated into the value of the sensor data point.

4. The gateway device Modbus data access processing method according to claim 3, characterized in that: The Modbus serial port processing thread class and the Modbus TCP processing thread class both inherit from the Modbus processing thread base class. The Modbus serial port processing thread class and the Modbus TCP processing thread class only need to inherit and implement the open virtual interface function, write virtual interface function and read virtual interface function of the Modbus processing thread base class.

5. The gateway device Modbus data access processing method according to claim 1, characterized in that: All sensor information points are divided into piles according to the sensor communication address, register address and function code. Sensor information points with consistent sensor communication address, consistent function code and continuous register address are divided into one data pile and sorted according to the register address; if the register address range of a data pile exceeds the Modbus message length limit, the register address range is fragmented based on the Modbus message length limit to form more than two data piles.

6. The gateway device Modbus data access processing method according to claim 1, characterized in that: To configure a fixed message queue through the Modbus data acquisition module, the following steps are included: (a1) Loading the records of the communication protocol type of Modbus RTU protocol and Modbus TCP protocol in the communication node configuration table, the loaded content includes the communication node ID, communication node name, communication parameters, communication mode and communication protocol type; (a2) Load the sensor configuration under each communication node and record the sensor communication address; (a3) loading the sensor information point configuration under each sensor, parsing the sensor information point parameters, and obtaining the register address and function code of the sensor information point; (a4) grouping all sensor information points into piles according to the sensor communication address, register address and function code, grouping sensor information points with the same sensor communication address, the same function code and continuous register address into one data pile, and sorting them according to the register address; (a5) If the register address range of a data pile exceeds the Modbus message length limit, the register address range is fragmented based on the Modbus message length limit to form two or more data piles; (a6) Generate a Modbus message for the data stack according to the sensor communication address, function code, register address and communication protocol type of each data stack, and establish a mapping relationship between the Modbus message and the sensor information point; if the communication protocol type is the Modbus RTU protocol, directly generate a complete Modbus message; if the communication protocol type is the Modbus TCP protocol, first generate a semi-complete Modbus message including a protocol identifier, length, unit identifier, function code and data part; set the collection period of the Modbus message according to the collection period of the sensor, and set a list of all sensor information points associated with the Modbus message.

7. The gateway device Modbus data access processing method according to claim 6, characterized in that: A communication node configuration table is added to the gateway machine. The configuration information includes the communication node ID, communication node name, communication parameters, communication mode and communication protocol type. The communication node is the communication interface of the sensor on the gateway machine, and the communication node configuration is the communication interface configuration of the sensor on the gateway machine. The communication parameters are in a string format, and the parameter format is determined by the communication mode and the communication protocol type. For the serial communication mode, it includes the serial port number, baud rate, data bit, stop bit and parity bit. For the TCP communication mode, it includes the IP address and port number.

8. The gateway device Modbus data access processing method according to claim 6, characterized in that: A sensor library is constructed in the database of the gateway machine. The sensor library is used to store sensor models of various sensors connected to the gateway machine. The sensor model includes a sensor module and a sensor information point module. Sensors and sensor parameters are defined in the sensor module. Sensor information points and sensor information point parameters are defined in the sensor information point module. The sensor parameters include a sensor communication address. For the Modbus protocol, the sensor communication address is the sensor communication address.

9. The gateway device Modbus data access processing method according to claim 7, characterized in that: A model library is constructed in the database of the gateway machine. The data models of different types of sensors are preset in the model library. The data model includes a model module and a model information point module. The model and model parameters are defined in the model module. The model information point and model information point parameters are defined in the model information point module. The sensor module inherits from the model module, the sensor information point module inherits from the model information point module, and the sensor information point parameters inherit from the model information point parameters; When configuring sensors on the gateway, first select the communication node of the sensor, then select the data model according to the sensor type, then add the sensor and sensor parameters, sensor information points and sensor information point parameters, and finally store the configured sensor model in the sensor library.

10. The gateway device Modbus data access processing method according to claim 9, characterized in that: The model information point parameters are in string format, and the parameter format is determined by the communication mode and the communication protocol type. For the Modbus protocol, it includes the register address and function code, as well as the data type, data length, data parsing method, data offset value and data scale factor.

Citation Information

Patent Citations

  • Method of realizing adaptive switching between two modes of transparent transmission and Modbus protocol conversion in network serial port

    CN109587158A

  • Method for automatically mapping Modbus protocol data to OPCUA address space

    CN112311808A

  • Communication system compatible with multiple protocols, control method thereof and electronic equipment

    CN116055583A

  • ModBus-based data interaction method during configuration software operation

    CN118945001A

  • Industrial control software development method and apparatus, and computing device

    WO2024131553A1