ModBus-based data interaction method during configuration software operation

By using the semaphore mechanism in the interaction between PLC and ModBus protocol devices, the problem of incomplete data interaction in the interaction between PLC and ModBus devices is solved, and the integrity and consistency of data interaction is achieved, and packet loss and data loss are avoided.

CN118945001APending Publication Date: 2024-11-12GUODIAN NANJING AUTOMATION
View PDF 0 Cites 1 Cited by

Patent Information

Application Number
CN202410973615.6
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-07-19
Publication Date
2024-11-12

AI Technical Summary

Technical Problem

In the prior art, when PLC interacts with ModBus protocol devices, the integrity and consistency of data interactions cannot be effectively solved, resulting in packet loss and data loss.

Method used

Using a semaphore-based mechanism, different semaphores are used at different times, indicating that the data is updated from the PLC's data storage area to the protocol stack storage area or vice versa. The PLC is configured as the ModBus master or slave station through the configuration software, and the use Receive or send semaphores to synchronize the operation of the protocol stack and the PLC data storage area to ensure the integrity and consistency of data interaction.

Benefits of technology

Through the semaphore mechanism, the data interaction integrity and consistency between the PLC, configuration software and external devices are ensured, and the data is correctly presented and accurately written to external devices in the configuration software, so as to avoid packet loss and data loss.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118945001A_ABST
    Figure CN118945001A_ABST
Patent Text Reader

Abstract

The invention provides a ModBus-based data interaction method during configuration software operation, when configuration software configures a PLC (Programmable Logic Controller) as a ModBus master station, the method comprises the following steps: according to a read-write function code, using different semaphores to represent the start or end of interaction between a protocol stack and a PLC data storage area, so as to ensure that the PLC reads data of external equipment, and the data is completely and correctly presented in the configuration software; it is ensured that data configured by the configuration software are accurately written into external equipment; when the configuration software configures the PLC as the ModBus slave station, according to a read-write request of an external master station, a protocol stack interacts with a PLC storage area by using different semaphores, and when interaction is finished, the protocol stack replies to the external master station to ensure that the ModBus slave station can completely and accurately interact with the configuration software every time the ModBus slave station receives the request of the external master station. Based on a semaphore mechanism, the integrity and consistency of data interaction between the configuration software and the PLC are ensured.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of communication technology, and in particular to a data interaction method for configuration software runtime based on ModBus. Background Art

[0002] Programmable logic controller (PLC) is a special computer for automation control, which is widely used in industrial production. It originated in the 1960s and was originally used in the automotive manufacturing industry. PLC realizes automation control tasks by reading input signals, performing logical operations, and controlling outputs. The background of PLC is closely related to the development of industrial automation. With the increase in automation needs, PLC has gradually replaced the traditional relay control system. Its advantages include programmability, flexibility, reliability and efficiency, making it an indispensable and important part of modern industrial production. Configuration software is an application used to configure and program programmable logic controller (PLC). It provides a graphical interface that allows users to create logic control programs, define input and output signals, set parameters, etc. Configuration software usually has intuitive drag-and-drop functions and rich libraries, allowing users to quickly build complex control systems. Configuration software acts as a programming tool for PLC, enabling engineers to easily develop, debug and maintain control logic. Through configuration software, users can intuitively understand and edit PLC programs, thereby achieving flexibility and reliability of automation control systems.

[0003] The data storage area of ​​the PLC is an important part of the PLC's internal memory, which is used to store various data related to user programs and working status. The data storage area usually includes multiple types of registers for storing different types of data. These data can be the status of input signals, intermediate calculation results, the status of output signals, and user-defined variables. In the configuration software, the PLC generally uses the input storage area (I area) and the output storage area (Q area). The I area stores the status information of the input signal and is usually used to store the status of digital input signals. The Q area stores the status information of the output signal and is usually used to store the status of digital output signals. The data in these two areas are usually read, processed, and written in the PLC program to realize the functions of the automation control system. In addition, the PLC also has a special storage area (M area). The M area is a special data storage area in the programmable logic controller (PLC) for storing internal flags and intermediate variables. The M area is usually used to store temporary data, intermediate calculation results, and specific flag bits during program execution. Unlike the I area and the Q area, the data in the M area is usually generated and used by logical operations or specific functional instructions in the program.

[0004] Currently, most configuration software on the market usually provide support for the ModBus (protocol name) protocol, including configuring ModBus communication parameters (such as serial port settings, IP address, port number, etc.), creating ModBus communication channels, defining ModBus data points, etc. Through the configuration software, users can easily communicate with PLC and other devices that support the ModBus protocol to achieve data exchange and control between devices. The ModBus protocol standard also defines several common data storage areas, including coils, discrete coils, holding registers, and input registers, among which coils and holding registers have both read and write properties, while discrete coils and discrete registers have only read-only properties. Most of these configuration software directly interact the PLC data storage area with the ModBus protocol stack data storage area. There is no clear solution for the integrity and consistency of data interaction when the PLC performs task cycle scheduling.

[0005] The prior art is generally applicable to the configuration software configuring the ModBus protocol for the PLC to interact with the bus device. The PLC obtains the corresponding data after parsing the message through the protocol stack and stores it in the ModBus storage area before responding. When the bus device is bound to the PLC periodic task, its data reading or writing depends on the I area or Q area of ​​the PLC. For some tasks with a long cycle time, the PLC data storage area will not be updated before the ModBus communication ends, so the next request or reply will be made, which will cause packet loss and data loss.

[0006] Currently, no effective solution has been proposed for the problems in the related technologies. Summary of the invention

[0007] In view of this, the present invention provides a data interaction method for configuration software runtime based on ModBus to solve the above-mentioned problems that may cause packet loss and data loss.

[0008] In order to solve the above problems, the specific technical solutions adopted by the present invention are as follows: According to one aspect of the present invention, a data interaction method for configuration software runtime based on ModBus is provided, and the data interaction method for configuration software runtime based on ModBus comprises the following steps: S1. The configuration software configures the PLC as a ModBus master station, configures the function code for the PLC and establishes a communication connection with the ModBus slave station. The function code includes a read function code and a write function code. S2. According to the function code type, the ModBus master sends the function code to the ModBus slave in a pre-set manner to realize data interaction.

[0009] Preferably, the configuration software configures the PLC as a ModBus master station, configures the function code for the PLC and establishes a communication connection with the ModBus slave station, including the following steps: S11, based on the preset configuration mode, using the configuration software to configure the PLC as a ModBus master station; S12, configuring a function code for the PLC according to a preset function code type; S13, according to the configuration mode of the PLC, select the corresponding protocol stack to establish a connection with the ModBus slave station; S14. Use the running part of the configuration software to start the ModBus master station program.

[0010] Preferably, according to the function code type, the ModBus master station sends the function code to the ModBus slave station in a preset manner to realize data interaction, including the following steps: S21. If the function code is a read function code, the ModBus master station sends a read function code message to the ModBus slave station and performs data interaction based on the feedback result of the ModBus slave station; S22. If the function code is a write function code, the data in the PLC output storage area is updated synchronously, a write function code message is constructed and sent to the ModBus slave station through the ModBus master station to realize data interaction.

[0011] Preferably, reading the function code message includes reading coils, reading discrete coils, reading holding registers and reading input registers.

[0012] Preferably, if the function code is a read function code, the ModBus master station sends a read function code message to the ModBus slave station and performs data interaction based on the feedback result of the ModBus slave station, including the following steps: S211, after the ModBus master station program is started, the ModBus master station sends a message with a read function code to the ModBus slave station; S212, the ModBus slave replies with a message containing the required data to the ModBus master and sets the receiving signal quantity through the ModBus master; S213, the protocol stack storage area obtains the received signal amount in real time, and updates the protocol stack storage area data to the PLC input storage area after obtaining the received signal amount.

[0013] Preferably, if the function code is a write function code, the PLC output storage area data is synchronously updated, a write function code message is constructed and sent to the ModBus slave station through the ModBus master station to realize data interaction, including the following steps: S221, after the ModBus master station program is started, the configuration software updates the PLC output storage area data according to its task execution cycle; S222, the PLC output storage area synchronizes the updated data to the protocol stack storage area, and sets the sending signal quantity through the protocol stack storage area; S223, the ModBus master station obtains the sending signal quantity in real time, and after obtaining the sending signal quantity, constructs a write function code message and sends it to the ModBus slave station.

[0014] According to another aspect of the present invention, a data interaction method for configuration software runtime based on ModBus is provided, and the data interaction method for configuration software runtime based on ModBus comprises the following steps: S1', the configuration software configures the PLC as a ModBus slave and sets the PLC protocol stack storage area size; S2', the ModBus slave station receives the function code request sent by the ModBus master station, and constructs a reply message in a preset manner according to the function code request type and sends it to the ModBus master station.

[0015] Preferably, the ModBus slave station receives the function code request sent by the ModBus master station, and constructs a reply message in a preset manner according to the function code request type and sends it to the ModBus master station, including: S21', when the ModBus slave station receives the function code request, it parses the function code request to obtain the function code request type, which includes a read function code request and a write function code request; S22', if the function code request type is a read function code request, the ModBus slave station constructs a reply message and sends it to the ModBus master station after receiving the signal that the storage area data update is completed; S23', if the function code request type is a write function code request, the storage area data is updated through the ModBus slave station and a reply message is constructed and sent to the ModBus master station after the update is completed.

[0016] Preferably, if the function code request type is a read function code request, the ModBus slave station constructs a reply message and sends it to the ModBus master station after receiving the signal that the storage area data update is completed, including the following steps: S221', when the ModBus slave station receives the read function code request from the ModBus master station, the ModBus slave station is set to a waiting-to-receive state; S222', based on the task execution cycle of the configuration software, update the data to the PLC storage area, set the sending signal quantity and send it to the ModBus slave station; S223', after receiving the sent signal, the ModBus slave station constructs a reply message to the master station according to the obtained function code.

[0017] Preferably, updating data to the PLC storage area based on the task execution cycle of the configuration software, setting the sending signal quantity and sending it to the ModBus slave station includes the following steps: S2221', the configuration software updates the PLC output storage area and PLC special storage area after each task cycle execution; S2222', update the data in the PLC special storage area to the PLC protocol stack storage area of ​​the coil and holding register; S2223', update the PLC output storage area data to the PLC protocol stack storage area of ​​the discrete coil and input register; S2224', after the PLC memory data is updated, set the send signal quantity and send it to the ModBus slave station.

[0018] Preferably, if the function code request type is a write function code request, the storage area data is updated through the ModBus slave station and a reply message is constructed and sent to the ModBus master station after the update is completed; S231', when the ModBus slave station receives the write function code request from the ModBus master station, it parses the write function code request and updates the parsed data to the protocol stack storage area; S232', synchronize the data to the PLC storage area through the protocol stack storage area, set the reply signal and send it to the ModBus slave station; S233', when the ModBus slave station receives the reply signal, it constructs a reply message to the ModBus master station.

[0019] Preferably, synchronizing the data to the PLC storage area through the protocol stack storage area, setting the reply signal amount and sending it to the ModBus slave station includes the following steps: S2321', parsing the write function code request to obtain the write function code request type, which includes write coil, write save register, write discrete coil and write input register; S2322', if the write function code request type is write coil or write save register, the data is synchronized to the PLC special storage area; S2323', if the write function code request type is to write discrete coils or write input registers, the data is synchronized to the PLC input storage area.

[0020] The beneficial effects of the present invention are: 1. The present invention is based on the mechanism of semaphore and uses different semaphores at different times, which respectively indicate the end of data updating from the data storage area of ​​the PLC to the protocol stack storage area or the end of data updating from the protocol stack storage area to the PLC data storage area. The protocol stack can complete the construction and sending of the request message or the reply message according to the reception of the semaphore, thereby ensuring the integrity and consistency of data interaction between the PLC, configuration software, and external devices.

[0021] 2. When the present invention configures the PLC as a ModBus master station through the configuration software, it uses the receiving or sending signal quantity to indicate the start or end of the synchronization operation between the protocol stack and the PLC data storage area according to the read-write function code, thereby ensuring that the data read by the PLC from the external device can be completely and correctly presented in the configuration software, and also ensuring that the data configured by the configuration software user is accurately written into the external device.

[0022] 3. When the present invention configures PLC as a ModBus slave station through configuration software, after receiving a read or write request from an external master station, the data storage area of ​​the protocol stack is updated and different semaphores are constructed according to different function codes, and the input storage area or output storage area of ​​the PLC is interacted with according to the attributes of the semaphore. When the data storage area of ​​the PLC is updated, the runtime of the PLC sends a reply semaphore to the protocol stack to notify the protocol stack to reply to the external master station. This operation can ensure that the ModBus slave station can interact with the configuration software completely and accurately every time it receives an external request. BRIEF DESCRIPTION OF THE DRAWINGS

[0023] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the following briefly introduces the drawings required for use in the embodiments. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work. In the drawings: Figure 1 It is a flow chart of a data interaction method of configuration software runtime based on ModBus according to an embodiment of the present invention; Figure 2 It is a flow chart of setting the read mode signal quantity and data interaction when the PLC acts as a ModBus master station in a data interaction method for configuration software runtime based on ModBus according to an embodiment of the present invention; Figure 3 It is a flowchart of setting write mode signal quantity and data interaction when PLC acts as ModBus master station in a data interaction method of configuration software runtime based on ModBus according to an embodiment of the present invention; Figure 4The present invention discloses a ModBus master station reading operation signal quantity setting and a data interaction timing diagram in a ModBus-based configuration software runtime data interaction method according to an embodiment of the present invention; Figure 5 The present invention discloses a ModBus master station write operation signal quantity setting and a data interaction timing diagram in a ModBus-based configuration software runtime data interaction method according to an embodiment of the present invention; Figure 6 It is a flow chart of executing a read request to set a semaphore and data interaction when a PLC acts as a ModBus slave station in a data interaction method for configuration software runtime based on ModBus according to an embodiment of the present invention; Figure 7 The present invention is a timing diagram of a ModBus slave station executing a master station to read a function code and set a signal quantity and data interaction in a data interaction method for a configuration software runtime based on ModBus according to an embodiment of the present invention; Figure 8 The present invention is a flow chart of a ModBus slave station executing a write request to set a semaphore and data interaction in a data interaction method for configuration software runtime based on ModBus according to an embodiment of the present invention; Fig. 9 The invention discloses a timing diagram of a ModBus slave station executing a master station to write a function code to set a signal quantity and data interaction in a data interaction method of a configuration software runtime based on ModBus according to an embodiment of the present invention. DETAILED DESCRIPTION

[0024] In order to enable those skilled in the art to better understand the technical solutions in this application, the technical solutions in the embodiments of this application will be clearly and completely described below in conjunction with the drawings in the embodiments of this application. Obviously, the described embodiments are only part of the embodiments of this application, not all of the embodiments. Based on the embodiments in this application, all other embodiments obtained by ordinary technicians in this field without creative work should fall within the scope of protection of this application.

[0025] According to an embodiment of the present invention, Figure 1 As shown, a data interaction method for configuration software runtime based on ModBus is provided.

[0026] In order to improve the integrity and accuracy of data interaction between bus external devices and PLC task cycles, the present invention proposes a data interaction method for PLC configuration software runtime based on ModBus protocol, that is, after configuring the task cycle through PLC configuration software and completing downloading, the configuration software will configure the bus protocol stack management storage area during runtime. Then, before and after each PLC cycle is executed, two semaphores are used, namely, a receiving semaphore and a sending semaphore. The receiving semaphore is used to indicate the end of inputting the protocol stack storage area data into the I area of ​​the PLC before the cycle starts, and the sending semaphore indicates the end of outputting the Q area of ​​the PLC to the protocol stack storage area after the cycle ends. According to these two semaphores, ModBus protocol stack request messages and reply messages are sent.

[0027] In the Modbus protocol, the two communicating parties are called the master station and the slave station. The master station will send a query or write command to the slave station, and the slave station will passively receive the command and then feedback the corresponding data result or execute the write command according to the command instruction. Therefore, the data interaction method based on ModBus configuration software runtime provided by the present invention is divided into two cases, one is that the configuration software configures the PLC to be a ModBus master station, and the other is that the configuration software configures the PLC to be a ModBus slave station.

[0028] When the PLC is the master station, the configuration software configures the function code executed by the master station and the corresponding channel address binding in the integrated development environment. For write-related function codes, the channel address binding should be the Q area (output storage area) of the PLC. After the protocol stack obtains the data to be written from the output storage area, it builds the protocol stack message to communicate with the external bus device. For read-related function codes, the channel address is bound to the I area (input storage area) of the PLC. When the protocol stack sends a read request to the external bus device and obtains the read data, it synchronizes the data to the input storage area.

[0029] When the PLC is a slave, the configuration software configures the corresponding ModBus data storage area, including coils, discrete coils, holding registers, and discrete registers. For coils and holding registers, the configuration software defaults to read-only attributes. At this time, the ModBus storage area is bound to the PLC's I area (input storage area). When the user configures the writable attributes of the coil or holding register area in the configuration software, the ModBus protocol stack storage area needs to interact with the special area M area of ​​the fan PLC (this area can be both input and output). For discrete coils and discrete registers, since the protocol specification states that these two areas can only perform read operations for external ModBus master station requests, discrete coils and discrete registers are bound to the PLC's Q area (output storage area).

[0030] The present invention is now further described in conjunction with the accompanying drawings and specific embodiments; According to one embodiment of the present invention, when the configuration software configures the PLC as a ModBus master station, a data interaction method for the configuration software based on ModBus is provided, and the data interaction method for the configuration software based on ModBus comprises the following steps: S1. The configuration software configures the PLC as a ModBus master station, configures the function code for the PLC and establishes a communication connection with the ModBus slave station. The function code includes a read function code and a write function code. As a preferred implementation, the configuration software configures the PLC as a ModBus master station, configures the function code for the PLC and establishes a communication connection with the ModBus slave station, including the following steps: S11, based on the preset configuration mode, using the configuration software to configure the PLC as a ModBus master station; It should be noted that there are two pre-set configuration modes, namely Ethernet communication configuration mode and serial communication configuration mode. The communication protocol stack corresponding to the Ethernet communication configuration mode is the Ethernet communication protocol; the communication protocol stack corresponding to the serial communication configuration mode is the serial communication protocol.

[0031] S12, configuring a function code for the PLC according to a preset function code type; S13, according to the configuration mode of the PLC, select the corresponding protocol stack to establish a connection with the ModBus slave station; S14. Use the running part of the configuration software to start the ModBus master station program.

[0032] It should be noted that the configuration software configures the PLC to act as a ModBus master and read function codes. After the integrated development environment part of the configuration software downloads the configuration, the runtime of the configuration software starts the ModBus protocol stack.

[0033] S2. According to the function code type, the ModBus master sends the function code to the ModBus slave in a pre-set manner to realize data interaction.

[0034] As a preferred implementation, according to the function code type, the ModBus master station sends the function code to the ModBus slave station in a preset manner to implement data interaction, including the following steps: S21. If the function code is a read function code, the ModBus master station sends a read function code message to the ModBus slave station and performs data interaction based on the feedback result of the ModBus slave station; Specifically, reading the function code message includes reading coils, reading discrete coils, reading holding registers, and reading input registers.

[0035] As a preferred implementation, if the function code is a read function code, the ModBus master station sends a read function code message to the ModBus slave station and performs data interaction based on the feedback result of the ModBus slave station, including the following steps: S211, after the ModBus master station program is started, the ModBus master station sends a message with a read function code to the ModBus slave station; S212, the ModBus slave replies with a message containing the required data to the ModBus master and sets the receiving signal quantity through the ModBus master; S213, the protocol stack storage area obtains the received signal amount in real time, and updates the protocol stack storage area data to the PLC input storage area after obtaining the received signal amount.

[0036] Specifically, if Figure 2-3 As shown, in order to better understand the above technical solution, if the function code is a read function code, the ModBus master sends a read function code message to the ModBus slave and performs data interaction based on the feedback result of the ModBus slave. The specific implementation process is as follows: (1) After the PLC starts the ModBus master program, it uses the ModBus-Tcp or ModBus-RTU protocol stack to establish a connection with the external slave device according to the different configuration methods. The difference between ModBus-Tcp and ModBus-RTU is the transmission mode. ModBus-Tcp is an Ethernet communication protocol that uses the ModBus protocol extension based on the TCP / IP protocol. Modbus-RTU is a serial communication protocol that uses binary encoding and is widely used in serial communication. Modbus-RTU transmits data through serial communication, usually using serial port standards such as RS-232 and RS-485. After the connection is established, the PLC sends a read function message, which mainly includes reading coils (0x01), reading discrete coils (0x02), reading holding registers (0x03), reading input registers (0x04), etc.

[0037] (2) After receiving the master request, the external ModBus slave device replies to the master. At this time, the PLC receives the reply message and parses it to obtain the slave data to be read and sets the receive signal to true. The process of obtaining slave data is as follows: a. If the protocol stack is ModBus-Tcp, the first to ninth bits in the reply message are the identification bit, message length, starting address and function code respectively. The data replied by the slave station starts from the tenth bit. The PLC master station takes out the read data from the tenth bit to the end of the message and sets the receive signal to true.

[0038] b. If the protocol stack is ModBus-RTU, the first to fifth bits in the reply message are the slave number, starting address and function code respectively, and the data replied by the slave starts from the sixth bit. The PLC master station takes out the data to be written from the sixth bit to the end of the message and sets the receiving signal to true.

[0039] (3) The runtime of the configuration software will periodically execute the tasks configured by the configuration software after the PLC is started. Before the cycle starts, the runtime will perform a read operation to update the I area of ​​the PLC. After the cycle ends, the runtime will perform a write operation and update the output storage area (Q area) of the PLC. Based on this, when the ModBus master performs a read operation and sets the semaphore, the runtime performs a read operation and receives the semaphore, and then updates the data of the protocol stack to the input storage area (I area). This ensures that the configuration software can correctly obtain the slave data of a specific cycle. The ModBus master read operation semaphore setting and data interaction timing diagram is shown below. Figure 4 shown.

[0040] S22. If the function code is a write function code, the data in the PLC output storage area is updated synchronously, a write function code message is constructed and sent to the ModBus slave station through the ModBus master station to realize data interaction.

[0041] As a preferred implementation, if the function code is a write function code, the PLC output storage area data is updated synchronously, a write function code message is constructed and sent to the ModBus slave station through the ModBus master station to realize data interaction, including the following steps: S221, after the ModBus master station program is started, the configuration software updates the PLC output storage area data according to its task execution cycle; S222, the PLC output storage area synchronizes the updated data to the protocol stack storage area, and sets the sending signal quantity through the protocol stack storage area; S223, the ModBus master station obtains the sending signal quantity in real time, and after obtaining the sending signal quantity, constructs a write function code message and sends it to the ModBus slave station.

[0042] Specifically, in order to better understand the above technical solution, if the function code is a write function code, the PLC output storage area data is updated synchronously, and a write function code message is constructed and sent to the ModBus slave through the ModBus master station to realize the specific implementation process of data interaction as follows: (1) The configuration software configures the PLC to act as a ModBus master and write function codes. After the configuration software is downloaded, the configuration software runtime starts the ModBus protocol stack.

[0043] (2) The runtime of the configuration software will periodically execute the tasks configured by the configuration software after the PLC is started. After each task cycle, the runtime will update the data in the output storage area (Q area). After the Q area is updated, the data will be synchronized to the protocol stack storage area. Then the runtime will set the send semaphore to true.

[0044] (3) After the PLC starts the ModBus master program, it uses TCP or RTU mode according to its configuration method to establish a connection with the external device as the ModBus slave. When the master receives the send signal, it updates the data from the input storage area (Q area) to the protocol stack storage area, and then constructs a write function message, which mainly includes writing coils (0x05, 0x0f) and registers (0x06, 0x10). Finally, the message is sent to the slave device to write data to the external device. The timing diagram of the PLC master executing the write function is shown in the figure below. Figure 5 shown.

[0045] According to another embodiment of the present invention, when the configuration software configures the PLC as a ModBus slave, a data interaction method for the configuration software runtime based on ModBus is provided, characterized in that the data interaction method for the configuration software runtime based on ModBus comprises the following steps: S1', the configuration software configures the PLC as a ModBus slave and sets the PLC protocol stack storage area size; It should be noted that the configuration software configures the PLC to act as a ModBus slave and the corresponding protocol stack storage area size. When the configuration software is downloaded and the protocol stack is started, it is always ready to accept requests from external devices (ModBus master).

[0046] S2', the ModBus slave station receives the function code request sent by the ModBus master station, and constructs a reply message in a preset manner according to the function code request type and sends it to the ModBus master station.

[0047] As a preferred implementation, the ModBus slave station receives the function code request sent by the ModBus master station, and constructs a reply message in a preset manner according to the function code request type and sends it to the ModBus master station, including: S21', when the ModBus slave station receives the function code request, it parses the function code request to obtain the function code request type, which includes a read function code request and a write function code request; S22', if the function code request type is a read function code request, the ModBus slave station constructs a reply message and sends it to the ModBus master station after receiving the signal that the storage area data update is completed; It should be noted that after each task cycle is executed, the runtime will update the data to the output storage area (Q area) and the special storage area (M area). Then the Q area updates the data to the coil and holding register storage area, and the M area updates the data to the discrete coil and output register storage area. After the update is completed, the send signal is set to indicate the end of synchronization. After receiving the request message sent by the external device, the PLC waits for the signal of the end of the update. When the signal is received, it constructs a reply message.

[0048] As a preferred implementation, if the function code request type is a read function code request, the ModBus slave station constructs a reply message and sends it to the ModBus master station after receiving the signal that the storage area data update is completed, including the following steps: S221', when the ModBus slave station receives the read function code request from the ModBus master station, the ModBus slave station is set to a waiting-to-receive state; S222', based on the task execution cycle of the configuration software, update the data to the PLC storage area, set the sending signal quantity and send it to the ModBus slave station; As a preferred implementation, updating data to the PLC storage area based on the task execution cycle of the configuration software, setting the sending signal quantity and sending it to the ModBus slave station includes the following steps: S2221', the configuration software updates the PLC output storage area and PLC special storage area after each task cycle execution; S2222', update the data in the PLC special storage area to the PLC protocol stack storage area of ​​the coil and holding register; S2223', update the PLC output storage area data to the PLC protocol stack storage area of ​​the discrete coil and input register; S2224', after the PLC memory data is updated, set the send signal quantity and send it to the ModBus slave station.

[0049] S223', after receiving the sent signal, the ModBus slave station constructs a reply message to the master station according to the obtained function code.

[0050] Specifically, in order to better understand the above technical solution, if the function code request type is a read function code request, the ModBus slave station constructs a reply message and sends it to the ModBus master station after receiving the signal that the storage area data update is completed. The specific implementation process is as follows: (1) When a ModBus slave station, its port or serial port is always in the listening state to receive the request message sent by the external ModBus master station.

[0051] (2) After receiving the request message, parse the message to obtain the function code. If the protocol stack is ModBus-Tcp, the function code is the 8th bit of the message. If the protocol stack is Modbus-RTU, the function code is the 2nd bit of the message. Then determine whether the function code is a read operation function code (0x01, 0x02, 0x03, 0x04). If so, wait for the signal to be sent.

[0052] (3) At the end of each runtime cycle, the output storage area (Q area) and the special storage area (M area) are updated, and the data in the M area is updated to the protocol stack storage area of ​​the coil and the holding register, and the data in the Q area is updated to the protocol stack storage area of ​​the discrete coil and the input register. Then the send semaphore is set to true.

[0053] (4) When the ModBus slave receives the sent signal, it takes data from different protocol stack storage areas according to the function code to construct a reply message to the master. The PLC slave executes the read function code data interaction timing diagram as shown in the figure. Figure 7 shown.

[0054] S23', if the function code request type is a write function code request, the storage area data is updated through the ModBus slave station and a reply message is constructed and sent to the ModBus master station after the update is completed.

[0055] As a preferred embodiment, Figure 8-9 As shown, if the function code request type is a write function code request, the storage area data is updated through the ModBus slave station and a reply message is constructed and sent to the ModBus master station after the update is completed; S231', when the ModBus slave station receives the write function code request from the ModBus master station, it parses the write function code request and updates the parsed data to the protocol stack storage area; S232', synchronize the data to the PLC storage area through the protocol stack storage area, set the reply signal and send it to the ModBus slave station; As a preferred implementation, synchronizing the data to the PLC storage area through the protocol stack storage area, setting the reply signal and sending it to the ModBus slave station includes the following steps: S2321', parsing the write function code request to obtain the write function code request type, which includes write coil, write save register, write discrete coil and write input register; S2322', if the write function code request type is write coil or write save register, the data is synchronized to the PLC special storage area; S2323', if the write function code request type is to write discrete coils or write input registers, the data is synchronized to the PLC input storage area.

[0056] It should be noted that when the PLC acts as a ModBus slave, it receives and parses the request message sent by the external master. After parsing the message, it obtains the data written by the master. If the message contains a write function code (0x05, 0x05, 0x0f, 0x10), etc., the data is stored in the storage area of ​​the protocol stack. The slave waits for the semaphore indicating that the input storage area or special storage area has been updated. When the semaphore is true, the slave replies with a message.

[0057] The protocol stack storage area will update data to different PLC storage areas according to the different write function codes requested by the master station. If the function code is to write coils or write storage registers, the data is updated to the special storage area (M area). If the data is to write discrete coils or write input registers, the data is updated to the input storage area (I area).

[0058] S233', when the ModBus slave station receives the reply signal, it constructs a reply message to the ModBus master station.

[0059] It should be noted that the reply message structure is as follows: a. If the protocol stack is ModBus-TCP, the 1st to 4th bits in the reply message are filled with the protocol stack identification bit, the 5th and 6th bits are filled with the overall length of the message data part, the 7th and 8th bits are filled with the slave ID and function code, and the 9th and 10th bits are filled with the starting address and quantity.

[0060] b. If the protocol stack is ModBus-RTU, the 1st to 4th bits in the reply message are filled with the station number, function code and start address, the 5th and 6th bits are filled with the number of written coils and registers, and the 7th and 8th bits are filled with the check code.

[0061] In summary, with the aid of the above technical scheme of the present invention, the present invention is based on the mechanism of semaphore, and uses different semaphores at different times to respectively indicate the end of data updating from the PLC data storage area to the protocol stack storage area or the protocol stack storage area to the PLC data storage area. The protocol stack can complete the construction and sending of the request message or the reply message according to the reception of the semaphore, thereby ensuring the integrity and consistency of the data interaction between the PLC, the configuration software, and the external device; when the present invention configures the PLC as a ModBus master station through the configuration software, according to the read and write function code, the receive or send semaphore is used to indicate the start or end of the synchronization operation between the protocol stack and the PLC data storage area, thereby ensuring that the PLC reads the external device. The data of the equipment can be completely and correctly presented in the configuration software, and the data configured by the configuration software user can also be ensured to be accurately written into the external device; when the present invention configures the PLC as a ModBus slave station through the configuration software, after receiving the read and write request of the external master station, the data storage area of ​​the protocol stack is updated and different semaphores are constructed according to different function codes, and the input storage area or output storage area of ​​the PLC is interacted with according to the attribute of the semaphore. When the data storage area of ​​the PLC is updated, the runtime of the PLC sends a reply semaphore to the protocol stack to notify the protocol stack to reply to the external master station. This operation can ensure that the ModBus slave station can interact with the configuration software completely and accurately every time it receives an external request.

[0062] It should be understood by those skilled in the art that the embodiments of the present invention may be provided as methods, systems or computer program products. Therefore, the present invention may take the form of a complete hardware embodiment, a complete software embodiment or an embodiment combining software and hardware aspects. Moreover, the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, optical storage, etc.) containing computer-usable program code.

[0063] The specific embodiments described above further illustrate the objectives, technical solutions and beneficial effects of the present invention in detail. It should be understood that the above description is only a specific embodiment of the present invention and is not intended to limit the scope of protection of the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present invention should be included in the scope of protection of the present invention.

Claims

1. A data interaction method for configuration software runtime based on ModBus, characterized in that: The data interaction method of the configuration software runtime based on ModBus comprises the following steps: S1. The configuration software configures the PLC as a ModBus master station, configures a function code for the PLC and establishes a communication connection with the ModBus slave station. The function code includes a read function code and a write function code. S2. According to the function code type, the ModBus master sends the function code to the ModBus slave in a pre-set manner to realize data interaction.

2. A data interaction method for configuration software runtime based on ModBus according to claim 1, characterized in that: The configuration software configures the PLC as a ModBus master station, configures the function code for the PLC and establishes a communication connection with the ModBus slave station, including the following steps: S11, based on the preset configuration mode, using the configuration software to configure the PLC as a ModBus master station; S12, configuring a function code for the PLC according to a preset function code type; S13, according to the configuration mode of the PLC, select the corresponding protocol stack to establish a connection with the ModBus slave station; S14. Use the running part of the configuration software to start the ModBus master station program.

3. A data interaction method for configuration software runtime based on ModBus according to claim 1, characterized in that: According to the function code type, the ModBus master station sends the function code to the ModBus slave station in a preset manner to realize data interaction, including the following steps: S21. If the function code is a read function code, the ModBus master station sends a read function code message to the ModBus slave station and performs data interaction based on the feedback result of the ModBus slave station; S22. If the function code is a write function code, the data in the PLC output storage area is updated synchronously, a write function code message is constructed and sent to the ModBus slave station through the ModBus master station to realize data interaction.

4. A data interaction method for configuration software runtime based on ModBus according to claim 3, characterized in that: The reading function code message includes reading coils, reading discrete coils, reading holding registers and reading input registers.

5. The data interaction method for configuration software runtime based on ModBus according to claim 3, characterized in that: If the function code is a read function code, the ModBus master station sends a read function code message to the ModBus slave station and performs data interaction based on the feedback result of the ModBus slave station, including the following steps: S211, after the ModBus master station program is started, the ModBus master station sends a message with a read function code to the ModBus slave station; S212, the ModBus slave replies with a message containing the required data to the ModBus master and sets the receiving signal quantity through the ModBus master; S213, the protocol stack storage area obtains the received signal amount in real time, and updates the protocol stack storage area data to the PLC input storage area after obtaining the received signal amount.

6. A data interaction method for configuration software runtime based on ModBus according to claim 5, characterized in that: If the function code is a write function code, the PLC output storage area data is updated synchronously, a write function code message is constructed and sent to the ModBus slave station through the ModBus master station to realize data interaction, including the following steps: S221, after the ModBus master station program is started, the configuration software updates the PLC output storage area data according to its task execution cycle; S222, the PLC output storage area synchronizes the updated data to the protocol stack storage area, and sets the sending signal quantity through the protocol stack storage area; S223, the ModBus master station obtains the sending signal quantity in real time, and after obtaining the sending signal quantity, constructs a write function code message and sends it to the ModBus slave station.

7. A data interaction method for configuration software runtime based on ModBus, characterized in that: The data interaction method of the configuration software runtime based on ModBus comprises the following steps: S1', the configuration software configures the PLC as a ModBus slave and sets the PLC protocol stack storage area size; S2', the ModBus slave station receives the function code request sent by the ModBus master station, and constructs a reply message in a preset manner according to the function code request type and sends it to the ModBus master station.

8. A data interaction method for configuration software runtime based on ModBus according to claim 7, characterized in that: The ModBus slave station receives the function code request sent by the ModBus master station, and constructs a reply message in a preset manner according to the function code request type and sends it to the ModBus master station, including: S21', when the ModBus slave station receives the function code request, it parses the function code request to obtain the function code request type, where the function code request type includes a read function code request and a write function code request; S22', if the function code request type is a read function code request, the ModBus slave station constructs a reply message and sends it to the ModBus master station after receiving the signal that the storage area data update is completed; S23', if the function code request type is a write function code request, the storage area data is updated through the ModBus slave station and a reply message is constructed and sent to the ModBus master station after the update is completed.

9. A data interaction method for configuration software runtime based on ModBus according to claim 8, characterized in that: If the function code request type is a read function code request, the ModBus slave station constructs a reply message and sends it to the ModBus master station after receiving the signal indicating that the storage area data update is completed, including the following steps: S221', when the ModBus slave station receives the read function code request from the ModBus master station, the ModBus slave station is set to a waiting-to-receive state; S222', based on the task execution cycle of the configuration software, update the data to the PLC storage area, set the sending signal quantity and send it to the ModBus slave station; S223', after receiving the sent signal, the ModBus slave station constructs a reply message to the master station according to the obtained function code.

10. A data interaction method for configuration software runtime based on ModBus according to claim 9, characterized in that: The task execution cycle based on the configuration software updates data to the PLC storage area, sets the sending signal quantity and sends it to the ModBus slave station, including the following steps: S2221', the configuration software updates the PLC output storage area and PLC special storage area after each task cycle execution; S2222', update the data in the PLC special storage area to the PLC protocol stack storage area of ​​the coil and holding register; S2223', update the PLC output storage area data to the PLC protocol stack storage area of ​​the discrete coil and input register; S2224', after the PLC memory data is updated, set the send signal quantity and send it to the ModBus slave station.

11. A data interaction method for configuration software runtime based on ModBus according to claim 8, characterized in that: If the function code request type is a write function code request, the storage area data is updated through the ModBus slave station and a reply message is constructed and sent to the ModBus master station after the update is completed; S231', when the ModBus slave station receives the write function code request from the ModBus master station, it parses the write function code request and updates the parsed data to the protocol stack storage area; S232', synchronize the data to the PLC storage area through the protocol stack storage area, set the reply signal and send it to the ModBus slave station; S233', when the ModBus slave station receives the reply signal, it constructs a reply message to the ModBus master station.

12. A data interaction method for configuration software runtime based on ModBus according to claim 11, characterized in that: The method of synchronizing the data to the PLC storage area through the protocol stack storage area, setting the reply signal amount and sending it to the ModBus slave station includes the following steps: S2321', parsing the write function code request to obtain the write function code request type, wherein the write function code request type includes write coil, write save register, write discrete coil and write input register; S2322', if the write function code request type is write coil or write save register, the data is synchronized to the PLC special storage area; S2323', if the write function code request type is to write discrete coils or write input registers, the data is synchronized to the PLC input storage area.

Citation Information

Cited By

  • Gateway machine equipment Modbus protocol data access processing method

    CN119544815A