PLC communication method and system based on MQTT protocol

By creating MQTT communication threads and asynchronous clients in the PLC operating system, the direct connection between the PLC and the MQTT server is solved, and the adaptation problem between the PLC device and the MQTT protocol is improved, and data transmission efficiency and system compatibility are improved.

CN120091011AInactive Publication Date: 2025-06-03ZHEJIANG SUPCON RES +1
View PDF 3 Cites 0 Cited by

Patent Information

Application Number
CN202510572216.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-05-06
Publication Date
2025-06-03
Estimated Expiration
Not applicable · inactive patent

AI Technical Summary

Technical Problem

Under the prior art, conventional PLC devices cannot be efficiently adapted to the MQTT protocol, resulting in the inability to realize the communication function based on the MQTT protocol.

Method used

By creating an MQTT communication thread in the PLC's operating system, porting the MQTT protocol stack, and creating an MQTT asynchronous client, the direct connection between the PLC and the MQTT server is realized to perform data transmission.

Benefits of technology

It realizes efficient adaptation between PLC devices and MQTT protocols, eliminates dependence on third-party communication gateways, and improves data transmission efficiency and system compatibility and scalability.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120091011A_ABST
    Figure CN120091011A_ABST
Patent Text Reader

Abstract

The invention provides a PLC communication method and system based on an MQTT protocol, and the method comprises the steps: S1, creating an MQTT communication thread in an operation system of a PLC through an upper computer, and transplanting an MQTT protocol stack to the operation system of the PLC; s2, creating an MQTT asynchronous client in an operating system of the PLC based on an MQTT protocol stack, and enabling the MQTT asynchronous client to be in communication connection with an MQTT server through an MQTT protocol; s3, the MQTT asynchronous client reads the first data of the PLC and sends the first data of the PLC to an MQTT server, and the MQTT server further sends the first data of the PLC to external equipment which is in communication connection with the MQTT server through an MQTT protocol; and moreover, the MQTT server receives second data output by the external equipment, and the MQTT asynchronous client further obtains the second data of the external equipment output by the MQTT server and sends the second data of the external equipment to the PLC. According to the invention, the MQTT communication function of the PLC equipment can be realized.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the technical field of PLC control, and particularly relates to a PLC communication method and system based on the MQTT protocol. Background Art

[0002] MQTT (Message Queuing Telemetry Transport) is a lightweight communication protocol based on the publish / subscribe model, built on top of the TCP / IP protocol. Its greatest advantage is that it can provide real-time and reliable message services for connecting remote devices with extremely little code and limited bandwidth. With the rapid development of the Internet of Things technology, MQTT has been widely used in fields such as small devices, mobile applications, and industrial automation control.

[0003] However, under the existing technology, conventional PLC (Programmable Logic Controller) devices basically do not support data transmission based on the MQTT protocol. That is, when collecting and transmitting PLC data, an additional MQTT gateway device needs to be added, increasing the hardware usage cost. Even if some PLC devices support the MQTT protocol, they also need to download the corresponding software toolbox or perform firmware updates, and the compatibility, stability, and ease of use of the toolbox are difficult to guarantee, and the use is complex. Therefore, the efficient adaptation of conventional PLC devices and the MQTT protocol cannot be achieved. Summary of the Invention

[0004] The present invention aims to provide a PLC communication method and system based on the MQTT protocol to solve the technical problem that under the existing technology, conventional PLC devices cannot be efficiently adapted to the MQTT protocol to achieve the communication function based on the MQTT protocol.

[0005] To solve the above problems, the technical solution of the present invention is: A PLC communication method based on the MQTT protocol, including the following steps: S1: The upper computer creates an MQTT communication thread in the operating system of the PLC, migrates the MQTT protocol stack to the operating system of the PLC, and configures the MQTT protocol stack to run independently in the MQTT communication thread; S2: The upper computer creates an MQTT asynchronous client in the operating system of the PLC based on the MQTT protocol stack, connects the PLC and the MQTT server to achieve Ethernet communication, and enables the MQTT asynchronous client to communicate with the MQTT server through the MQTT protocol; S3: Configure the MQTT asynchronous client to read the first data of the PLC, and send the first data of the PLC to the MQTT server. The MQTT server further sends the first data to an external device that is communicatively connected to it via the MQTT protocol. Moreover, the MQTT server receives the second data output by the external device, configures the MQTT asynchronous client to obtain the second data of the external device relayed and output by the MQTT server, and further sends the second data to the PLC.

[0006] Preferably, creating the MQTT asynchronous client in S2 and making it communicatively connected to the MQTT server includes the following steps: S21: The host computer creates an MQTT asynchronous client connection function block and an MQTT asynchronous client disconnection function block in the PLC based on the MQTT protocol stack. S22: The host computer calls the MQTT asynchronous client connection function block in the PLC to configure the connection configuration parameters of the MQTT asynchronous client, so that the MQTT asynchronous client is communicatively connected to the MQTT server. When the MQTT asynchronous client attempts to create a connection with the MQTT server for the first time, the MQTT asynchronous client sends a first heartbeat request message to the MQTT server at a fixed interval. After receiving the first heartbeat request message, the MQTT server sends a first heartbeat response message back to the MQTT asynchronous client. If the MQTT asynchronous client does not receive the first heartbeat response message within the specified interval, the communication connection between the MQTT asynchronous client and the MQTT server fails. S23: When the MQTT asynchronous client needs to actively disconnect from the MQTT server, the host computer calls the MQTT asynchronous client disconnection function block in the PLC to enable the disconnection between the two.

[0007] Preferably, when the MQTT asynchronous client is communicatively connected to the MQTT server via the MQTT protocol in S2, it further includes the following steps: S24: The host computer calls the MQTT asynchronous client connection function block in the PLC to assign an identifier to the specified MQTT asynchronous client, and enables the TLS encryption communication setting between the MQTT asynchronous client with the corresponding identifier and the MQTT server. The host computer issues an encryption certificate file to the MQTT asynchronous client. An encrypted channel is established between the MQTT asynchronous client and the MQTT server, and a shared key is generated based on the encryption algorithm. The first data of the PLC sent by the MQTT asynchronous client to the MQTT server and the second data of the external device sent by the MQTT server to the MQTT asynchronous client are both encrypted and decrypted through the same encryption algorithm and shared key.

[0008] Preferably, in S2, the MQTT asynchronous client communicates with the MQTT server through the MQTT protocol, and further includes the following steps: S25: The host computer creates a topic message subscription function block, a topic message unsubscription function block, and a topic message publishing function block in the PLC based on the MQTT protocol stack; S26: The host computer calls the topic message subscription function block in the PLC to subscribe to the topic message types that can be sent and received for the MQTT asynchronous client with the corresponding identifier; S27: The host computer calls the topic message publishing function block in the PLC to set the MQTT server to enable the message staging function; When the PLC goes online again, the MQTT server will send all the second data that conform to the topic message types output by the external device when the MQTT asynchronous client is offline to the MQTT asynchronous client with the corresponding identifier; S28: When the MQTT asynchronous client needs to change the subscribed topic message types, the host computer calls the topic message unsubscription function block in the PLC to enable the MQTT asynchronous client to unsubscribe from the current topic message types.

[0009] Preferably, in the data interaction process between the MQTT asynchronous client and the PLC in S3, it includes the following steps: S31: The host computer creates a topic message query function block in the PLC based on the MQTT protocol stack; S32: The host computer calls the topic message query function block in the PLC to allocate the extraction function for different types of topic messages for the MQTT asynchronous client, and calls the topic message publishing function block to allocate the publishing function for different types of topic messages for the MQTT asynchronous client; In the MQTT asynchronous client, corresponding receive / send message buffer queues are generated for different topic message types, and the receive / send message buffer queues follow the principle of first in first out; After the receive message buffer queue of the MQTT asynchronous client extracts the second data of the external device output by the MQTT server, the MQTT asynchronous client maps the second data of the external device to the input storage area of the PLC and deletes the data in the receive message buffer queue; After the MQTT asynchronous client extracts the first data of the PLC from the output storage area of the PLC and maps it to its send message buffer queue, the MQTT asynchronous client sends the first data of the PLC to the MQTT server and deletes the data in the send message buffer queue.

[0010] Preferably, after the host computer calls the topic message unsubscribe function block in the PLC to set the MQTT asynchronous client to unsubscribe from any type of topic message, the MQTT asynchronous client clears the existing data in the receive / send message buffer queue corresponding to this type of topic message and deletes the receive / send message buffer queue corresponding to this type of topic message.

[0011] Preferably, when the receive message buffer queue of the MQTT asynchronous client extracts the second data of the external device output by the MQTT server, or when the MQTT asynchronous client maps the second data of the external device to the input storage area of the PLC, the receive message buffer queue adopts a mutex protection mechanism to restrict the receive message buffer queue to only perform one of the functions of data writing or data reading at the same time; When the MQTT asynchronous client extracts and maps the first data in the PLC output storage area to the send message buffer queue, or when the MQTT asynchronous client sends the first data of the PLC in the send message buffer queue to the MQTT server, the send message buffer queue adopts a mutex protection mechanism to restrict the send message buffer queue to only perform one of the functions of data writing or data reading at the same time.

[0012] Preferably, during the communication process between the MQTT asynchronous client and the MQTT server in S3, the following steps are further included: S33: The host computer creates an MQTT asynchronous client connection status query function block in the PLC based on the MQTT protocol stack; S34: The host computer calls the MQTT asynchronous client connection status query function block in the PLC to enable the MQTT asynchronous client to send a second heartbeat request message to the MQTT server at a fixed interval. After receiving the second heartbeat request message, the MQTT server sends a second heartbeat response message back to the MQTT asynchronous client to verify the real-time connection status between the MQTT asynchronous client and the MQTT server based on the heartbeat mechanism. When there is a connection anomaly between the MQTT asynchronous client and the MQTT server, the MQTT asynchronous client outputs an alarm prompt to the PLC and the host computer.

[0013] Preferably, verifying the real-time connection status between the MQTT asynchronous client and the MQTT server in S34 further includes the following steps: S35: The host computer calls the MQTT asynchronous client connection status query function block in the PLC, measures the total response time from when the MQTT asynchronous client sends the second heartbeat request message to when it receives the second heartbeat response message. When the response total time exceeds the preset time, it proves that the network is congested, and the MQTT asynchronous client reduces the data sending frequency to the MQTT server; S36: When a network congestion situation occurs, the host computer enables the MQTT asynchronous client to split the first data of the PLC and set priorities for each part of the first data, ensuring that the MQTT asynchronous client preferentially sends the high-priority part of the first data to the MQTT server.

[0014] Based on the same concept, the present invention also provides an MQTT protocol-based PLC communication system for executing the MQTT protocol-based PLC communication method described in any one of the above, including: PLC; A host computer, which is used to create an MQTT asynchronous client in the PLC and configure the communication connection between the MQTT asynchronous client and the MQTT server; The MQTT server further establishes a communication connection with an external device through the MQTT protocol, is used to receive the first data of the PLC and transfer it to the external device for transit, and receive the second data of the external device and transfer it to the PLC for transit.

[0015] Due to the above technical solutions, the present invention has the following advantages and positive effects compared with the prior art: The present invention provides a PLC communication method and system based on the MQTT protocol. The MQTT protocol stack is transplanted into the operating system of the PLC, and an MQTT asynchronous client is created. The MQTT asynchronous client communicates and connects with the MQTT server through the MQTT protocol. The MQTT asynchronous client can read the first data of the PLC and send the first data of the PLC to the MQTT server. The MQTT server further sends the first data of the PLC to an external device that communicates and connects with it through the MQTT protocol. At the same time, the MQTT server can receive the second data output by the external device. The MQTT asynchronous client further obtains the second data of the external device output by the MQTT server and sends the second data of the external device to the PLC body system. Thus, the function of PLC data communication with external devices based on the MQTT protocol is realized. In the present invention, the PLC realizes a direct connection with the MQTT server without configuring a third-party communication gateway, and is provided with seven groups of programmable function blocks, which is convenient for realizing the efficient adaptation of the PLC device to the MQTT protocol and the drive control of the MQTT asynchronous client. BRIEF DESCRIPTION OF THE DRAWINGS

[0016] Figure 1 A flowchart of a PLC communication method based on the MQTT protocol provided by the present invention; Figure 2 A structural diagram of a PLC communication system based on the MQTT protocol provided by the present invention. DETAILED DESCRIPTION OF THE EMBODIMENTS

[0017] The following further describes in detail a PLC communication method and system based on the MQTT protocol proposed by the present invention in conjunction with the accompanying drawings and specific embodiments. The advantages and features of the present invention will be clearer according to the following description and the claims.

[0018] First Embodiment Refer to Figure 1 - Figure 2 , this embodiment provides a PLC communication method based on the MQTT protocol, which is used to enable the PLC device to support the data transmission function based on the MQTT protocol. The specific steps are as follows: S1: In the operating system of the PLC that conforms to the IEC61131-3 operation standard through the host computer, create an MQTT communication thread for running the MQTT protocol stack. Subsequently, transplant the MQTT protocol stack source code into the operating system of the PLC through the host computer, and configure the MQTT protocol stack to be able to run independently in the MQTT communication thread. That is, after the PLC starts running, the PLC operating system main process and the MQTT protocol stack run in different threads, avoiding the operation of the MQTT protocol stack from interfering with the core operation logic of the PLC body.

[0019] S2: The host computer creates an MQTT asynchronous client in the operating system of the PLC based on the MQTT protocol stack, and connects the PLC and the MQTT server through a physical connection to achieve Ethernet communication between the two, so that the MQTT asynchronous client can directly communicate with the MQTT server through the MQTT protocol.

[0020] S3: The MQTT asynchronous client can read the first data of the PLC, and then send the first data of the PLC to the MQTT server. The MQTT server further sends the first data to an external device that is communicatively connected to it through the MQTT protocol.

[0021] Conversely, the MQTT server can receive the second data output by the external device. Subsequently, the MQTT asynchronous client further obtains the second data output by the MQTT server and synchronizes the second data of the external device to the PLC, thereby realizing the MQTT communication function between the PLC, the MQTT server, and the external device.

[0022] Among them, the first data of the PLC includes the status data, measurement data, or control instructions output to the outside of the PLC body. In one embodiment, the control instructions of the PLC can be relayed through the MQTT server and sent to an external device, and then the PLC controls the external device to operate. Similarly, the second data of the external device includes its status data, measurement data, or control instructions output to the outside. In one embodiment, the control instructions of the external device can also be relayed through the MQTT server and sent to the PLC, and then the external device controls the PLC to operate.

[0023] It should be noted that in this embodiment, the external device can include sensor devices, actuator devices, human-computer interaction devices, etc. The MQTT communication function connection method between the external device and the MQTT server is not specifically limited in this embodiment.

[0024] Next, the specific steps and functions of a PLC communication method based on the MQTT protocol provided in this embodiment will be further described in detail: It should be noted that the host computer in the PLC can create at least an MQTT asynchronous client connection function block, an MQTT asynchronous client disconnection function block, an MQTT asynchronous client connection status query function block, a topic message publishing function block, a topic message subscribing function block, a topic message unsubscribing function block, and a topic message query function block based on the MQTT protocol stack, and realize multiple functions in the MQTT communication process through the design and invocation of seven different programmable function blocks.

[0025] Among them, the MQTT asynchronous client connection function block is used to create an MQTT asynchronous client and establish a first connection with a specified MQTT server.

[0026] The MQTT asynchronous client disconnection function block is used to disconnect the MQTT asynchronous client from the MQTT server.

[0027] The MQTT asynchronous client connection status query function block is used to query the real-time connection status and communication status between the MQTT asynchronous client and the MQTT server after confirming that the connection has been established.

[0028] The topic message publishing function block is used to enable the MQTT asynchronous client to publish topic messages of a specified topic type.

[0029] The topic message subscription function block is used to enable the MQTT asynchronous client to subscribe to one or more topics, so that the subsequent MQTT asynchronous client can receive topic messages from the MQTT server that match the subscribed topic type.

[0030] The topic message unsubscription function block is used to enable the MQTT asynchronous client to unsubscribe from one or more topics, so that the subsequent MQTT asynchronous client stops receiving topic messages from the MQTT server or PLC that match the topic type.

[0031] The topic message query function block is used to enable the MQTT asynchronous client to extract topic messages from the MQTT server or PLC that match the subscribed topic type.

[0032] Preferably, in one embodiment, creating an MQTT asynchronous client in S2 and making it communicate with the MQTT server specifically includes the following steps: S21: The host computer creates an MQTT asynchronous client connection function block and an MQTT asynchronous client disconnection function block in the PLC based on the MQTT protocol stack.

[0033] S22: The host computer calls the MQTT asynchronous client connection function block in the PLC to configure each connection configuration parameter of the MQTT asynchronous client (including the MQTT server connection address, TLS / SSI encryption option, MQTT asynchronous client ID, user name, password, heartbeat time, will topic, quality of service, message retention, will message, and return message, etc.), so that the MQTT asynchronous client creates and establishes a communication connection with the MQTT server.

[0034] When the MQTT asynchronous client first creates a connection with the MQTT server, the MQTT asynchronous client sends a first heartbeat request message to the MQTT server at a fixed interval. After receiving the first heartbeat request message, the MQTT server sends a corresponding first heartbeat response message back to the MQTT asynchronous client. If the MQTT asynchronous client does not receive the first heartbeat response message within the specified interval, the MQTT asynchronous client, based on the MQTT asynchronous client connection function block, retries to establish a communication connection with the MQTT server until the upper limit of the reconnection times is reached, which proves that the communication connection between the MQTT asynchronous client and the MQTT server fails, and an alarm is issued indicating that there is an abnormal connection between the MQTT asynchronous client and the MQTT server. In this embodiment, through the heartbeat mechanism, it can be determined whether the first connection between the MQTT asynchronous client and the MQTT server is successful, and through the communication experiment of the first heartbeat request message and the first heartbeat response message, the data transmission accuracy and stability during the first connection between the MQTT asynchronous client and the MQTT server can be verified.

[0035] S23: When the MQTT asynchronous client needs to actively disconnect from the MQTT server, the host computer can enable the disconnection between the two by calling the MQTT asynchronous client disconnection function block in the PLC.

[0036] Preferably, in one embodiment, when the MQTT asynchronous client communicates with the MQTT server through the MQTT protocol in S2, the following steps are further included: S24: The host computer calls the MQTT asynchronous client connection function block in the PLC to assign an identifier to the specified MQTT asynchronous client. When multiple different PLCs and MQTT asynchronous clients are connected to the same MQTT server at the same time, the identifier, such as the ID number, can be used to mark different MQTT asynchronous clients to ensure that the subsequent MQTT server can correctly identify and manage the connection, message subscription, and message transmission of each MQTT asynchronous client.

[0037] Meanwhile, the host computer calls the MQTT asynchronous client connection function block in the PLC to enable the TLS encryption communication setting between the MQTT asynchronous client with the corresponding identifier and the MQTT server, including: The host computer sends the encryption certificate file to the MQTT asynchronous client, and an encrypted channel is established between the MQTT asynchronous client and the MQTT server, and a shared key is generated based on the encryption algorithm.

[0038] Specifically, the MQTT asynchronous client sends a ClientHello message to the MQTT server based on an encrypted certificate file, and informs it of the supported encryption protocols, versions, and encryption algorithms. The MQTT server responds to the MQTT asynchronous client with a ServerHello message. Then, the MQTT asynchronous client selects an encryption protocol and algorithm supported by both parties. Subsequently, the MQTT server verifies the identity legality of the MQTT asynchronous client, and finally, both parties jointly generate a set of shared keys based on the encryption algorithm.

[0039] In this embodiment, the first data of the PLC sent by the MQTT asynchronous client to the MQTT server and the second data of the external device sent by the MQTT server to the MQTT asynchronous client are both encrypted and decrypted through the same encryption algorithm and shared key, thereby improving the security of data transmission and preventing the data from being stolen or tampered with during the transmission process.

[0040] Preferably, in one embodiment, in S2, the MQTT asynchronous client communicates with the MQTT server through the MQTT protocol, and further includes the following steps: S25: The host computer creates a topic message subscription function block, a topic message unsubscription function block, and a topic message publishing function block in the PLC based on the MQTT protocol stack.

[0041] S26: The host computer calls the topic message subscription function block in the PLC to subscribe to the topic message types that the MQTT asynchronous client with the corresponding identifier can send and receive.

[0042] S27: The host computer calls the topic message publishing function block in the PLC to set the MQTT server to enable the message staging function.

[0043] When the PLC reconnects after going offline, the MQTT server will send all the second data that conforms to the topic message type output by the external device when the MQTT asynchronous client is offline to the MQTT asynchronous client with the corresponding identifier.

[0044] That is, in this embodiment, the communication connection between the MQTT asynchronous client and the MQTT server has the function of message staging. When the MQTT asynchronous client with the corresponding identifier is temporarily offline, as long as the MQTT server maintains a normal MQTT connection with the external device, the MQTT server can still receive the second data output by the external device and stage the second data output by the external device. After the PLC and the MQTT asynchronous client reconnect, the MQTT server will resend the second data output by the external device to the MQTT asynchronous client to ensure the integrity of data transmission and avoid data loss caused by the temporary disconnection of the MQTT asynchronous client.

[0045] Similarly, when the external device is in an offline state, the first data of the PLC output by the MQTT asynchronous client will also be temporarily stored in the MQTT server. When the external device comes back online, the MQTT server will resend the topic messages that match the topic message type subscribed by the external device to the external device.

[0046] S28: When the MQTT asynchronous client needs to change the subscribed topic message type, the host computer calls the topic message unsubscribe function block in the PLC to enable the MQTT asynchronous client to unsubscribe from the current topic message type.

[0047] Preferably, in one embodiment, in S3, the MQTT asynchronous client receives the second data of the external device and sends it to the PLC, or sends the first data of the PLC to the external device, including the following steps: S31: The host computer creates a topic message query function block in the PLC based on the MQTT protocol stack.

[0048] S32: After the MQTT asynchronous client subscribes to the sendable and receivable topic message types in S26, the host computer calls the topic message query function block in the PLC to allocate the extraction function for the MQTT asynchronous client for different types of topic messages, and calls the topic message publish function block to allocate the publishing function for the MQTT asynchronous client for different types of topic messages.

[0049] In the MQTT asynchronous client, corresponding receive message buffer queues and send message buffer queues are generated for different topic message types, and both the receive / send message buffer queues follow the first-in, first-out (FIFO) execution principle.

[0050] After the receive message buffer queue of the MQTT asynchronous client extracts the second data of the external device output by the MQTT server, the MQTT asynchronous client maps the second data of the external device to the input storage area of the PLC and deletes the data in the receive message buffer queue in a timely manner.

[0051] Similarly, when there is first data output by the PLC, the first data of the PLC is temporarily stored in the output storage area of the PLC. The MQTT asynchronous client extracts the first data of the PLC from the output storage area of the PLC and maps it to the send message buffer queue. After sending the first data of the PLC from the send message buffer queue to the MQTT server, the data in the receive message buffer queue will be deleted in a timely manner.

[0052] In this embodiment, by setting the receive / send message buffer queues, the situation of message disorder or loss can be avoided. And when the data is completed with mapping processing or transmission, the data is deleted from the receive / send message buffer queues in a timely manner to release the queue space and avoid queue overload or excessive memory occupation.

[0053] Preferably, in one embodiment, when the host computer calls the topic message unsubscribe function block in the PLC to set the MQTT asynchronous client to unsubscribe any type of topic message, the MQTT asynchronous client will clear the existing data in the receive / send message buffer queue corresponding to the type of topic message, and delete the receive / send message buffer queue corresponding to the type of topic message, thereby releasing system resources, avoiding the accumulation of useless data, and improving the real-time performance and response speed of the system.

[0054] Preferably, in one embodiment, when the receive message buffer queue of the MQTT asynchronous client extracts and writes the second data of the external device output to the MQTT server, or when the MQTT asynchronous client maps the second data of the external device from the receive message buffer queue to the input storage area of the PLC, the receive message buffer queue adopts a mutex protection mechanism to restrict the receive message buffer queue to only execute either the data write or data read function at the same time.

[0055] Similarly, when the first PLC data in the PLC output storage area is extracted and mapped to the send message buffer queue of the MQTT asynchronous client, or when the MQTT asynchronous client sends the first data of the PLC to the MQTT server through the send message buffer queue, the send message buffer queue also adopts a mutex protection mechanism to restrict the send message buffer queue to only execute either the data write or data read function at the same time.

[0056] That is, in this embodiment, since the PLC operating system main process and the MQTT protocol stack run in different threads, in a multi-threaded environment, through the respective mutex protection mechanisms of the receive / send message buffer queues, it is possible to prevent multiple threads from accessing the receive / send message buffer queues simultaneously at the same time, causing data competition. That is, based on this embodiment, the integrity and orderliness of the data in the receive / send message buffer queues can be guaranteed.

[0057] Preferably, in one embodiment, during the communication between the MQTT asynchronous client and the MQTT server in S3, the following steps are further included: S33: The host computer creates an MQTT asynchronous client connection status query function block in the PLC based on the MQTT protocol stack.

[0058] S34: The host computer calls the MQTT asynchronous client connection status query function block in the PLC, enables the MQTT asynchronous client to send the second heartbeat request message to the MQTT server at a fixed interval, and the MQTT server sends the second heartbeat response message back to the MQTT asynchronous client after receiving the second heartbeat request message. Based on the heartbeat mechanism, the real-time connection status between the MQTT asynchronous client and the MQTT server is verified. When there is a connection anomaly between the MQTT asynchronous client and the MQTT server, the MQTT asynchronous client outputs an alarm prompt to the PLC and the host computer.

[0059] In this embodiment, the connection status between the MQTT asynchronous client and the server can be accurately monitored through the heartbeat mechanism, and the fault status can be quickly detected and responded to.

[0060] Preferably, in one embodiment, verifying the real-time connection status between the MQTT asynchronous client and the MQTT server in S34 further includes the following steps: S35: The host computer calls the MQTT asynchronous client connection status query function block in the PLC, times the total response time from the MQTT asynchronous client sending the second heartbeat request message to receiving the second heartbeat response message. When the total response time exceeds the preset time, it proves that the network is congested, and the MQTT asynchronous client reduces the data sending frequency to the MQTT server.

[0061] S36: When a network congestion situation occurs, the host computer enables the MQTT asynchronous client to split the first data of the PLC and set priorities for each part of the first data respectively, ensuring that the MQTT asynchronous client preferentially sends the high-priority part of the first data to the MQTT server.

[0062] In this embodiment, when network congestion is detected, reducing the data sending frequency helps to reduce the network burden and avoid further deterioration of the network condition. By dynamically adjusting the sending rate, the limited network bandwidth can be more effectively utilized, thereby optimizing the use of network resources.

[0063] Meanwhile, in the case of network congestion, splitting the first data of the PLC and setting different priorities for each part ensures that high-priority data (such as emergency control commands or critical status updates) can be preferentially sent and received, thus ensuring the normal operation of the core functions of the system, improving the success rate of critical data transmission, and enhancing the stability and reliability of the system.

[0064] In summary, this embodiment provides a PLC communication method based on the MQTT protocol. The MQTT protocol stack is transplanted into the operating system of the PLC, and an MQTT asynchronous client is created. The MQTT asynchronous client communicates with the MQTT server through the MQTT protocol. The MQTT asynchronous client can read the first data of the PLC and send the first data of the PLC to the MQTT server. The MQTT server further sends the first data of the PLC to an external device that communicates with it through the MQTT protocol. At the same time, the MQTT server can receive the second data output by the external device. The MQTT asynchronous client further obtains the second data of the external device output by the MQTT server and sends the second data of the external device to the PLC. Thus, the function of data communication between the PLC and the external device based on the MQTT protocol is realized. In this embodiment, the PLC realizes a direct connection with the MQTT server without configuring a third-party communication gateway, and is provided with seven groups of programmable function blocks to achieve efficient adaptation of the PLC device to the MQTT protocol and drive control of the MQTT asynchronous client.

[0065] Second Embodiment See Figure 2 , based on the same concept, this embodiment provides a PLC communication system based on the MQTT protocol, which is used to execute the PLC communication method based on the MQTT protocol provided in the first embodiment, including a PLC, a host computer, an MQTT server, and an external device.

[0066] Among them, the PLC realizes a communication connection with the MQTT server through an Ethernet port, and the host computer creates an MQTT asynchronous client in the PLC and configures the communication connection between the MQTT asynchronous client and the MQTT server. Thus, data interaction between the PLC and the MQTT server is realized through the MQTT protocol.

[0067] The MQTT server further establishes a communication connection with the external device through the MQTT protocol, that is, the MQTT server is used to receive the first data of the PLC and transfer it to the external device, and receive the second data of the external device and transfer it to the PLC.

[0068] In this embodiment, by using the MQTT protocol to establish communication between the PLC, the MQTT server, and the external device, the communication overhead can be significantly reduced, the data transmission efficiency can be improved, and the communication between the PLC and the external device is directly relayed through the MQTT server to achieve data decoupling between the two, without the need to configure a third-party communication gateway, and has better compatibility and scalability.

[0069] The embodiments of the present invention have been described in detail above with reference to the accompanying drawings. However, the present invention is not limited to the above embodiments. Even if various changes are made to the present invention, provided that these changes fall within the scope of the claims of the present invention and equivalent technologies, they still fall within the protection scope of the present invention.

Claims

1. A PLC communication method based on the MQTT protocol, characterized in that: The steps include: S1: The host computer creates an MQTT communication thread in the operating system of the PLC, transplants the MQTT protocol stack to the operating system of the PLC, and configures the MQTT protocol stack to run independently in the MQTT communication thread; S2: The host computer creates an MQTT asynchronous client in the operating system of the PLC based on the MQTT protocol stack, connects the PLC with the MQTT server to realize Ethernet communication, and enables the MQTT asynchronous client to communicate with the MQTT server through the MQTT protocol; S3: Configure the MQTT asynchronous client to read the first data of the PLC, and send the first data of the PLC to the MQTT server, and the MQTT server further sends the first data to an external device connected to it through the MQTT protocol; and, the MQTT server receives the second data output by the external device, configures the MQTT asynchronous client to obtain the second data of the external device output via the MQTT server, and further sends the second data to the PLC.

2. The PLC communication method based on the MQTT protocol as claimed in claim 1, characterized in that: In S2, the MQTT asynchronous client is created and connected to the MQTT server for communication, including the following steps: S21: The host computer creates an MQTT asynchronous client connection function block and an MQTT asynchronous client disconnection function block in the PLC based on the MQTT protocol stack; S22: The host computer calls the MQTT asynchronous client connection function block in the PLC to configure the connection configuration parameters of the MQTT asynchronous client, so that the MQTT asynchronous client communicates with the MQTT server; When the MQTT asynchronous client and the MQTT server attempt to establish a connection for the first time, the MQTT asynchronous client sends a first heartbeat request message to the MQTT server at a fixed interval, and the MQTT server returns a first heartbeat response message to the MQTT asynchronous client after receiving the first heartbeat request message. If the MQTT asynchronous client does not receive the first heartbeat response message within the specified interval, the communication connection between the MQTT asynchronous client and the MQTT server fails; S23: When the MQTT asynchronous client needs to actively disconnect from the MQTT server, the host computer calls the MQTT asynchronous client disconnection function block in the PLC to enable the two to disconnect.

3. The PLC communication method based on the MQTT protocol as claimed in claim 2, characterized in that: In S2, the MQTT asynchronous client communicates with the MQTT server through the MQTT protocol, and further includes the following steps: S24: the host computer calls the MQTT asynchronous client connection function block in the PLC to assign an identifier to the specified MQTT asynchronous client, and enables the TLS encrypted communication setting between the MQTT asynchronous client with the corresponding identifier and the MQTT server; The host computer sends an encryption certificate file to the MQTT asynchronous client, an encrypted channel is established between the MQTT asynchronous client and the MQTT server, and a shared key is generated based on an encryption algorithm; The first data of the PLC sent by the MQTT asynchronous client to the MQTT server, and the second data of the external device sent by the MQTT server to the MQTT asynchronous client, are both encrypted and decrypted using the same encryption algorithm and shared key.

4. The PLC communication method based on the MQTT protocol as claimed in claim 3, characterized in that: In S2, the MQTT asynchronous client is connected to the MQTT server through the MQTT protocol, and further includes the following steps: S25: The host computer creates a topic message subscription function block, a topic message unsubscription function block and a topic message publishing function block in the PLC based on the MQTT protocol stack; S26: The host computer calls the topic message subscription function block in the PLC to subscribe to the topic message type that can be sent and received by the MQTT asynchronous client corresponding to the identifier; S27: The host computer calls the topic message publishing function block in the PLC to set the MQTT server to enable the message temporary storage function; When the PLC is back online, the MQTT server sends to the MQTT asynchronous client of the corresponding identifier all second data conforming to the topic message type output by the external device when the MQTT asynchronous client is in an offline state; S28: When the MQTT asynchronous client needs to change the subscribed topic message type, the host computer calls the topic message unsubscription function block in the PLC to enable the MQTT asynchronous client to unsubscribe from the current topic message type.

5. The PLC communication method based on the MQTT protocol as claimed in claim 1, characterized in that: The data interaction process between the MQTT asynchronous client and the PLC in S3 includes the following steps: S31: The host computer creates a topic message query function block in the PLC based on the MQTT protocol stack; S32: the host computer calls the topic message query function block in the PLC to allocate the extraction function for different types of topic messages to the MQTT asynchronous client, and calls the topic message publishing function block to allocate the publishing function for different types of topic messages to the MQTT asynchronous client; In the MQTT asynchronous client, corresponding receive / send message buffer queues are generated for different topic message types, and the receive / send message buffer queues adopt the first-in-first-out execution principle; When the receiving message buffer queue of the MQTT asynchronous client extracts the second data of the external device output by the MQTT server, the MQTT asynchronous client maps the second data of the external device to the input storage area of ​​the PLC and deletes the data in the receiving message buffer queue; After the MQTT asynchronous client extracts the first data of the PLC from the output storage area of ​​the PLC and maps it to its sending message buffer queue, the MQTT asynchronous client sends the first data of the PLC to the MQTT server and deletes the data in the sending message buffer queue.

6. The PLC communication method based on the MQTT protocol as claimed in claim 5, characterized in that: When the host computer calls the topic message unsubscription function block in the PLC to set the MQTT asynchronous client to unsubscribe from any type of topic message, the MQTT asynchronous client clears the existing data in the receive / send message buffer queue corresponding to the type of topic message, and deletes the receive / send message buffer queue corresponding to the type of topic message.

7. The PLC communication method based on the MQTT protocol as claimed in claim 5, characterized in that: When the receiving message buffer queue of the MQTT asynchronous client extracts the second data of the external device output by the MQTT server, or when the MQTT asynchronous client maps the second data of the external device to the input storage area of ​​the PLC, the receiving message buffer queue adopts a mutex protection mechanism to limit the receiving message buffer queue to only execute one of the functions of data writing or data reading at the same time; When the MQTT asynchronous client extracts the first data in the PLC output storage area and maps it to the sending message buffer queue, or when the MQTT asynchronous client sends the first data of the PLC in the sending message buffer queue to the MQTT server, the sending message buffer queue adopts a mutex protection mechanism to limit the sending message buffer queue to only execute either data writing or data reading at the same time.

8. The PLC communication method based on the MQTT protocol according to claim 1, characterized in that: In the communication process between the MQTT asynchronous client and the MQTT server in S3, the following steps are also included: S33: The host computer creates an MQTT asynchronous client connection status query function block in the PLC based on the MQTT protocol stack; S34: The host computer calls the MQTT asynchronous client connection status query function block in the PLC, enabling the MQTT asynchronous client to send a second heartbeat request message to the MQTT server at a fixed interval. After receiving the second heartbeat request message, the MQTT server returns a second heartbeat response message to the MQTT asynchronous client, and verifies the real-time connection status between the MQTT asynchronous client and the MQTT server based on the heartbeat mechanism. When there is a connection abnormality between the MQTT asynchronous client and the MQTT server, the MQTT asynchronous client outputs an alarm prompt to the PLC and the host computer.

9. The PLC communication method based on the MQTT protocol as claimed in claim 8, characterized in that: Verifying the real-time connection status between the MQTT asynchronous client and the MQTT server in S34 further includes the following steps: S35: The host computer calls the MQTT asynchronous client connection status query function block in the PLC, and times the total response time from the MQTT asynchronous client sending the second heartbeat request message to receiving the second heartbeat response message. When the total response time exceeds the preset time, it proves that the network is congested, and the MQTT asynchronous client reduces the frequency of data transmission to the MQTT server; S36: When network congestion occurs, the host computer enables the MQTT asynchronous client to split the first data of the PLC and set a priority for each portion of the first data, to ensure that the MQTT asynchronous client sends high-priority portions of the first data to the MQTT server first.

10. A PLC communication system based on the MQTT protocol, characterized in that: Used to execute the PLC communication method based on the MQTT protocol as described in any one of claims 1 to 9, comprising: PLC; A host computer, the host computer is used to create an MQTT asynchronous client in the PLC and configure a communication connection between the MQTT asynchronous client and an MQTT server; The MQTT server further establishes a communication connection with an external device through the MQTT protocol, and is used to receive the first data of the PLC and transfer it to the external device, and receive the second data of the external device and transfer it to the PLC.

Citation Information

Patent Citations

  • Modbus communication method and system based on MQTT cloud platform

    CN112350914A

  • Communication method and device and message queue telemetry transport protocol client

    CN115567619A

  • Systems and methods for controlling and monitoring farms

    US20220187780A1