Disclosure of Invention
The invention aims to overcome the defects in the prior art, and provides a method for transmitting and distributing Modbus data of gateway equipment, which increases message processing on the uplink and downlink transmission of Modbus and simultaneously supports the requirements of reporting various uplink protocol data on the basis of being compatible with Modbus message acquisition.
The technical scheme adopted by the invention is as follows:
A gateway machine device Modbus data transmission and distribution method includes dividing network shutdown data types into downlink data and uplink data, wherein the downlink data refers to Modbus data interacted by downlink equipment and a gateway machine through a downlink serial port, the uplink data refers to Modbus data interacted by an uplink platform and the gateway machine through an uplink serial port and other uplink protocol data, the Modbus data transmission is divided into a data common transmission mode and a data transmission mode, in the data common transmission mode, the downlink data transmission and the uplink data transmission are carried out separately through a buffer, communication node configuration and the uplink platform configuration are independent, the data transmission mode refers to serial port acquisition commands issued by the uplink platform, the gateway machine forwards the serial port acquisition commands to the downlink serial port and monitors the downlink serial port response, and the downlink serial port response is written into the uplink serial port after the downlink serial port response is received. The gateway machine starts a data acquisition thread, monitors a read signal of an uplink serial port, adds all monitored data to the tail of a data buffer queue, firstly searches a function code in the data buffer queue, then identifies a message type based on the function code, extracts a Modbus message according to the Modbus protocol, and transmits the Modbus message to the downlink device, then reads a downlink serial port response according to the Modbus protocol within a timeout period and transmits the Modbus response to the uplink platform, if the downlink serial port response is a query response message and an uplink transmission requirement based on other uplink protocols related to the query response message exists, analyzes relevant parameters of data points of the device, analyzes the data value of the data points of the device, refreshes the data value of the data points of the device according to the analysis result, and finally packages the data message according to the corresponding other uplink protocol requirements and transmits the data up.
The gateway machine monitors a read signal of an uplink serial port, adds all monitored data to the tail of a data buffer queue, and starts searching the function code of the Modbus from the head of the data buffer queue, when the function code of the Modbus is identified, judges whether the message type of a serial port acquisition command belongs to a Modbus inquiry message or a Modbus control message according to the function code of the Modbus.
Specifically, according to the Modbus protocol, if the message type belongs to a Modbus inquiry message, judging whether the length of a received message in a data buffer queue is greater than or equal to 6, if the length of the received message is greater than or equal to 6, taking the previous byte of a function code as a slave station address, taking the last two bytes of the function code as the high bits of a register address and the low bits of the register address, taking the last two bytes of the register address as the high bits of the register number and the low bits of the register number, and taking the last two bytes as a CRC check code, calculating whether the CRC check code is correct, if so, extracting a frame of message from the data buffer queue according to the Modbus inquiry message format, if not, continuing to search the function code of the Modbus in the data buffer queue, and judging, wherein the extracted message frame is the Modbus inquiry message, and writing the extracted message frame into a downlink serial port.
Specifically, according to the Modbus protocol, if the message type belongs to the Modbus control message, the former byte of the function code is taken as the address of the slave station, the latter two bytes of the function code are taken as the high bit of the register address and the low bit of the register address, other corresponding data and CRC check codes are obtained according to the type of the function code, whether the CRC check codes are correct or not is calculated, if the CRC check codes are correct, the Modbus control message format extracts a frame of message from the data buffer queue, if the CRC check codes are incorrect, the function code continues to be searched in the data buffer queue and judged, the extracted message frame is the Modbus control message, and the extracted message frame is written into the downlink serial port.
Specifically, after the network shutdown issues the extracted message frame to the downlink equipment, the downlink serial port response message is read within the timeout period, if the read timeout period or the read downlink serial port response message length is shorter than the Modbus message format requirement, the read downlink serial port response message is discarded, and if the read downlink serial port response message length meets the Modbus message format requirement, the downlink serial port response message is written into the uplink serial port.
The communication method comprises the steps of presetting a communication mode and a communication protocol in a database dictionary table of a gateway machine, wherein the communication mode comprises serial communication, the communication protocol comprises a Modbus transparent protocol, the communication protocol of a downlink serial port is called a downlink Modbus transparent protocol, the communication protocol of an uplink serial port is called an uplink Modbus transparent protocol, a communication node, a downlink device and a device data point and an uplink platform which are connected with the downlink device are configured in a network shutdown, configuration information of the communication node comprises a communication node ID, a communication mode, a communication protocol and communication address information, the communication protocol is a downlink Modbus transparent protocol, the configuration information of the downlink device comprises a communication node ID, a device ID and a device communication parameter, the device communication parameter is a Modbus communication address of the device, the configuration information of the device data point comprises a device ID, a device data point ID and a device data point information parameter, the configuration information of the uplink platform comprises a platform ID, platform connection information and the platform protocol is an uplink Modbus transparent protocol.
Specifically, for serial port communication, the communication address information comprises a serial port address, a communication protocol, a baud rate, a parity check bit, a data bit and a stop bit, the equipment data point information parameters comprise a Modbus register address, a function code, a data type, a data length, a data analysis method, a decimal point number, a conversion relation and a data offset, and the platform connection information comprises a serial port address, a communication protocol, a baud rate, a parity check bit, a data bit and a stop bit.
Specifically, the method comprises the following steps:
Step1, performing information configuration in the network shutdown, including:
(11) Presetting a communication mode and a communication protocol in a database dictionary table of a gateway machine, wherein the communication mode comprises serial port communication, the communication protocol comprises a Modbus transparent transmission protocol, the communication protocol of a downlink serial port is called a downlink Modbus transparent transmission protocol, and the communication protocol of an uplink serial port is called an uplink Modbus transparent transmission protocol;
(12) Configuring a communication node connected with downlink equipment in a database of the network shutdown, wherein the configuration information comprises a communication node ID, a communication mode, a communication protocol and communication address information, wherein the communication protocol is a downlink Modbus transparent transmission protocol, the communication address information is combined information about equipment communication parameters, and for serial port communication, the communication address information comprises a serial port address, a communication protocol, a baud rate, a parity check bit, a data bit and a stop bit;
(13) Selecting a communication node, configuring downlink equipment, wherein the configuration information comprises a communication node ID, an equipment ID and equipment communication parameters, and the equipment communication parameters are Modbus communication addresses of the equipment;
(14) Selecting downlink equipment, configuring equipment data points, wherein the configuration information comprises equipment ID, equipment data point ID and equipment data point information parameters, the equipment data point information parameters are combined information about Modbus channels, and the equipment data point information parameters comprise Modbus register addresses, function codes, data types, data lengths, data analysis methods, decimal point numbers, conversion relations and data offsets;
(15) Configuring an uplink platform in a database of network shutdown, wherein configuration information comprises a platform ID, a platform protocol and platform connection information, wherein the platform protocol is an uplink Modbus transparent transmission protocol, the platform connection information is combination information about access parameters, and the platform connection information comprises a serial port address, a communication protocol, a baud rate, a parity check bit, a data bit and a stop bit;
Step2, loading communication node configuration, downlink equipment configuration, equipment data point configuration and uplink platform configuration;
step3, judging whether the communication node configures a downlink Modbus transparent transmission protocol, and the uplink platform configures an uplink Modbus transparent transmission protocol, wherein if not, the communication node is processed according to a common data reporting flow, and if so, the communication node is processed according to a data transparent transmission flow, wherein the data transparent transmission flow comprises the following steps:
(31) Analyzing communication address information in the communication node configuration information, and opening a downlink serial port according to the serial port address, the baud rate, the parity check bit, the data bit, the stop bit and other information of the downlink serial port;
(32) Analyzing platform connection information in the uplink platform configuration information, and opening an uplink serial port according to the serial port address, the baud rate, the parity check bit, the data bit, the stop bit and other information of the uplink serial port;
(33) Starting a data acquisition thread, monitoring a read signal of an uplink serial port, reading all data of the uplink serial port when the read signal of the uplink serial port is monitored, and adding all the read data to the tail of a data buffer queue;
(34) Searching the function code of the Modbus from the head of a queue of the data buffer, when the function code of the Modbus is identified, judging whether the message type belongs to a Modbus inquiry message or a Modbus control message according to the function code of the Modbus, and extracting a frame of message according to the format requirement of the Modbus message;
(341) According to the Modbus protocol, if the message type belongs to a Modbus inquiry message, judging whether the length of a received message in a data buffer queue is more than or equal to 6, if so, taking the previous byte of a function code as a slave station address, taking the last two bytes of the function code as the high bits of a register address and the low bits of the register address, taking the last two bytes of the register address as the high bits of the register number and the low bits of the register number, and taking the last two bytes as CRC check codes;
(342) According to the Modbus protocol, if the message type belongs to a Modbus control message, taking the previous byte of the function code as a slave station address, taking the last two bytes of the function code as the high bit of a register address and the low bit of the register address, and acquiring other corresponding data and CRC (cyclic redundancy check) codes according to the function code type;
(35) The method comprises the steps of writing a frame of extracted message into a downlink serial port, and reading a downlink serial port response message within a timeout period, discarding the read downlink serial port response message if the reading timeout period or the read downlink serial port response message length is shorter than the Modbus message format requirement, and writing the downlink serial port response message into an uplink serial port if the read downlink serial port response message length meets the Modbus message format requirement;
(36) If the downlink serial port response message is a query response message and there is an uplink transmission requirement on the query response message based on other uplink protocols, analyzing the downlink serial port response message at the same time, acquiring a slave station address, a register address and a register length in the downlink serial port response message, matching corresponding equipment data points according to the slave station address, the register address and the register length, then analyzing the data value of the equipment data points according to information such as Modbus register address, function code, data type, data length, data analysis method, decimal place number, conversion relation, data offset and the like contained in the equipment data point information parameters, refreshing the data value of the equipment data points according to analysis results, packaging the data message according to the corresponding other uplink protocol requirements, and transmitting the data.
The gateway equipment Modbus data transparent transmission and distribution device for realizing the method comprises a configuration module, a data common transmission module, a data transparent transmission module and other protocol uplink transmission modules;
The configuration module is used for configuring communication nodes connected with the downlink equipment, equipment data points of the downlink equipment and an uplink platform in the network shutdown;
the data common transmission module sets up downlink data transmission and uplink data transmission to be carried out separately through a cache when one or two of an uplink serial port or a downlink serial port in a data acquisition thread do not adopt a Modbus transparent transmission protocol, and independently configures a communication node and an uplink platform;
The gateway machine forwards the serial port acquisition command issued by the uplink platform to the downlink serial port and monitors the downlink serial port response, and writes the downlink serial port response into the uplink serial port after receiving the downlink serial port response;
And if the downlink serial port response is a query response message and an uplink transmission requirement based on other uplink protocols about the query response message exists, analyzing the query response message, acquiring relevant parameters of equipment data points, analyzing data values of the equipment data points, refreshing the data values of the equipment data points according to analysis results, and finally packaging the data message and transmitting uplink according to the corresponding other uplink protocol requirements.
The configuration module presets a communication mode and a communication protocol in a database dictionary table of a gateway machine, wherein the communication mode comprises serial communication, the communication protocol comprises a Modbus transparent protocol, the communication protocol of a downlink serial port is called a downlink Modbus transparent protocol, the communication protocol of an uplink serial port is called an uplink Modbus transparent protocol, a communication node, a downlink device, device data points of the downlink device and an uplink platform which are connected in a network machine are configured, configuration information of the communication node comprises a communication node ID, a communication mode, a communication protocol and communication address information, the communication protocol is the downlink Modbus transparent protocol, the configuration information of the downlink device comprises a communication node ID, a device ID and a device communication parameter, the device communication parameter is a Modbus communication address of the device, the configuration information of the device data point comprises a device ID, a device data point ID and a device data point information parameter, the configuration information of the uplink platform comprises a platform ID, platform protocol and platform connection information, and the platform protocol is the uplink Modbus transparent protocol.
Compared with the prior art, the method and the device for Modbus data transmission and protocol conversion of gateway equipment have the advantages that 1, the processing mechanism of Modbus uplink and downlink transmission messages is innovatively increased on the basis of being compatible with traditional Modbus message collection, 2, the characteristic that function codes of Modbus messages are relatively fixed is fully utilized, the function codes are used as message retrieval basis, modbus messages are accurately taken out and checked according to strict Modbus message formats, the data transmission error rate is effectively reduced, 3, data values can be intelligently matched according to register addresses and the number of registers in the messages, data points of the equipment are taken out, 4, multi-protocol conversion can be achieved by combining parameter configuration while Modbus data transmission is achieved, data is packaged into other uplink protocol formats, communication requirements of different systems and platforms are met, and the method and the device have high flexibility and expandability, can be easily adapted to complex and changeable industrial automation environments, and difficulty and cost of system integration are effectively reduced.
Detailed Description
The invention is described in detail below with reference to the drawings and the specific embodiments.
A gateway device Modbus data transparent transmission and distribution device comprises a configuration module, a data common transmission module, a data transparent transmission module and other protocol uplink transmission modules. The configuration module is used for configuring the communication node, the downlink equipment, the equipment data points of the downlink equipment and the uplink platform which are connected with the downlink equipment in the network shutdown. And the data common transmission module sets the downlink data transmission and the uplink data transmission to be carried out separately through the cache when one or two of the uplink serial port and the downlink serial port in the data acquisition thread do not adopt the Modbus transparent transmission protocol, and configures the communication node and the uplink platform independently. The gateway machine forwards the serial port acquisition command issued by the uplink platform to the downlink serial port and monitors the downlink serial port response, and writes the downlink serial port response into the uplink serial port after receiving the downlink serial port response. And if the downlink serial port response is a query response message and an uplink transmission requirement based on other uplink protocols about the query response message exists, analyzing the query response message, acquiring relevant parameters of equipment data points, analyzing data values of the equipment data points, refreshing the data values of the equipment data points according to analysis results, and finally packaging the data message and transmitting uplink according to the corresponding other uplink protocol requirements.
In the scheme, the data types of the network shutdown are divided into downlink data and uplink data, the downlink data refers to Modbus data interacted by downlink equipment and a gateway machine through a downlink serial port, the uplink data refers to Modbus data interacted by an uplink platform and the gateway machine through an uplink serial port and other uplink protocol data, the transmission of the Modbus data is divided into a data common transmission mode and a data transmission mode, in the data common transmission mode, the downlink data transmission and the uplink data transmission are carried out separately through a cache, the communication node configuration and the uplink platform configuration are independent from each other, the data transmission mode refers to the downlink serial port acquisition command issued by the uplink platform, the gateway machine forwards the serial port acquisition command to the downlink serial port and monitors the downlink serial port response, and after the downlink serial port response is received, the downlink serial port response is written into the uplink serial port.
The following describes the present invention further by taking a sensor as an example of a downlink device in conjunction with the specific implementation steps of the Modbus data transparent transmission and distribution method of the gateway device shown in fig. 1.
Step1, information configuration is carried out in the network shutdown
Gateway machines typically support inputs and outputs for multiple interfaces and multiple protocols; in the data common transmission mode, downlink data transmission and uplink data transmission are separately carried out through buffering, a gateway machine generates proper uplink data or downlink data according to a transmission protocol and then carries out data transmission, and in the data transparent transmission mode, the gateway machine is mainly responsible for data forwarding. In order to distinguish the common data transmission mode and realize the data transmission mode, a protocol type needs to be added for the data transmission.
The communication mode and the communication protocol are preset in a database dictionary table of the gateway machine, the communication mode comprises serial port communication (shown in table 1), the communication protocol comprises Modbus transparent transmission protocol, the communication protocol of a downlink serial port is called downlink Modbus transparent transmission protocol, and the communication protocol of an uplink serial port is called uplink Modbus transparent transmission protocol (shown in table 2).
Table 1 preset communication modes in database dictionary table of gateway machine
| Dictionary code |
Dictionary item ID |
Name of the name |
Description of the invention |
| commmode |
1 |
Serial port |
Serial port communication |
Table 2 adding communication protocols to a database dictionary table of a gateway machine
| |
|
|
|
| Dictionary code |
Dictionary item ID |
Name of the name |
Description of the invention |
| protocoltype |
11 |
MODBUS transparent transmission |
Downstream MODBUS transparent transmission protocol |
| agreementtype |
11 |
MODBUS transparent transmission |
Uplink MODBUS transparent transmission protocol |
As shown in table 3, a communication node connected to the downstream equipment is arranged in a database of the gateway device.
Table 3 configuration of communication nodes in database of network shutdown
| Fields |
Type(s) |
Description of the invention |
| Id |
Number |
Communication node ID, non-null, |
| Name |
String |
Communication node name |
| Commaddr |
String |
Communication address information |
| Commmode |
Number |
Communication method |
| Protocoltype |
Number |
Communication protocol |
The communication address information is combined information about equipment communication parameters, and for serial port communication, the communication address information comprises serial port address, communication protocol, baud rate, parity check bit, data bit, stop bit and other information, and the communication protocol adopts downlink Modbus transparent transmission protocol.
And the gateway machine needs to collect the relevant information of the sensor and the sensor data point to carry out sensor configuration and sensor data point configuration.
As shown in table 4, the sensor configuration information includes a communication node ID, a sensor ID, and a sensor communication parameter, which is a Modbus communication address of the sensor.
Table 4 sensor configuration
| Fields |
Type(s) |
Description of the invention |
| Id |
Number |
Sensor ID, non-null # |
| Name |
String |
Sensor name |
| commnodeid |
Number |
Communication node ID |
| commaddr |
String |
Sensor communication parameters |
As shown in table 5, the sensor data point configuration information includes a sensor ID, a sensor data point ID, and a sensor data point information parameter, which is combination information about the Modbus channel, and the sensor data point information parameter includes information such as a Modbus register address, a function code, a data type, a data length, a data parsing method, a decimal point number, a conversion relation, and a data offset.
Table 5 sensor data point configuration
| Fields |
Type(s) |
Description of the invention |
| Id |
Number |
Sensor data point ID, non-null |
| Name |
String |
Sensor data point name |
| sensorid |
Number |
Sensor ID |
| url |
String |
Sensor data point information parameters |
As shown in table 6, an uplink platform is configured in a database of the network shutdown, the configuration information includes a platform ID, a platform protocol and platform connection information, the platform protocol is an uplink Modbus transparent transmission protocol, the platform connection information is combination information about access parameters, and the platform connection information includes information such as serial port address, communication protocol, baud rate, parity check bit, data bit, stop bit, and the like.
Table 6 uplink platform configuration
| Fields |
Type(s) |
Description of the invention |
| Id |
Number |
Platform ID, non-null |
| Name |
String |
Platform name |
| type |
String |
Platform protocol, non-null |
| Url |
String |
Platform connectivity information |
Step2, load configuration information
Loading communication node configuration, downlink equipment configuration, equipment data point configuration and uplink platform configuration.
Step3, starting a data common transmission module and a data transparent transmission module
If one or both of the communication node and the uplink platform are not configured with the Modbus transparent transmission protocol, a data common transmission module is adopted for processing according to the current common data reporting flow.
If the communication node is configured with a downlink Modbus transparent transmission protocol and the uplink platform is configured with an uplink Modbus transparent transmission protocol, a data transparent transmission module is adopted for data transmission, and the specific steps are as follows.
And step 31, analyzing communication address information in the communication node configuration information, and opening the downlink serial port according to the serial port address, the baud rate, the parity check bit, the data bit, the stop bit and other information of the downlink serial port.
And step 32, analyzing the platform connection information in the uplink platform configuration information, and opening the uplink serial port according to the serial port address, the baud rate, the parity check bit, the data bit, the stop bit and other information of the uplink serial port.
And 33, starting a data acquisition thread, monitoring a read signal of the uplink serial port, reading all data of the uplink serial port when the read signal of the uplink serial port is monitored, and adding all the read data to the tail of the data buffer queue.
And step 34, searching the function code of the Modbus from the head of the data buffer queue, judging whether the message type belongs to the Modbus inquiry message or the Modbus control message according to the function code of the Modbus when the function code of the Modbus is identified, and extracting a frame of message according to the format requirement of the Modbus message.
The query message format is shown in table 7, and the control message format is shown in table 8.
Table 7 query message format
Table 8 control message format
Step 341, according to the Modbus protocol, if the message type belongs to the Modbus query message, in combination with table 7, judging whether the length of the received message in the data buffer queue is greater than or equal to 6, if so, taking the previous byte of the function code as the slave address, taking the last two bytes of the function code as the high bits of the register address and the low bits of the register address, taking the last two bytes of the register address as the high bits of the register number and the low bits of the register number, taking the last two bytes as the CRC check code, calculating whether the CRC check code is correct, if so, extracting a frame of message from the data buffer queue according to the format of the Modbus query message, if not, continuing to search the function code of the Modbus in the data buffer queue and judging.
Step 342, according to the Modbus protocol, if the message type belongs to the Modbus control message, in combination with table 8, the previous byte of the function code is taken as the slave address, the last two bytes of the function code are taken as the high bit of the register address and the low bit of the register address, other corresponding data and the CRC check code are obtained according to the function code type, whether the CRC check code is correct or not is calculated, if so, the Modbus control message format extracts a frame of message from the data buffer queue, and if not, the function code continues to be searched in the data buffer queue and judged.
Step 35, writing the extracted frame of message into the downlink serial port, and reading the downlink serial port response message within the overtime time, and if the read overtime or the read downlink serial port response message length is shorter than the Modbus message format requirement, discarding the read downlink serial port response message, and if the read downlink serial port response message length meets the Modbus message format requirement, writing the downlink serial port response message into the uplink serial port by combining the tables 9 and 10.
Table 9 query response message format
Table 10 controls the format of the reply message
Step 36, if the downlink serial port response message is a query response message and there is an uplink transmission requirement about the query response message based on other uplink protocols, analyzing the downlink serial port response message at the same time, acquiring a slave station address, a register address and a register length in the downlink serial port response message, matching corresponding device data points according to the slave station address, the register address and the register length, then analyzing data values of the device data points according to information such as Modbus register address, function codes, data types, data lengths, data analysis methods, decimal place numbers, conversion relations, data offsets and the like contained in the device data point information parameters, refreshing the data values of the device data points according to analysis results, and packaging the data message and transmitting the data messages uplink according to the corresponding other uplink protocol requirements.
The foregoing has shown and described the basic principles, principal features and advantages of the invention. It will be appreciated by persons skilled in the art that the above embodiments are not intended to limit the invention in any way, and that all technical solutions obtained by means of equivalent substitutions or equivalent transformations fall within the scope of the invention.