Modbus fast response method, slave and master
By introducing a delayed response mechanism for custom function code requests in Modbus communication, the problem of excessive resource consumption in the Modbus standard protocol during rapid response is solved, achieving efficient communication resource management and fast event processing.
Patent Information
- Application Number
- CN202310493173.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-04-28
- Publication Date
- 2025-12-05
- Estimated Expiration
- 2043-04-28
AI Technical Summary
The existing Modbus standard protocol has the problem of consuming too many system resources when a fast response is required.
By receiving a custom function code request from the host, if the corresponding event occurs within a preset first timeout period, the first event code is sent; otherwise, the second event code is sent. This enables the slave device to respond with a delay, avoiding channel congestion and reducing system resource consumption.
It effectively reduces channel congestion and system resource consumption during Modbus communication, ensuring that rapid response does not affect the host's handling of events.
Smart Images

Figure CN116389189B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of communication technology, specifically to a Modbus fast response method, a slave device, and a master device. Background Technology
[0002] Currently, the widely adopted Modbus standard protocol is a query-response communication protocol, using a one-question-one-answer format. Each communication begins with the master sending a data request message, and the slave, upon receiving the correct message, can send data back to the master in response to the request; the master can also directly send information to modify the slave's data, enabling bidirectional read and write operations.
[0003] However, this approach makes it difficult to handle logic that requires a fast response. For example, an emergency stop signal requires the system to stop working within a very short time. If a fast response is required, the Modbus host needs to poll the emergency stop flag at an extremely high speed, resulting in the consumption of a large amount of system resources. This is unacceptable for many low-performance embedded communication devices. Summary of the Invention
[0004] To address the shortcomings of existing technologies, the purpose of this application is to provide a Modbus fast response method, slave device, and master device to solve the problem that using the standard Modbus protocol in related technologies requires excessive system resources to achieve fast response.
[0005] The first aspect of this application provides a Modbus fast response method, which includes the following steps:
[0006] If a custom function code request is received from the host, and the event corresponding to the custom function code occurs within a preset first timeout period, a first event code is sent to the host so that the host can read the event using the Modbus protocol; otherwise, a second event code is sent to the host. The first event code is defined as the event that needs to be processed, and the second event code is defined as the event that has not occurred.
[0007] In some embodiments, timing begins from the receipt of the custom function code request. If the timing reaches the first timeout period and the event corresponding to the custom function code has not occurred, a timeout is determined, and a second event code is sent to the host.
[0008] In some embodiments, the host starts timing when it sends a custom function code request. If no response is received after the timing reaches the second timeout period, a timeout is determined and a communication loss fault is reported. The second timeout period is greater than the first timeout period.
[0009] In some embodiments, when the host receives the first event code, it reads and processes the event using the Modbus protocol.
[0010] In some embodiments, when the host receives an event code other than the first event code and the second event code, it reports an event code error fault.
[0011] In some embodiments, when a non-custom function code request is received from the host, a standard Modbus protocol response is executed.
[0012] A second aspect of this application provides a slave device, comprising:
[0013] The acquisition module is used to receive custom function code requests sent by the host;
[0014] The first sending module is used to send a first event code, defined as requiring the processing of the event, to the host when the event corresponding to the custom function code occurs within a preset first timeout period, so that the host can read the event using the Modbus protocol; otherwise, it sends a second event code to the host; the first event code is defined as requiring the processing of the event, and the second event code is defined as no event has occurred.
[0015] In some embodiments, the slave device further includes:
[0016] The first timing module is used to start timing upon receiving the aforementioned custom function code request;
[0017] When the timeout period reaches the first timeout period and the event corresponding to the custom function code has not occurred, the first sending module sends the second event code to the host.
[0018] A third aspect of this application provides a host computer comprising:
[0019] The second sending module is used to send a custom function code request to the slave device described in claim 7;
[0020] The processing module is used to read and process the event using the Modbus protocol when the first event code is received.
[0021] In some embodiments, the host described above further includes:
[0022] The second timing module is used to start timing automatically by sending a custom function code request.
[0023] If the processing module still does not receive a response when the second timeout period is reached, the aforementioned processing module is also used to determine the timeout and report a communication loss fault; the aforementioned second timeout period is greater than the first timeout period.
[0024] The beneficial effects of the technical solution provided in this application include:
[0025] The Modbus fast response method, slave device, and master device of this application, when the slave device receives a custom function code request sent by the master device, if the event corresponding to the custom function code occurs within a preset first timeout period, it sends a first event code to the master device so that the master device can read the event using the Modbus protocol; if the event corresponding to the custom function code does not occur within the preset first timeout period, it sends a second event code to the master device, indicating that nothing has occurred. Since the slave device does not reply immediately upon receiving the custom function code request, but only replies to the master device when the corresponding event occurs or when the first timeout period has elapsed, the delayed reply from the slave device, compared to the question-and-answer communication process where the slave device replies to the master device in response to a data request, avoids generating a large amount of information in the channel and causing channel congestion. At the same time, the number of Modbus messages that the master device needs to process is greatly reduced, effectively reducing the system resources occupied by communication, without affecting the master device's rapid processing of events. Attached Figure Description
[0026] To more clearly illustrate the technical solutions in the embodiments of this application, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0027] Figure 1 This is a flowchart of the Modbus fast response slave processing in the embodiments of this application;
[0028] Figure 2 This is a flowchart of the Modbus fast response host processing in an embodiment of this application. Detailed Implementation
[0029] To make the objectives, technical solutions, and advantages of this application clearer, the present invention will be further described in detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative of the invention and are not intended to limit the invention. Furthermore, the technical features involved in the various embodiments of the present invention described below can be combined with each other as long as they do not conflict with each other.
[0030] This application provides a Modbus fast response method, which includes the following steps:
[0031] If a custom function code request is received from the host, and the event corresponding to the custom function code occurs within a preset first timeout period, a first event code is sent to the host so that the host can read the event using the Modbus protocol; otherwise, a second event code is sent to the host. The first event code is defined as the event that needs to be processed, and the second event code is defined as the event that has not occurred.
[0032] The Modbus protocol allows users to define custom function codes, ranging from 65 to 72 and 100 to 110. These custom function codes enable custom functions for communication.
[0033] Custom function code requests are applicable to various information that require a fast response and are not frequently triggered. For example, custom function code requests (or events corresponding to custom function codes) include, but are not limited to, fault states such as battery undervoltage, battery overvoltage, and overtemperature.
[0034] In this embodiment of the Modbus fast response method, when the slave device receives a custom function code request from the master device, if the event corresponding to the custom function code occurs within a preset first timeout period, it sends a first event code to the master device so that the master device can read the event using the Modbus protocol; if the event corresponding to the custom function code does not occur within the preset first timeout period, it sends a second event code to the master device, indicating that nothing has occurred. Since the slave device does not reply immediately upon receiving the custom function code request, but only replies when the corresponding event occurs or when the first timeout period has elapsed, by implementing the slave device's delayed reply function, compared to the question-and-answer communication process where the slave device replies to the master in response to a data request, a large amount of information can be generated in the channel, avoiding channel congestion. At the same time, the number of Modbus messages that the master device needs to process is greatly reduced, effectively reducing the system resources occupied by communication without affecting the master device's rapid processing of events.
[0035] In some embodiments, taking a response within 10 milliseconds as an example, the host sends a request every 10 milliseconds, and with the timely reply from the slave, 200 messages are generated per second.
[0036] In this embodiment, taking a first timeout of 500ms as an example, if no event corresponding to the custom function code occurs, only 4 messages need to be generated per second; when an event occurs, due to the immediate response from the slave device, the time for the master to obtain the event is less than 10ms, which will not affect the master's rapid processing of the event.
[0037] Based on the above embodiments, in this embodiment, the slave device starts timing after receiving the custom function code request. If the timing reaches the first timeout period and the event corresponding to the custom function code has not occurred, it is determined that the timeout has occurred and a second event code is sent to the host device to indicate that nothing has happened.
[0038] In this embodiment, when the slave device receives a Modbus request from the master device, it first needs to determine whether it is a custom function code request. When the slave device receives a non-custom function code request, it executes the Modbus protocol standard to reply to the master device.
[0039] Based on the above embodiments, in this embodiment, the host starts timing when it sends a custom function code request. If no reply is received after the timing reaches the second timeout period, a timeout is determined and a communication loss fault is reported. The second timeout period is greater than the first timeout period.
[0040] Furthermore, when the host receives the first event code, it reads and processes the event using the Modbus protocol.
[0041] In this embodiment, when the host receives an event code from the slave, it further includes determining whether the event code is a pre-defined first event code or a second event code. If the host receives an event code other than the first or second event code, it reports an event code error.
[0042] In cases of emergency shutdowns, such as electric shocks, where real-time information is critical, it is necessary to stop the equipment as quickly as possible.
[0043] In this embodiment, taking the use of custom function codes to detect emergency stops as an example, the first event code b is defined as an emergency stop change, the second event code a is defined as no event occurs, and there is a first timeout time T1 and a second timeout time T2, wherein T1 is less than T2.
[0044] like Figure 1 As shown, the Modbus fast response slave processing flow is as follows:
[0045] A1. The slave device determines whether it has received a Modbus request. If so, it proceeds to A2; otherwise, it proceeds to A1.
[0046] A2. Start timing and determine if the Modbus request is a custom function code request. If so, proceed to A4; otherwise, proceed to A3.
[0047] A3. Execute the Modbus protocol standard response and redirect to A1.
[0048] A4. Determine if the event corresponding to the custom function code has occurred, i.e., the emergency stop status has changed. If so, proceed to A5; otherwise, proceed to A6.
[0049] A5. Send the first event code b to the host and then redirect to A1.
[0050] A6. Determine if the timing time has exceeded the first timeout period. If yes, proceed to A7; otherwise, proceed to A4.
[0051] A7. Send the second event code 'a' to the host and redirect to A1.
[0052] like Figure 2 As shown, the Modbus fast response host processing flow is as follows:
[0053] B1. The host sends a custom function code request to the slave and starts timing.
[0054] B2. Determine if a response has been received from the slave device. If yes, proceed to B5; otherwise, proceed to B3.
[0055] B3. Determine if the timing time has exceeded the second timeout period. If so, proceed to B4; otherwise, proceed to B2.
[0056] B4 reports a communication loss fault and redirects to B1.
[0057] B5. Obtain the event code and determine if the event code is 'a'. If it is, proceed to B1; otherwise, proceed to B6.
[0058] B6. Determine if the event code is 'b'. If it is, proceed to B8; otherwise, proceed to B7.
[0059] B7 reports an event code error and redirects to B1.
[0060] B8. Use the Modbus protocol to read the emergency stop status and switch to B1.
[0061] The method in this embodiment can monitor all events that require rapid processing, such as power limitations, fault triggering, and a series of other states.
[0062] This application embodiment also provides a slave device, which includes an acquisition module and a first sending module. The acquisition module is used to receive a custom function code request sent by a host. The first sending module is used to send a first event code, defined as requiring processing of the event, to the host when an event corresponding to the custom function code occurs within a preset first timeout period, so that the host can read the event using the Modbus protocol; otherwise, the first sending module sends a second event code to the host; the first event code is defined as requiring processing of the event, and the second event code is defined as no event occurring.
[0063] Furthermore, the slave device also includes a first timing module. This first timing module is used to start timing upon receiving the custom function code request.
[0064] When the timeout period reaches the first timeout period and the event corresponding to the custom function code has not occurred, the first sending module sends the second event code to the host.
[0065] The slave device in this embodiment is applicable to the Modbus fast response methods described above. With the addition of a delayed reply function, it does not reply immediately when it receives a custom function code request. Instead, it replies to the master only when the corresponding event occurs or when the event has not occurred after the first timeout period. When the event has not occurred, there is no large amount of message interaction, which will not cause channel congestion. Under the premise of ensuring fast response, the communication load is greatly reduced.
[0066] This application also provides a host, which includes a second sending module and a processing module.
[0067] The second sending module is used to send a custom function code request to the slave device; the processing module is used to read and process the event using the Modbus protocol when it receives the first event code.
[0068] Furthermore, the host also includes a second timing module. This second timing module is used to start timing from the moment a custom function code request is sent.
[0069] If the processing module still does not receive a response when the second timeout period is reached, the aforementioned processing module is also used to determine the timeout and report a communication loss fault; the aforementioned second timeout period is greater than the first timeout period.
[0070] In this embodiment, after the host sends a custom function code request, if no corresponding event occurs, it will not immediately receive a reply from the slave. Instead, the host will only receive an event code a from the slave when the timeout occurs. When no reply is received, there is no large amount of message interaction, which will not cause channel congestion and reduce communication load.
[0071] In the description of this application, it should be noted that the terms "upper," "lower," etc., indicating the orientation or positional relationship are based on the orientation or positional relationship shown in the accompanying drawings, and are only for the convenience of describing this application and simplifying the description, and do not indicate or imply that the device or element referred to must have a specific orientation, or be constructed and operated in a specific orientation, and therefore should not be construed as a limitation of this application. Unless otherwise expressly specified and limited, the terms "installed," "connected," and "linked" should be interpreted broadly. For example, they can refer to a fixed connection, a detachable connection, or an integral connection; they can refer to a mechanical connection or an electrical connection; they can refer to a direct connection or an indirect connection through an intermediate medium; they can refer to the internal communication between two elements. For those skilled in the art, the specific meaning of the above terms in this application can be understood according to the specific circumstances.
[0072] It should be noted that in this application, relational terms such as "first" and "second" are used merely to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes the element.
[0073] The above are merely specific embodiments of this application, enabling those skilled in the art to understand or implement this application. Various modifications to these embodiments will be readily apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of this application. Therefore, this application is not to be limited to the embodiments shown herein, but is to be accorded the widest scope consistent with the principles and novel features claimed herein.
Claims
1. A Modbus fast response method, characterized by, It comprises steps of: receiving a custom function code request sent by a host, if an event corresponding to the custom function code occurs within a preset first timeout time, sending a first event code to the host to make the host read the event using a Modbus protocol; otherwise, sending a second event code to the host; the first event code is defined as needing to process the event, and the second event code is defined as nothing happening; starting timing from receiving the custom function code request, if the event corresponding to the custom function code does not occur when the timing time reaches the first timeout time, judging timeout, and sending the second event code to the host.
2. The Modbus fast response method of claim 1, wherein: the host starts timing from sending the custom function code request, if the host does not receive a reply when the timing time reaches a second timeout time, judging timeout, and reporting a communication loss fault; the second timeout time is greater than the first timeout time.
3. The Modbus fast response method of claim 1, wherein: the host reads and processes the event using the Modbus protocol when the host receives the first event code.
4. The Modbus fast response method of claim 1, wherein: reporting an event code error fault when the host receives an event code other than the first event code and the second event code.
5. The Modbus fast response method of claim 1, wherein: performing a standard reply of the Modbus protocol when receiving a non-custom function code request sent by the host.
6. A slave from a machine, characterized by, It comprises: an acquisition module configured to receive a custom function code request sent by a host; a first sending module configured to send a first event code defined as needing to process the event to the host to make the host read the event using a Modbus protocol when an event corresponding to the custom function code occurs within a preset first timeout time; otherwise, sending a second event code to the host; the first event code is defined as needing to process the event, and the second event code is defined as nothing happening; the slave further comprises: a first timing module configured to start timing from receiving the custom function code request; when the event corresponding to the custom function code does not occur when the timing time reaches the first timeout time, the first sending module is configured to send the second event code to the host.
7. A host computer of a kind, It comprises: a second sending module configured to send a custom function code request to the slave of claim 6; a processing module configured to read and process the event using the Modbus protocol when receiving the first event code.
8. The host of claim 7, wherein, The host further comprises: a second timing module configured to start timing from sending the custom function code request; when the processing module does not receive a reply when the timing time reaches a second timeout time, the processing module is further configured to judge timeout and report a communication loss fault; the second timeout time is greater than the first timeout time.
Citation Information
Patent Citations
Communication method and communication system based on Modbus RTU protocol
CN110851392A
Modbus response processing method and host equipment
CN114185824A