Method for realizing MODBUS communication based on serial port, slave station terminal and equipment

By introducing an auxiliary state machine to monitor the serial port status in real time in MODBUS communication, the problem of low communication fault tolerance in the existing technology is solved, higher stability and automatic recovery capabilities are achieved, and the fault tolerance performance of the system is improved.

CN120263583APending Publication Date: 2025-07-04SHENZHEN V&T TECH
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202410016110.0
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-01-04
Publication Date
2025-07-04

AI Technical Summary

Technical Problem

In the prior art, the software architecture that implements MODBUS protocol communication through the chip's own peripheral function lacks a control logic vulnerability detection mechanism, resulting in the inability to automatically recover in abnormal situations, low communication fault tolerance, easy to get stuck, and high requirements for MCU stability and developer capabilities.

Method used

The auxiliary state machine is used to monitor the serial port communication status in real time, and through state switching and exception handling strategies, the communication fault tolerance and stability are improved, including four states: waiting for data to be received, data to be received, data processing and return data, and data to be managed using DMA and serial port interrupts.

Benefits of technology

It improves the fault tolerance and stability of MODBUS communication, reduces the failure rate in abnormal situations, and realizes automatic recovery and efficient data transmission.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120263583A_ABST
    Figure CN120263583A_ABST
Patent Text Reader

Abstract

The invention discloses a method for realizing MODBUS communication based on a serial port, a slave station terminal and equipment, and the method comprises the steps: firstly initializing the serial port, starting to monitor a communication state after an auxiliary state machine is switched to a state of waiting for receiving data, and waiting for receiving frame data when a first state condition is satisfied; if the first character data is received, the auxiliary state machine is switched to a data receiving state, and receives frame data when monitoring that the communication state meets a second state condition; if the serial port idle interruption is triggered when the frame data is received, the auxiliary state machine is switched to a data processing state, and the received data is processed according to the MODBUS protocol when it is monitored that the communication state meets a third state condition; and finally, when the processing result data needs to be returned, the auxiliary state machine is switched to a data returning state, and the processing result data is sent to the master station terminal under the condition that the communication state meets a fourth state condition. The communication state is monitored in real time through the auxiliary state machine, so that the communication error-tolerant rate and stability are improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of serial communication, and particularly to a method for implementing MODBUS communication based on a serial port, a slave terminal, a computer device, and a storage medium. Background Art

[0002] MODBUS communication is widely used in the industrial control industry. For example, MODBUS communication can be used between inverters and upper-level computers such as touch screens, computers, and industrial control computers. Through MODBUS communication, the upper-level computer can query the status of the inverter in real time or control the inverter to perform corresponding actions.

[0003] The traditional software architecture for implementing MODBUS protocol communication using a serial port only uses the built-in peripheral functions of the chip. However, implementing MODBUS communication only using the built-in peripheral functions of the chip itself lacks a control logic vulnerability detection mechanism, and abnormal situations cannot be automatically restored. It requires manual restart or power-off restart to recover. Only relying on the rigor of the program logic and the usage environment to ensure no errors, an alarm will be triggered and the machine will stop when the communication times out. Moreover, it has high requirements for the stability of the MCU and the developer's ability. Once an abnormality occurs in a certain link, the next step cannot be carried out, and there may be a "stuck" phenomenon, that is, the communication is interrupted. In the face of complex and changing application environments, situations such as data frame loss, disconnection, and interference are inevitable. Therefore, the prior art is prone to error alarm and machine stop in these abnormal situations, with a low fault tolerance rate and a high failure rate. Summary of the Invention

[0004] The present invention aims to solve the deficiencies existing in the prior art. Embodiments of the present invention provide a method for implementing MODBUS communication based on a serial port, a slave terminal, a computer device, and a storage medium, which can improve the communication fault tolerance rate and stability by using an auxiliary state machine to monitor the serial communication state in real time.

[0005] In a first aspect, an embodiment of the present invention provides a method for implementing MODBUS communication based on a serial port, which is applied to a slave terminal. The slave terminal is communicatively connected to a master terminal through a serial port, and an auxiliary state machine is provided in the slave terminal. The states of the auxiliary state machine include a preset waiting for receiving data state, a receiving data state, a data processing state, and a returning data state. The method includes:

[0006] Initializing the serial port, switching the state of the auxiliary state machine to the waiting for receiving data state, and waiting for receiving frame data sent by the master terminal when the auxiliary state machine monitors that its current communication state meets a preset first state condition;

[0007] If the first character data in the frame data is received, the state of the auxiliary state machine is switched to the received data state, and when the auxiliary state machine monitors that its current communication state meets the preset second state condition, the frame data is received through DMA;

[0008] If a serial port idle interrupt is triggered during the reception of the frame data, the state of the auxiliary state machine is switched to the data processing state, and when the auxiliary state machine monitors that its current communication state meets the preset third state condition, the processed result data obtained by the auxiliary state machine processing the received frame data according to the MODBUS protocol is acquired;

[0009] If it is determined that the processed result data is a non-empty set, the state of the auxiliary state machine is switched to the return data state, and when the auxiliary state machine monitors that its current communication state meets the preset fourth state condition, the processed result data is sent to the master station terminal through the DMA and the serial port;

[0010] If it is determined that the sending of the processed result data has ended, return to execute the step of initializing the serial port, switch the state of the auxiliary state machine to the waiting for receiving data state, and when the auxiliary state machine monitors that its current communication state meets the preset first state condition, wait for receiving the frame data sent by the master station terminal.

[0011] In a second aspect, an embodiment of the present invention provides a slave station terminal. The slave station terminal is communicatively connected to a master station terminal through a serial port, and the slave station terminal is provided with an auxiliary state machine. The states of the auxiliary state machine include a preset waiting for receiving data state, a received data state, a data processing state, and a return data state, and it includes:

[0012] A waiting for receiving data unit, configured to initialize the serial port, switch the state of the auxiliary state machine to the waiting for receiving data state, and when the auxiliary state machine monitors that its current communication state meets the preset first state condition, wait for receiving the frame data sent by the master station terminal;

[0013] A received data unit, configured to if the first character data in the frame data is received, switch the state of the auxiliary state machine to the received data state, and when the auxiliary state machine monitors that its current communication state meets the preset second state condition, receive the frame data through DMA;

[0014] A data processing unit, configured to, if a serial port idle interrupt is triggered when receiving the frame data, switch the state of the auxiliary state machine to the data processing state, and obtain, when the auxiliary state machine monitors that its current communication state meets a preset third state condition, the processed result data obtained by the auxiliary state machine processing the received frame data according to the MODBUS protocol;

[0015] A return data unit, configured to, if it is determined that the processed result data is a non-empty set, switch the state of the auxiliary state machine to the return data state, and send the processed result data to the master station terminal through the DMA and the serial port when the auxiliary state machine monitors that its current communication state meets a preset fourth state condition;

[0016] A sending end unit, configured to, if it is determined that the sending of the processed result data is completed, return to execute the initialization of the serial port, switch the state of the auxiliary state machine to the waiting for receiving data state, and wait for the step of receiving the frame data sent by the master station terminal when the auxiliary state machine monitors that its current communication state meets a preset first state condition.

[0017] In a third aspect, an embodiment of the present invention further provides a computer device, which includes a memory, a processor, and a computer program stored on the memory and executable on the processor. When the processor executes the computer program, the method for implementing MODBUS communication based on a serial port in the first aspect is realized.

[0018] In a fourth aspect, an embodiment of the present invention further provides a computer-readable storage medium, where the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the processor is caused to execute the method for implementing MODBUS communication based on a serial port in the first aspect.

[0019] An embodiment of the present invention provides a method, a terminal, a device, and a medium for implementing MODBUS communication based on a serial port. The method first initializes the serial port. After the auxiliary state machine switches to the waiting for receiving data state, it starts to monitor the communication state and waits for receiving frame data when the first state condition is met; if the first character data is received, the auxiliary state machine switches to the receiving data state and receives the frame data when the communication state is monitored to meet the second state condition; if a serial port idle interrupt is triggered when receiving the frame data, the auxiliary state machine switches to the data processing state and processes the received data according to the MODBUS protocol when the communication state is monitored to meet the third state condition; finally, when the processed result data needs to be returned, the auxiliary state machine switches to the return data state and ensures that the processed result data is sent to the master station terminal when the communication state meets the fourth state condition. The present invention improves the communication fault tolerance and stability by the auxiliary state machine monitoring the communication state in real time. Description of the Drawings

[0020] To more clearly illustrate the technical solutions of the embodiments of the present invention, the following will briefly introduce the drawings required for the description of the embodiments. Obviously, the drawings in the following description are some embodiments of the present invention. For those of ordinary skill in the art, without creative efforts, other drawings can also be obtained based on these drawings.

[0021] Figure 1 It is a schematic flowchart of a method for implementing MODBUS communication based on a serial port provided by an embodiment of the present invention;

[0022] Figure 2 It is a schematic block diagram of a slave terminal provided by an embodiment of the present invention;

[0023] Figure 3 It is a schematic block diagram of a computer device provided by an embodiment of the present invention. Specific embodiments

[0024] The following will clearly and completely describe the technical solutions in the embodiments of the present invention in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are some, but not all, of the embodiments of the present invention. Based on the embodiments of the present invention, all other embodiments obtained by those of ordinary skill in the art without creative efforts fall within the protection scope of the present invention.

[0025] It should be understood that when used in this specification and the appended claims, the terms "include" and "comprise" indicate the presence of the described features, wholes, steps, operations, elements, and / or components, but do not exclude the presence or addition of one or more other features, wholes, steps, operations, elements, components, and / or their combinations.

[0026] It should also be understood that the terms used in this specification of the present invention are only for the purpose of describing specific embodiments and are not intended to limit the present invention. As used in this specification of the present invention and the appended claims, unless the context clearly indicates otherwise, the singular forms "a", "an", and "the" are intended to include the plural forms.

[0027] It should be further understood that the term "and / or" used in this specification of the present invention and the appended claims refers to any combination and all possible combinations of one or more of the associated listed items, and includes these combinations.

[0028] Please refer to Figure 1 , Figure 1The flowchart shows a method for implementing MODBUS communication based on a serial port according to an embodiment of the present invention. The method for implementing MODBUS communication based on a serial port provided by the embodiment of the present invention is applied to a slave terminal. The slave terminal is communicatively connected to a master terminal through a serial port, and an auxiliary state machine is provided in the slave terminal. The states of the auxiliary state machine include a preset waiting-for-data-reception state, a data-reception state, a data-processing state, and a data-return state. Among them, the auxiliary state machine is short for a finite state automaton, which is a mathematical model abstracted from the operating rules of real things. For example, taking an inverter as the slave terminal, and a host computer such as a touch screen, a computer, or an industrial control computer as the master terminal, and the USART1 serial port of the MCU chip in the inverter can be used to connect to the host computer. An auxiliary state machine is provided in the inverter. The method for implementing MODBUS communication based on a serial port provided by the embodiment of the present invention is applied to the inverter to achieve MODBUS communication between the inverter and host computers such as touch screens, computers, and industrial control computers, and improve the communication fault tolerance rate and stability.

[0029] Refer to Figure 1 , the method for implementing MODBUS communication based on a serial port includes steps S11 to S15.

[0030] S11. Initialize the serial port, switch the state of the auxiliary state machine to the waiting-for-data-reception state, and wait to receive the frame data sent by the master terminal when the auxiliary state machine monitors that its current communication state meets a preset first state condition.

[0031] In this embodiment, when the slave terminal is powered on, the serial port is initialized. After the serial port initialization is completed, the state of the auxiliary state machine is switched to the waiting-for-data-reception state, so that the slave terminal can respond in a timely manner when the frame data sent by the master terminal arrives at the serial port, and thus perform corresponding actions to receive the frame data sent by the master terminal in a timely manner. After the state of the auxiliary state machine is switched to the waiting-for-data-reception state, the auxiliary state machine monitors in real time whether the current communication state of the slave terminal meets the first state condition. Among them, the communication state of the slave terminal includes the state of the serial port interrupt corresponding to the state of the auxiliary state machine and / or the state of DMA (Direct Memory Access). To detect whether the slave terminal has performed actions corresponding to the state of the auxiliary state machine, so as to detect whether the communication state of the slave terminal is abnormal. If the auxiliary state machine monitors that the current communication state of the master terminal meets the preset first state condition, it means that the current communication state of the slave terminal is normal, and the slave terminal starts to wait to receive the frame data sent by the master terminal, which can effectively improve the data fault tolerance rate.

[0032] In one embodiment, the switching the state of the auxiliary state machine to the waiting-for-data-reception state includes:

[0033] Switch the state of the auxiliary state machine to the waiting for data reception state, so as to open the serial port reception interrupt through the auxiliary state machine and close the pre-configured DMA reception channel and DMA transmission channel in the DMA.

[0034] The auxiliary state machine monitors that its current communication state meets a preset first state condition, including:

[0035] The auxiliary state machine monitors that the status bit corresponding to the serial port reception interrupt is set to 1 in the waiting for data reception state.

[0036] In this embodiment, the state of the auxiliary state machine is switched to the waiting for data reception state. Then, the auxiliary state machine performs actions corresponding to the waiting for data reception state. The auxiliary state machine opens the serial port reception interrupt and closes the pre-configured DMA reception channel and DMA transmission channel in the DMA, so that only the serial port reception interrupt is opened for the serial port. Among them, the MCU chip of the slave terminal can adopt STM32F407VGT6. There are 2 DMAs (i.e., DMA1 and DMA2) inside this chip, and each DMA has 8 data streams (i.e., data streams 0 to 7). The DMA reception channel can be set to DMA2_Stream2 (i.e., data stream 2 of DMA2), and the DMA transmission channel can be set to DMA2_Stream7 (i.e., data stream 7 of DMA2).

[0037] Moreover, the auxiliary state machine determines whether the serial port reception interrupt is opened by checking whether the current value of the status bit corresponding to the serial port reception interrupt in the control register of the slave terminal is 1, and further determines whether the current state of the slave terminal is abnormal. If the auxiliary state machine checks that the current value of the status bit corresponding to the serial port reception interrupt is 1, that is, it is determined that the status bit corresponding to the serial port reception interrupt is set to 1, it indicates that the auxiliary state machine monitors that the current communication state meets the first state condition and the current state of the slave terminal is normal. In addition, if the auxiliary state machine checks that the current value of the status bit corresponding to the serial port reception interrupt is not 1, that is, the auxiliary state machine monitors that the current communication state of the slave terminal does not meet the first state condition, it indicates that the current state of the slave terminal is abnormal, and corresponding exception handling can be performed through the auxiliary state machine.

[0038] S12. If the first character data in the frame data is received, switch the state of the auxiliary state machine to the data reception state, and receive the frame data through the DMA when the auxiliary state machine monitors that its current communication state meets a preset second state condition.

[0039] In this embodiment, the frame data includes multiple character data. If the slave terminal receives the first character data in the frame data sent by the master terminal, it switches the state of the auxiliary state machine to the receive data state, so that the slave terminal performs corresponding actions for receiving the frame data. At the same time, the auxiliary state machine monitors in real time whether its current communication state meets the second state condition, so as to ensure that the slave terminal receives the frame data through DMA when the communication state is normal. Among them, DMA is used to provide high-speed data transfer functions between peripherals and memories and between memories, and can quickly move data through DMA without any CPU operation, so as to save CPU resources for other operations.

[0040] In one embodiment, the switching the state of the auxiliary state machine to the receive data state includes:

[0041] Switching the state of the auxiliary state machine to the receive data state in the serial port receive interrupt, so as to close the serial port receive interrupt, start the DMA receive channel, and open the serial port idle interrupt through the auxiliary state machine;

[0042] The auxiliary state machine monitors that its current communication state meets the preset second state condition, including:

[0043] The auxiliary state machine monitors in the receive data state that the status bit corresponding to the serial port receive interrupt is set to 0, the enable bit corresponding to the DMA receive channel is set to 1, and the status bit corresponding to the serial port idle interrupt is set to 1;

[0044] The receiving the frame data through DMA includes:

[0045] Receiving the frame data through the DMA receive channel in the DMA;

[0046] If the number of characters of the received data monitored by the auxiliary state machine exceeds the preset number when receiving the frame data through the DMA receive channel, return to execute step S11.

[0047] In this embodiment, if the slave terminal receives the first character data in the frame data, the serial port receive interrupt is triggered, and the state of the auxiliary state machine is switched to the receive data state in the serial port receive interrupt, so as to close the serial port receive interrupt, start the DMA receive channel, and open the serial port idle interrupt through the auxiliary state machine, so that the frame data received by the slave terminal through the serial port is automatically transmitted to the receive buffer data group by the DMA receive channel, and because the serial port idle interrupt is already open at this time, when a character time idle is detected on the serial port bus, the serial port idle interrupt will be triggered, so it is initially considered that the frame data sent by the master terminal may be received completely.

[0048] Moreover, the auxiliary state machine determines whether the UART receive interrupt is closed by checking the current value of the status bit corresponding to the UART receive interrupt in the control register of the slave terminal in real time, determines whether the UART idle interrupt is enabled by checking the current value of the status bit corresponding to the UART idle interrupt in the control register of the slave terminal in real time, and determines whether the DMA receive channel is started by checking the current value of the enable bit corresponding to the DMA receive channel in the slave terminal, so as to further determine whether the current state of the slave terminal is abnormal, thereby ensuring that the slave terminal can receive frame data. When the communication state of the slave terminal is normal, the slave terminal receives frame data through the DMA receive channel in the DMA. At the same time, when the DMA receive channel receives frame data, the auxiliary state machine monitors in real time whether the number of characters of the received data exceeds a preset number, so as to determine whether there is an overflow in the number of characters of the received data. If the number of characters of the received data exceeds the preset number, it means there is an overflow, and the communication state of the slave terminal is abnormal, and it is necessary to return to step S11 to receive frame data again. In addition, if the auxiliary state machine checks that the current value of any one of the status bit corresponding to the UART receive interrupt, the enable bit corresponding to the DMA receive channel, and the status bit corresponding to the UART idle interrupt does not conform to the situation corresponding to the data reception state, that is, the auxiliary state machine monitors that the current communication state of the slave terminal does not meet the second state condition, indicating that the current state of the slave terminal is abnormal, then the corresponding exception handling can be executed through the auxiliary state machine.

[0049] S13. If the UART idle interrupt is triggered when receiving the frame data, switch the state of the auxiliary state machine to the data processing state, and when the auxiliary state machine monitors that its current communication state meets the preset third state condition, obtain the processed result data obtained by the auxiliary state machine processing the received frame data according to the MODBUS protocol.

[0050] In this embodiment, if the UART idle interrupt is triggered during the process of the slave terminal receiving frame data, the state of the auxiliary state machine is switched to the data processing state, so that the slave terminal performs corresponding actions for processing frame data. At the same time, the auxiliary state machine monitors in real time whether its current communication state meets the third state condition to determine whether the communication state of the slave terminal is abnormal. If the current communication state meets the third state condition, it is normal, and the processed result data obtained by the auxiliary state machine processing the received frame data according to the MODBUS protocol is obtained when the communication state of the slave terminal is normal.

[0051] In one embodiment, the switching the state of the auxiliary state machine to the data processing state includes:

[0052] In the serial port idle interrupt, switch the state of the auxiliary state machine to the data processing state to turn off the serial port idle interrupt through the auxiliary state machine;

[0053] Turn off the DMA reception channel through the auxiliary state machine when the frame data reception is completed;

[0054] Process the received frame data according to the MODBUS protocol through the auxiliary state machine to obtain the processed result data;

[0055] When the auxiliary state machine monitors that its current communication state meets the preset third state condition, including:

[0056] The auxiliary state machine monitors that the status bit corresponding to the serial port idle interrupt is set to 0 in the data processing state.

[0057] In this embodiment, the slave terminal triggers a serial port idle interrupt during the process of receiving frame data, and switches the state of the auxiliary state machine to the data processing state in the serial port idle interrupt, so that the auxiliary state machine executes actions corresponding to the data processing state. Specifically, the serial port idle interrupt is turned off through the auxiliary state machine to prevent secondary triggering. At this time, there is at least one character time idle on the serial port bus, but it does not mean that the frame data has been received completely. Because according to the MODBUS protocol, at least more than 3.5 character times of idle are detected on the serial port bus to count as the completion of frame data reception. Therefore, the DMA reception channel cannot be immediately turned off. At this time, the slave terminal needs to wait for a sufficient delay time for the frame data reception to end. After turning off the serial port idle interrupt through the auxiliary state machine, a timer is started for preset reception delay timing. The preset reception delay can be set to 2.5 character times. If the timer timing time reaches the preset reception delay and it is detected that the slave terminal does not receive data anymore, it means that the frame data reception is completed; if the timer timing time has not reached the preset reception delay, but it is detected that the slave terminal receives data again, the timer timing time is cleared and re-timed until the requirement of frame data reception completion is met, so that the auxiliary state detects the completion of frame data reception, and the DMA reception channel is turned off through the auxiliary state machine when the frame data reception is completed to prevent data from coming in during the data processing process, thus affecting the processing result. After the DMA reception channel is turned off, the received frame data is processed according to the MODBUS protocol through the auxiliary state machine, thereby obtaining the processed result data.

[0058] Moreover, the auxiliary state machine determines whether the serial port idle interrupt is closed by checking whether the current value of the status bit corresponding to the serial port idle interrupt in the control register of the slave terminal is 0, and further determines whether the current state of the slave terminal is abnormal. If the auxiliary state machine checks that the current value of the status bit corresponding to the serial port idle interrupt is 0, it indicates that the auxiliary state machine monitors that the current communication state meets the third state condition, and the current state of the slave terminal is normal. Additionally, if the auxiliary state machine checks that the current value of the status bit corresponding to the serial port idle interrupt is not 0, that is, the auxiliary state machine monitors that the current communication state of the slave terminal does not meet the third state condition, indicating that the current state of the slave terminal is abnormal, corresponding exception handling can be performed through the auxiliary state machine.

[0059] In one embodiment, after step S13, it further includes:

[0060] If it is determined that the processed result data is an empty set, return to execute step S11.

[0061] In this embodiment, after the slave terminal obtains the processed result data, it is necessary to determine whether the processed result data is an empty set to determine whether to return data to the master terminal. If the slave terminal determines that the processed result data is an empty set, it indicates that there is no need to return data to the master terminal after processing the received frame data, and then return to execute step S11 to start waiting again for receiving frame data sent by the master terminal.

[0062] S14. If it is determined that the processed result data is a non-empty set, switch the state of the auxiliary state machine to the return data state, and when the auxiliary state machine monitors that its current communication state meets the preset fourth state condition, send the processed result data to the master terminal through the DMA and the serial port.

[0063] In this embodiment, if the slave terminal determines that the processed result data is a non-empty set, it indicates that data needs to be returned to the master terminal after processing the received frame data, and at this time the processed result data is the data to be returned, then switch the state of the auxiliary state machine to the return data state, so that the slave terminal performs corresponding actions to return data to the master terminal. At the same time, the auxiliary state machine monitors in real time whether its current communication state meets the fourth state condition to ensure that the slave terminal sends the processed result data to the master terminal through the DMA and the serial port when the communication state is normal.

[0064] In one embodiment, before switching the state of the auxiliary state machine to the return data state, it further includes:

[0065] Start a timer for timing through the auxiliary state machine and wait for the timing time of the timer to reach a preset delay.

[0066] In this embodiment, in order to ensure that the master terminal is ready to receive data, after the slave terminal determines that the processed result data needs to be returned to the master terminal, and before the slave terminal switches the state of the auxiliary state machine to the return data state, the slave terminal can start a timer through the auxiliary state machine for timing. When the timing time of the timer reaches the preset delay, the slave terminal will switch the state of the auxiliary state machine to the return data state, and then start sending the processed result data to the master terminal. Among them, the preset delay can be set according to the user's usage requirements, and no specific limitation is made here.

[0067] In one embodiment, the switching the state of the auxiliary state machine to the return data state includes:

[0068] Switching the state of the auxiliary state machine to the return data state to start the DMA send channel through the auxiliary state machine, open the DMA transfer complete interrupt, enable the transmit enable bit of the serial port, and start the serial port transmit complete interrupt in the DMA transfer complete interrupt;

[0069] The auxiliary state machine monitors that its current communication state meets the preset fourth state condition, including:

[0070] The auxiliary state machine monitors that the enable bit corresponding to the DMA send channel is set to 1 in the return data state, and monitors that the transmit enable bit is set to 1.

[0071] In this embodiment, when the slave terminal determines that it needs to return the processed result data to the master terminal, it will switch the state of the auxiliary state machine to the return data state to start the DMA send channel, open the DMA transfer complete interrupt, and enable the transmit enable bit of the serial port through the auxiliary state, so as to transfer the processed result data to the transmit register of the serial port through the DMA send channel, and then the serial port sends the processed result data in the transmit register to the master terminal. When the DMA send channel has transferred all the processed result data to the transmit register of the serial port, it will trigger the DMA transfer complete interrupt. Then, the auxiliary state machine starts the serial port transmit complete interrupt in the DMA transfer complete interrupt. When the serial port finishes sending the processed result data and the transmit register is empty, it will trigger the serial port transmit complete interrupt to facilitate determining that the processed result data has been sent.

[0072] Moreover, the auxiliary state machine determines whether the DMA transmission channel is started by checking in real time whether the current value of the enable bit corresponding to the DMA transmission channel in the slave terminal is 1, and determines whether the transmission enable bit of the serial port is enabled by checking in real time whether the current value of the transmission enable bit in the slave terminal is 1, and further determines whether the current state of the slave terminal is abnormal. If the auxiliary state machine checks that the current value of the enable bit corresponding to the DMA transmission channel is 1 and the current value of the transmission enable bit is 1, it indicates that the auxiliary state machine monitors that the current communication state meets the fourth state condition and the current state of the slave terminal is normal. Additionally, if the auxiliary state machine checks that the enable bit corresponding to the DMA transmission channel and / or the transmission enable bit is set to 0, it indicates that the auxiliary state machine monitors that the current communication state does not meet the fourth state condition and the current state of the slave terminal is abnormal, and corresponding exception handling can be performed through the auxiliary state machine.

[0073] S15. If it is determined that the processing result data has been sent, return to execute step S11.

[0074] In this embodiment, if the slave terminal determines that the processing result data has been sent, it returns to execute step S11 to start waiting again for the frame data sent by the master terminal.

[0075] In one embodiment, the method for implementing MODBUS communication based on a serial port provided by the embodiments of the present invention further includes:

[0076] If, when the auxiliary state machine is in the waiting-to-receive data state, it is monitored through the auxiliary state machine that the current communication state does not meet the first state condition, the first processing strategy in the preset exception handling strategy is executed through the auxiliary state machine for exception handling;

[0077] If, when the auxiliary state machine is in the receiving data state, it is monitored through the auxiliary state machine that the current communication state does not meet the second state condition, the second processing strategy in the preset exception handling strategy is executed through the auxiliary state machine for exception handling;

[0078] If, when the auxiliary state machine is in the data processing state, it is monitored through the auxiliary state machine that the current communication state does not meet the third state condition, the third processing strategy in the preset exception handling strategy is executed through the auxiliary state machine for exception handling;

[0079] If, when the auxiliary state machine is in the returning data state, it is monitored through the auxiliary state machine that the current communication state does not meet the fourth state condition, the fourth processing strategy in the preset exception handling strategy is executed through the auxiliary state machine for exception handling.

[0080] In this embodiment, when the auxiliary state machine monitors that the current communication state does not meet the corresponding state condition in any state, it executes a preset exception handling strategy for exception handling. The exception handling strategy is used to control the slave terminal to return to execute step S11 when the auxiliary state machine monitors that the duration of the current communication state not meeting the corresponding state condition is greater than the preset exception duration, so that the slave terminal can be automatically reset when a communication exception occurs, and the states of the serial port and the auxiliary state machine are automatically reset, effectively reducing the failure rate and effectively improving the data fault tolerance rate.

[0081] Specifically, when the auxiliary state machine monitors that the current communication state does not meet the first state condition in the waiting-to-receive data state, it executes the first handling strategy in the exception handling strategy for exception handling. The auxiliary state machine can check whether the current communication state meets the first state condition every preset monitoring period based on the first handling strategy. If the auxiliary state machine continuously checks that the current communication state does not meet the first state condition for a preset number of times, it means that the duration of the auxiliary state machine monitoring that the current communication state does not meet the first state condition exceeds the preset duration, and a communication exception occurs at the slave terminal. At this time, the slave terminal needs to return to execute step S11 to automatically reset the states of the serial port and the auxiliary state machine. The preset duration is the product of the monitoring period and the preset number of times. Setting the preset duration is to prevent the auxiliary state machine from misjudging. Similarly, when the auxiliary state machine is in the receiving data state and monitors that the current communication state does not meet the second state condition, it executes the second handling strategy in the exception handling strategy for exception handling. The auxiliary state machine can check whether the current communication state meets the second state condition every preset monitoring period based on the second handling strategy. If the auxiliary state machine continuously checks that the current communication state does not meet the second state condition for a preset number of times, it means that the duration of the auxiliary state machine monitoring that the current communication state does not meet the second state condition exceeds the preset duration, and a communication exception occurs at the slave terminal. At this time, the slave terminal needs to return to execute step S11 to automatically reset the states of the serial port and the auxiliary state machine. And so on, the auxiliary state machine makes corresponding similar processing in the data processing state and the return data state as described above, so that the auxiliary state machine can make the slave terminal automatically reset the states of the serial port and the auxiliary state machine when it monitors a communication exception at the slave terminal in any state.

[0082] A method for implementing MODBUS communication based on a serial port disclosed by the present invention monitors the communication state in real time through an auxiliary state machine, so that the slave terminal can complete communication with the master terminal through the serial port when the communication state is normal, improving the communication fault tolerance rate and stability.

[0083] The embodiment of the present invention also provides a slave terminal, which is used to execute any embodiment of the foregoing method for implementing MODBUS communication based on a serial port. Specifically, please refer to Figure 2, Figure 2 A schematic block diagram of a slave terminal provided by an embodiment of the present invention. The slave terminal provided by the embodiment of the present invention includes a data waiting-to-be-received unit 11, a data receiving unit 12, a data processing unit 13, a data returning unit 14, and a transmission end unit 15. Among them, the slave terminal is communicatively connected to the master terminal through a serial port, and the slave terminal is provided with an auxiliary state machine, and the states of the auxiliary state machine include a preset data waiting-to-be-received state, a data receiving state, a data processing state, and a data returning state.

[0084] The data waiting-to-be-received unit 11 is configured to initialize the serial port, switch the state of the auxiliary state machine to the data waiting-to-be-received state, and wait to receive the frame data sent by the master terminal when the auxiliary state machine monitors that its current communication state meets a preset first state condition.

[0085] In this embodiment, when the slave terminal is powered on, the serial port is initialized. After the initialization of the serial port is completed, the state of the auxiliary state machine is switched to the data waiting-to-be-received state, so that the slave terminal can respond in a timely manner when the frame data sent by the master terminal arrives at the serial port, and thus perform corresponding actions to receive the frame data sent by the master terminal in a timely manner. After the state of the auxiliary state machine is switched to the data waiting-to-be-received state, the auxiliary state machine monitors in real time whether the current communication state of the slave terminal meets the first state condition. Among them, the communication state of the slave terminal includes the state of the serial port interrupt corresponding to the state of the auxiliary state machine and / or the state of DMA (Direct Memory Access). To detect whether the slave terminal has performed actions corresponding to the state of the auxiliary state machine, so as to detect whether the communication state of the slave terminal is abnormal. If the auxiliary state machine monitors that the current communication state of the master terminal meets the preset first state condition, it means that the current communication state of the slave terminal is normal, and the slave terminal starts to wait to receive the frame data sent by the master terminal, which can effectively improve the data fault tolerance rate.

[0086] In one embodiment, the switching the state of the auxiliary state machine to the data waiting-to-be-received state includes:

[0087] Switch the state of the auxiliary state machine to the data waiting-to-be-received state, so as to open the serial port receive interrupt and close the pre-configured DMA receive channel and DMA transmit channel through the auxiliary state machine;

[0088] The auxiliary state machine monitors that its current communication state meets the preset first state condition, including:

[0089] The auxiliary state machine monitors that the status bit corresponding to the serial port receive interrupt is set to 1 in the data waiting-to-be-received state.

[0090] In this embodiment, the state of the auxiliary state machine switches to the waiting-to-receive data state. Then, the auxiliary state machine performs actions corresponding to the waiting-to-receive data state. The auxiliary state machine enables the serial port receive interrupt and closes the pre-configured DMA receive channel and DMA transmit channel in the DMA, so that only the serial port receive interrupt is enabled for the serial port. Among them, the MCU chip of the slave terminal can adopt STM32F407VGT6. There are 2 DMAs (i.e., DMA1 and DMA2) inside this chip, and each DMA has 8 data streams (i.e., data streams 0 to 7). The DMA receive channel can be set to DMA2_Stream2 (i.e., data stream 2 of DMA2), and the DMA transmit channel can be set to DMA2_Stream7 (i.e., data stream 7 of DMA2).

[0091] Moreover, the auxiliary state machine determines whether the serial port receive interrupt is enabled by checking whether the current value of the status bit corresponding to the serial port receive interrupt in the control register of the slave terminal is 1, and further determines whether the current state of the slave terminal is abnormal. If the auxiliary state machine checks that the current value of the status bit corresponding to the serial port receive interrupt is 1, that is, it is determined that the status bit corresponding to the serial port receive interrupt is set to 1, it indicates that the auxiliary state machine monitors that the current communication state meets the first state condition and the current state of the slave terminal is normal. Additionally, if the auxiliary state machine checks that the current value of the status bit corresponding to the serial port receive interrupt is not 1, that is, the auxiliary state machine monitors that the current communication state of the slave terminal does not meet the first state condition, it indicates that the current state of the slave terminal is abnormal, and corresponding exception handling can be performed through the auxiliary state machine.

[0092] The received data unit 12 is configured to switch the state of the auxiliary state machine to the receive data state if the first character data in the frame data is received, and receive the frame data through the DMA when the auxiliary state machine monitors that its current communication state meets a preset second state condition.

[0093] In this embodiment, the frame data includes multiple character data. If the slave terminal receives the first character data in the frame data sent by the master terminal, it switches the state of the auxiliary state machine to the receive data state, so that the slave terminal performs corresponding actions for receiving the frame data. At the same time, the auxiliary state machine monitors in real time whether its current communication state meets the second state condition to ensure that the slave terminal receives the frame data through the DMA when the communication state is normal. Among them, the DMA is used to provide high-speed data transfer functions between peripherals and memories and between memories, and can quickly move data through the DMA without any CPU operation, thus saving CPU resources for other operations.

[0094] In one embodiment, switching the state of the auxiliary state machine to the received data state includes:

[0095] In the serial port receive interrupt, switching the state of the auxiliary state machine to the received data state to close the serial port receive interrupt, start the DMA receive channel, and open the serial port idle interrupt through the auxiliary state machine;

[0096] The auxiliary state machine monitors that its current communication state meets a preset second state condition, including:

[0097] The auxiliary state machine monitors in the received data state that the status bit corresponding to the serial port receive interrupt is set to 0, the enable bit corresponding to the DMA receive channel is set to 1, and the status bit corresponding to the serial port idle interrupt is set to 1;

[0098] Receiving the frame data through the DMA includes:

[0099] Receiving the frame data through the DMA receive channel in the DMA;

[0100] If the number of characters in the data received through the auxiliary state machine exceeds a preset number when receiving the frame data through the DMA receive channel, return to execute step S11.

[0101] In this embodiment, if the slave terminal receives the first character data in the frame data, a serial port receive interrupt is triggered. Then, in the serial port receive interrupt, the state of the auxiliary state machine is switched to the received data state to close the serial port receive interrupt, start the DMA receive channel, and open the serial port idle interrupt through the auxiliary state machine, so that the frame data received by the slave terminal through the serial port is automatically transmitted to the receive buffer data group by the DMA receive channel. And because the serial port idle interrupt is already open at this time, when a character time idle is detected on the serial port bus, the serial port idle interrupt will be triggered, thus initially considering that the frame data sent by the master terminal may be received completely.

[0102] Moreover, the auxiliary state machine determines whether the UART receive interrupt is closed by checking the current value of the status bit corresponding to the UART receive interrupt in the control register of the slave terminal in real time, determines whether the UART idle interrupt is enabled by checking the current value of the status bit corresponding to the UART idle interrupt in the control register of the slave terminal in real time, and determines whether the DMA receive channel is started by checking the current value of the enable bit corresponding to the DMA receive channel in the slave terminal, so as to determine whether the current state of the slave terminal is abnormal, thereby ensuring that the slave terminal can receive frame data. When the communication state of the slave terminal is normal, the slave terminal receives frame data through the DMA receive channel in the DMA. At the same time, when receiving frame data through the DMA receive channel, the auxiliary state machine monitors in real time whether the number of characters in the received data exceeds a preset number, so as to determine whether there is an overflow in the number of characters in the received data. If the number of characters in the received data exceeds the preset number, it means there is an overflow, and the slave terminal has an abnormal communication state and needs to return to step S11 to receive frame data again. In addition, if the auxiliary state machine checks that the current value of any one of the status bit corresponding to the UART receive interrupt, the enable bit corresponding to the DMA receive channel, and the status bit corresponding to the UART idle interrupt does not conform to the situation corresponding to the data reception state, that is, the auxiliary state machine monitors that the current communication state of the slave terminal does not meet the second state condition, indicating that the current state of the slave terminal is abnormal, then the corresponding exception handling can be executed through the auxiliary state machine.

[0103] The data processing unit 13 is configured to, if a UART idle interrupt is triggered when receiving the frame data, switch the state of the auxiliary state machine to the data processing state, and obtain the processed result data obtained by the auxiliary state machine processing the received frame data according to the MODBUS protocol when the auxiliary state machine monitors that its current communication state meets a preset third state condition.

[0104] In this embodiment, if a UART idle interrupt is triggered during the process of the slave terminal receiving frame data, the state of the auxiliary state machine is switched to the data processing state, so that the slave terminal performs corresponding actions for processing the frame data. At the same time, the auxiliary state machine monitors in real time whether its current communication state meets the third state condition to determine whether the communication state of the slave terminal is abnormal. If the current communication state meets the third state condition, it is normal, and the processed result data obtained by the auxiliary state machine processing the received frame data according to the MODBUS protocol is obtained when the communication state of the slave terminal is normal.

[0105] In one embodiment, the switching the state of the auxiliary state machine to the data processing state includes:

[0106] In the serial port idle interrupt, switch the state of the auxiliary state machine to the data processing state to turn off the serial port idle interrupt through the auxiliary state machine;

[0107] Turn off the DMA reception channel through the auxiliary state machine when the frame data reception is completed;

[0108] Process the received frame data according to the MODBUS protocol through the auxiliary state machine to obtain the processed result data;

[0109] When the auxiliary state machine monitors that its current communication state meets the preset third state condition, including:

[0110] The auxiliary state machine monitors that the status bit corresponding to the serial port idle interrupt is set to 0 in the data processing state.

[0111] In this embodiment, the slave terminal triggers a serial port idle interrupt during the reception of frame data, and switches the state of the auxiliary state machine to the data processing state in the serial port idle interrupt, so that the auxiliary state machine executes actions corresponding to the data processing state. Specifically, the serial port idle interrupt is turned off through the auxiliary state machine to prevent secondary triggering. At this time, there is at least one character time idle on the serial bus, but it does not mean that the frame data reception is completed. Because according to the MODBUS protocol, at least more than 3.5 character times of idle are detected on the serial bus to count as the completion of frame data reception. Therefore, the DMA reception channel cannot be immediately turned off. At this time, the slave terminal needs to wait for a sufficient delay time to wait for the end of frame data reception. After turning off the serial port idle interrupt through the auxiliary state, a timer is started to perform a preset reception delay timing. The preset reception delay can be set to 2.5 character times. If the timer timing reaches the preset reception delay and it is detected that the slave terminal does not receive data again, it means that the frame data reception is completed; if the timer timing has not reached the preset reception delay, but it is detected that the slave terminal receives data again, the timer timing is cleared and re-timed until the requirement for the completion of frame data reception is met, so that the auxiliary state detects the completion of frame data reception, and the DMA reception channel is turned off through the auxiliary state machine when the frame data reception is completed to prevent data from coming in during the data processing process, thereby affecting the processing result. After the DMA reception channel is turned off, the received frame data is processed according to the MODBUS protocol through the auxiliary state machine to obtain the processed result data.

[0112] Moreover, the auxiliary state machine determines whether the UART idle interrupt is closed by checking whether the current value of the status bit corresponding to the UART idle interrupt in the control register of the slave terminal is 0, and further determines whether the current state of the slave terminal is abnormal. If the auxiliary state machine checks that the current value of the status bit corresponding to the UART idle interrupt is 0, it indicates that the auxiliary state machine monitors that the current communication state meets the third state condition, and the current state of the slave terminal is normal. Additionally, if the auxiliary state machine checks that the current value of the status bit corresponding to the UART idle interrupt is not 0, that is, the auxiliary state machine monitors that the current communication state of the slave terminal does not meet the third state condition, indicating that the current state of the slave terminal is abnormal, then the corresponding exception handling can be executed through the auxiliary state machine.

[0113] In one embodiment, after the data processing unit 13 is used to execute the step of, if a UART idle interrupt is triggered when receiving the frame data, switching the state of the auxiliary state machine to the data processing state, and when the auxiliary state machine monitors that its current communication state meets a preset third state condition, obtaining the processed result data obtained by the auxiliary state machine processing the received frame data according to the MODBUS protocol, it is further used for:

[0114] If it is determined that the processed result data is an empty set, return to execute the step of initializing the UART, switching the state of the auxiliary state machine to the waiting for receiving data state, and waiting for the frame data sent by the master terminal when the auxiliary state machine monitors that its current communication state meets a preset first state condition.

[0115] In this embodiment, after the slave terminal obtains the processed result data, it is necessary to determine whether the processed result data is an empty set to determine whether to return data to the master terminal. If the slave terminal determines that the processed result data is an empty set, it indicates that there is no need to return data to the master terminal after processing the received frame data, and then starts to wait again for the frame data sent by the master terminal.

[0116] The return data unit 14 is used to, if it is determined that the processed result data is not an empty set, switch the state of the auxiliary state machine to the return data state, and send the processed result data to the master terminal through the DMA and the UART when the auxiliary state machine monitors that its current communication state meets a preset fourth state condition.

[0117] In this embodiment, if the slave terminal determines that the processing result data is a non-empty set, it indicates that data needs to be returned to the master terminal after processing the received frame data, and at this time the processing result data is the data to be returned. Then, the state of the auxiliary state machine is switched to the data return state, so that the slave terminal executes the corresponding actions to return data to the master terminal. Meanwhile, the auxiliary state machine monitors in real time whether its current communication state meets the fourth state condition, so as to ensure that the slave terminal sends the processing result data to the master terminal through DMA and the serial port when the communication state is normal.

[0118] In one embodiment, before switching the state of the auxiliary state machine to the data return state, it further includes:

[0119] Starting a timer through the auxiliary state machine for timing and waiting for the timing time of the timer to reach a preset delay.

[0120] In this embodiment, in order to ensure that the master terminal is ready to receive data, after the slave terminal determines that the processing result data needs to be returned to the master terminal and before the slave terminal switches the state of the auxiliary state machine to the data return state, the auxiliary state machine can be used to start a timer for timing. When the timing time of the timer reaches the preset delay, the slave terminal switches the state of the auxiliary state machine to the data return state, and then starts to send the processing result data to the master terminal. Among them, the preset delay can be set according to the user's usage requirements and is not specifically limited here.

[0121] In one embodiment, switching the state of the auxiliary state machine to the data return state includes:

[0122] Switching the state of the auxiliary state machine to the data return state to start the DMA transmission channel through the auxiliary state machine, open the DMA transfer complete interrupt, enable the transmit enable bit of the serial port, and start the serial port transmit complete interrupt in the DMA transfer complete interrupt;

[0123] The auxiliary state machine monitors that its current communication state meets the preset fourth state condition, including:

[0124] The auxiliary state machine monitors that the enable bit corresponding to the DMA transmission channel is set to 1 and monitors that the transmit enable bit is set to 1 in the data return state.

[0125] In this embodiment, when the slave terminal determines that it is necessary to return the processing result data to the master terminal, it switches the state of the auxiliary state machine to the data return state, so as to start the DMA transmission channel through the auxiliary state, enable the DMA transmission completion interrupt, and enable the transmission enable bit of the serial port, thereby transmitting the processing result data to the transmission register of the serial port through the DMA transmission channel. Then, the serial port sends the processing result data in the transmission register to the master terminal. When the DMA transmission channel has transmitted all the processing result data to the transmission register of the serial port, the DMA transmission completion interrupt will be triggered. Then, the auxiliary state machine starts the serial port transmission completion interrupt in the DMA transmission completion interrupt. When the serial port finishes sending the processing result data and the transmission register is empty, the serial port transmission completion interrupt will be triggered, so as to determine that the transmission of the processing result data has ended.

[0126] Moreover, the auxiliary state machine determines whether the DMA transmission channel is started by checking whether the current value of the enable bit corresponding to the DMA transmission channel in the slave terminal is 1 in real time, and determines whether the transmission enable bit of the serial port is enabled by checking whether the current value of the transmission enable bit in the slave terminal is 1 in real time, and further determines whether the current state of the slave terminal is abnormal. If the auxiliary state machine checks that the current value of the enable bit corresponding to the DMA transmission channel is 1 and the current value of the transmission enable bit is 1, it indicates that the auxiliary state machine monitors that the current communication state meets the fourth state condition and the current state of the slave terminal is normal. In addition, if the auxiliary state machine checks that the enable bit corresponding to the DMA transmission channel and / or the transmission enable bit is set to 0, it indicates that the auxiliary state machine monitors that the current communication state does not meet the fourth state condition and the current state of the slave terminal is abnormal, and corresponding exception handling can be performed through the auxiliary state machine.

[0127] The transmission end unit 15 is configured to, if it is determined that the transmission of the processing result data has ended, return to execute the initialization of the serial port, switch the state of the auxiliary state machine to the waiting for receiving data state, and wait for the step of receiving the frame data sent by the master terminal when the auxiliary state machine monitors that its current communication state meets the preset first state condition.

[0128] In this embodiment, if the slave terminal determines that the transmission of the processing result data has ended, it starts to wait again for receiving the frame data sent by the master terminal.

[0129] In one embodiment, the slave terminal provided by the embodiment of the present invention further includes an exception handling unit, and the exception handling unit is configured to:

[0130] If, when the auxiliary state machine is in the waiting for receiving data state, the auxiliary state machine monitors that the current communication state does not meet the first state condition, the auxiliary state machine executes the first processing strategy in the preset exception handling strategy to perform exception handling;

[0131] If, when the auxiliary state machine is in the receiving data state, it is monitored by the auxiliary state machine that the current communication state does not meet the second state condition, the second processing strategy in the preset exception handling strategy is executed by the auxiliary state machine to perform exception handling;

[0132] If, when the auxiliary state machine is in the data processing state, it is monitored by the auxiliary state machine that the current communication state does not meet the third state condition, the third processing strategy in the preset exception handling strategy is executed by the auxiliary state machine to perform exception handling;

[0133] If, when the auxiliary state machine is in the return data state, it is monitored by the auxiliary state machine that the current communication state does not meet the fourth state condition, the fourth processing strategy in the preset exception handling strategy is executed by the auxiliary state machine to perform exception handling.

[0134] In this embodiment, when the auxiliary state machine monitors that the current communication state does not meet the corresponding state condition in any state, the preset exception handling strategy is executed to perform exception handling. Among them, the exception handling strategy is used to make the auxiliary state machine control the slave terminal to return to execute the initialization of the serial port when the duration of monitoring that the current communication state does not meet the corresponding state condition is greater than the preset exception duration, switch the state of the auxiliary state machine to the waiting for receiving data state, and when the auxiliary state machine monitors that its own current communication state meets the preset first state condition, wait for the step of receiving the frame data sent by the master terminal, so that the slave terminal can be automatically reset when a communication exception occurs, automatically reset the serial port and the state of the auxiliary state machine, effectively reducing the failure rate and effectively improving the data fault tolerance rate.

[0135] Specifically, when the auxiliary state machine monitors that the current communication state does not meet the first state condition in the waiting-to-receive data state, it executes the first processing strategy in the exception handling strategy for exception handling. The auxiliary state machine can check whether the current communication state meets the first state condition every preset monitoring period based on the first processing strategy. If the auxiliary state machine continuously checks that the current communication state does not meet the first state condition for a preset number of times, it indicates that the duration for which the auxiliary state machine monitors that the current communication state does not meet the first state condition exceeds the preset duration, and a communication exception occurs at the slave terminal. At this time, the slave terminal needs to automatically reset the serial port and the state of the auxiliary state machine. The preset duration is the product of the monitoring period and the preset number of times. Setting the preset duration is to prevent the auxiliary state machine from misjudging. Similarly, when the auxiliary state machine is in the receiving data state and monitors that the current communication state does not meet the second state condition, it executes the second processing strategy in the exception handling strategy for exception handling. The auxiliary state machine can check whether the current communication state meets the second state condition every preset monitoring period based on the second processing strategy. If the auxiliary state machine continuously checks that the current communication state does not meet the second state condition for a preset number of times, it indicates that the duration for which the auxiliary state machine monitors that the current communication state does not meet the second state condition exceeds the preset duration, and a communication exception occurs at the slave terminal. At this time, the slave terminal needs to automatically reset the serial port and the state of the auxiliary state machine. And so on, the auxiliary state machine performs corresponding processing similar to the foregoing description in the data processing state and the return data state, so that the auxiliary state machine can make the slave terminal automatically reset the serial port and the state of the auxiliary state machine when it monitors a communication exception at the slave terminal in any state.

[0136] A slave terminal disclosed in the present invention is used to execute any embodiment of the foregoing method for implementing MODBUS communication based on a serial port, and can monitor the communication state in real time through an auxiliary state machine, so that the slave terminal can complete communication with the master terminal through the serial port when the communication state is normal, and can improve the communication fault tolerance and stability.

[0137] The foregoing method for implementing MODBUS communication based on a serial port can be implemented in the form of a computer program, and the computer program can run on a computer device as shown in Figure 3 shown.

[0138] Please refer to Figure 3 , Figure 3 which is a schematic block diagram of a computer device provided by an embodiment of the present invention. The computer device 500 includes a processor 502, a memory, and a network interface 505 connected through a device bus 501. Among them, the memory may include a storage medium 503 and an internal memory 504.

[0139] The storage medium 503 can store an operating device 5031 and a computer program 5032. When the computer program 5032 is executed, it can cause the processor 502 to execute a method for implementing MODBUS communication based on a serial port.

[0140] The processor 502 is used to provide computing and control capabilities to support the operation of the entire computer device 500.

[0141] The internal memory 504 provides an environment for the operation of the computer program 5032 in the storage medium 503. When the computer program 5032 is executed by the processor 502, it can cause the processor 502 to execute a method for implementing MODBUS communication based on a serial port.

[0142] The network interface 505 is used for network communication, such as providing the transmission of data information, etc. Those skilled in the art can understand that Figure 3 The structure shown in is only a block diagram of some structures related to the solution of the present invention, and does not constitute a limitation on the computer device 500 to which the solution of the present invention is applied. The specific computer device 500 may include more or fewer components than those shown in the figure, or combine some components, or have different component arrangements.

[0143] Among them, the processor 502 is used to run the computer program 5032 stored in the memory to implement the method for implementing MODBUS communication based on a serial port disclosed in the embodiments of the present invention.

[0144] Those skilled in the art can understand that Figure 3 The embodiments of the computer device shown in do not constitute a limitation on the specific composition of the computer device. In other embodiments, the computer device may include more or fewer components than those shown in the figure, or combine some components, or have different component arrangements. For example, in some embodiments, the computer device may only include a memory and a processor. In such an embodiment, the structures and functions of the memory and the processor are the same as those in Figure 3 the embodiment shown, and will not be described in detail here.

[0145] It should be understood that in the embodiments of the present invention, the processor 502 may be a central processing unit (CPU), and the processor 502 may also be other general-purpose processors, digital signal processors (DSPs), application specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. Among them, the general-purpose processor may be a microprocessor or the processor may also be any conventional processor, etc.

[0146] In another embodiment of the present invention, a computer-readable storage medium is provided. The computer-readable storage medium may be a non-volatile computer-readable storage medium or a volatile computer-readable storage medium. The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method for implementing MODBUS communication based on a serial port disclosed in the embodiments of the present invention is realized.

[0147] Those skilled in the art can clearly understand that for the convenience and conciseness of description, the specific working processes of the above-described devices, terminals, and units can refer to the corresponding processes in the foregoing method embodiments, and will not be described herein again. Those of ordinary skill in the art can realize that the units and algorithm steps of each example described in combination with the embodiments disclosed herein can be implemented by electronic hardware, computer software, or a combination of the two. To clearly illustrate the interchangeability of hardware and software, the components and steps of each example have been generally described according to their functions in the above description. Whether these functions are executed in a hardware or software manner depends on the specific application and design constraints of the technical solution. Professional technicians can use different methods to implement the described functions for each specific application, but such implementation should not be considered to exceed the scope of the present invention.

[0148] In several embodiments provided by the present invention, it should be understood that the disclosed devices, terminals, and methods can be implemented in other ways. For example, the terminal embodiments described above are merely illustrative. For example, the division of units is only for logical function division. In actual implementation, there may be other division methods, or units with the same function can be aggregated into one unit. For example, multiple units or components can be combined or integrated into another terminal, or some features can be ignored or not executed. Additionally, the displayed or discussed couplings or direct couplings or communication connections to each other can be indirect couplings or communication connections through some interfaces, terminals, or units, or can be electrical, mechanical, or other forms of connection.

[0149] The units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they can be located in one place or distributed to multiple network units. Some or all of the units can be selected according to actual needs to achieve the objectives of the embodiments of the present invention.

[0150] In addition, each functional unit in various embodiments of the present invention can be integrated in a processing unit, or each unit can exist physically alone, or two or more units can be integrated in one unit. The above-mentioned integrated units can be implemented in the form of hardware or in the form of software functional units.

[0151] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a storage medium. Based on such an understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions for causing a computer device (which can be a personal computer, a background server, or a network device, etc.) to execute all or part of the steps of the methods in various embodiments of the present invention. The aforementioned storage medium includes: various media such as USB flash drives, mobile hard disks, read-only memories (ROMs), magnetic disks, or optical discs that can store program codes.

[0152] The above is only the specific implementation manner of the present invention, but the protection scope of the present invention is not limited thereto. Any person skilled in the art within the technical scope disclosed by the present invention can easily think of various equivalent modifications or substitutions, and these modifications or substitutions should all be covered within the protection scope of the present invention. Therefore, the protection scope of the present invention should be subject to the protection scope of the claims.

Claims

1. A method for implementing MODBUS communication based on a serial port, characterized in that, Applied to a slave terminal, the slave terminal is communicatively connected to a master terminal through a serial port, and the slave terminal is provided with an auxiliary state machine. The states of the auxiliary state machine include a preset waiting-to-receive data state, a receiving data state, a data processing state, and a returning data state. The method includes: Initialize the serial port, switch the state of the auxiliary state machine to the waiting-to-receive data state, and wait to receive frame data sent by the master terminal when the auxiliary state machine monitors that its current communication state meets a preset first state condition; If the first character data in the frame data is received, switch the state of the auxiliary state machine to the receiving data state, and receive the frame data through DMA when the auxiliary state machine monitors that its current communication state meets a preset second state condition; If a serial port idle interrupt is triggered during the reception of the frame data, switch the state of the auxiliary state machine to the data processing state, and obtain the processed result data obtained by the auxiliary state machine processing the received frame data according to the MODBUS protocol when the auxiliary state machine monitors that its current communication state meets a preset third state condition; If it is determined that the processed result data is a non-empty set, switch the state of the auxiliary state machine to the returning data state, and send the processed result data to the master terminal through the DMA and the serial port when the auxiliary state machine monitors that its current communication state meets a preset fourth state condition; If it is determined that the sending of the processed result data has ended, return to execute the step of initializing the serial port, switching the state of the auxiliary state machine to the waiting-to-receive data state, and waiting to receive frame data sent by the master terminal when the auxiliary state machine monitors that its current communication state meets a preset first state condition; 2. The method for implementing MODBUS communication based on a serial port according to claim 1, wherein It also includes: If, when the auxiliary state machine is in the waiting-to-receive data state, it is monitored by the auxiliary state machine that the current communication state does not meet the first state condition, execute a first processing strategy in a preset exception handling strategy through the auxiliary state machine for exception handling; If, when the auxiliary state machine is in the receiving data state, it is monitored by the auxiliary state machine that the current communication state does not meet the second state condition, execute a second processing strategy in a preset exception handling strategy through the auxiliary state machine for exception handling; If, when the auxiliary state machine is in the data processing state, it is monitored by the auxiliary state machine that the current communication state does not meet the third state condition, execute a third processing strategy in a preset exception handling strategy through the auxiliary state machine for exception handling; If, when the auxiliary state machine is in the returning data state, it is monitored by the auxiliary state machine that the current communication state does not meet the fourth state condition, execute a fourth processing strategy in a preset exception handling strategy through the auxiliary state machine for exception handling.

3. The method for implementing MODBUS communication based on a serial port according to claim 1, wherein If a serial port idle interrupt is triggered when receiving the frame data, the state of the auxiliary state machine is switched to the data processing state, and after the auxiliary state machine monitors that its current communication state meets a preset third state condition and obtains the processed result data obtained by processing the received frame data according to the MODBUS protocol by the auxiliary state machine, it further includes: If it is determined that the processed result data is an empty set, return to execute the initialization of the serial port, switch the state of the auxiliary state machine to the waiting for receiving data state, and wait for the step of receiving the frame data sent by the master station terminal when the auxiliary state machine monitors that its current communication state meets a preset first state condition.

4. The method for implementing MODBUS communication based on a serial port according to claim 1, characterized in that, The switching the state of the auxiliary state machine to the waiting for receiving data state includes: Switch the state of the auxiliary state machine to the waiting for receiving data state to open the serial port receive interrupt through the auxiliary state machine and close the pre-configured DMA receive channel and DMA transmit channel in the DMA. The auxiliary state machine monitors that its current communication state meets a preset first state condition, including: The auxiliary state machine monitors that the status bit corresponding to the serial port receive interrupt is set to 1 in the waiting for receiving data state.

5. The method for implementing MODBUS communication based on a serial port according to claim 4, wherein The switching the state of the auxiliary state machine to the receiving data state includes: Switch the state of the auxiliary state machine to the receiving data state in the serial port receive interrupt to close the serial port receive interrupt, start the DMA receive channel, and open the serial port idle interrupt through the auxiliary state machine. The auxiliary state machine monitors that its current communication state meets a preset second state condition, including: The auxiliary state machine monitors that the status bit corresponding to the serial port receive interrupt is set to 0, the enable bit corresponding to the DMA receive channel is set to 1, and the status bit corresponding to the serial port idle interrupt is set to 1 in the receiving data state. Receiving the frame data through the DMA includes: Receiving the frame data through the DMA receive channel in the DMA. If the number of characters of the received data monitored by the auxiliary state machine exceeds a preset number when receiving the frame data through the DMA receive channel, return to execute the initialization of the serial port, switch the state of the auxiliary state machine to the waiting for receiving data state, and wait for the step of receiving the frame data sent by the master station terminal when the auxiliary state machine monitors that its current communication state meets a preset first state condition.

6. The method for implementing MODBUS communication based on a serial port according to claim 5, characterized in that, The switching the state of the auxiliary state machine to the data processing state includes: Switch the state of the auxiliary state machine to the data processing state in the serial port idle interrupt to close the serial port idle interrupt through the auxiliary state machine. Closing the DMA receive channel by the auxiliary state machine when the frame data reception is completed. Processing the received frame data by the auxiliary state machine according to the MODBUS protocol to obtain the processed result data. When the auxiliary state machine monitors that its current communication state meets the preset third state condition, it includes: When the auxiliary state machine monitors in the data processing state that the status bit corresponding to the serial port idle interrupt is set to 0.

7. The method for implementing MODBUS communication based on a serial port according to claim 6, wherein The switching the state of the auxiliary state machine to the return data state includes: Switching the state of the auxiliary state machine to the return data state to start the DMA transmission channel through the auxiliary state machine, turn on the DMA transmission completion interrupt, enable the transmission enable bit of the serial port, and start the serial port transmission completion interrupt in the DMA transmission completion interrupt; When the auxiliary state machine monitors that its current communication state meets the preset fourth state condition, it includes: When the auxiliary state machine monitors in the return data state that the enable bit corresponding to the DMA transmission channel is set to 1 and monitors that the transmission enable bit is set to 1.

8. A slave terminal, characterized in that, The slave terminal communicates with the master terminal through the serial port, and the slave terminal is provided with an auxiliary state machine. The states of the auxiliary state machine include a preset waiting for receiving data state, receiving data state, data processing state, and returning data state. The slave terminal includes: A waiting for receiving data unit, configured to initialize the serial port, switch the state of the auxiliary state machine to the waiting for receiving data state, and wait for receiving the frame data sent by the master terminal when the auxiliary state machine monitors that its current communication state meets the preset first state condition; A receiving data unit, configured to, if the first character data in the frame data is received, switch the state of the auxiliary state machine to the receiving data state, and receive the frame data through DMA when the auxiliary state machine monitors that its current communication state meets the preset second state condition; A data processing unit, configured to, if a serial port idle interrupt is triggered when receiving the frame data, switch the state of the auxiliary state machine to the data processing state, and obtain the processed result data obtained by the auxiliary state machine processing the received frame data according to the MODBUS protocol when the auxiliary state machine monitors that its current communication state meets the preset third state condition; A returning data unit, configured to, if it is determined that the processed result data is a non-empty set, switch the state of the auxiliary state machine to the returning data state, and send the processed result data to the master terminal through the DMA and the serial port when the auxiliary state machine monitors that its current communication state meets the preset fourth state condition; A sending end unit, configured to, if it is determined that the processed result data has been sent, return to execute the step of initializing the serial port, switching the state of the auxiliary state machine to the waiting for receiving data state, and waiting for receiving the frame data sent by the master terminal when the auxiliary state machine monitors that its current communication state meets the preset first state condition; 9. A computer device, comprising a memory, a processor, and a computer program stored on the memory and executable on the processor, characterized in that, When the processor executes the computer program, it implements the method for realizing MODBUS communication based on the serial port according to any one of claims 1 to 7.

10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the processor is caused to execute the method for implementing MODBUS communication based on a serial port according to any one of claims 1 to 7.