Multi-channel modbus rtu asynchronous transceiving communication method for single-chip microcomputer
By using timer interrupts in a microcontroller to detect transmit and receive flags and canceling interrupts and timers in multi-channel Modbus RTU communication, the problems of interrupt conflicts and data loss in multi-channel Modbus RTU communication are solved, and efficient asynchronous communication is achieved.
Patent Information
- Application Number
- CN202410792919.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-19
- Publication Date
- 2024-10-22
AI Technical Summary
Existing Modbus RTU asynchronous communication is prone to interrupt conflicts in multi-channel environments, leading to data loss and communication instability. This is especially true in microcontrollers, where the interrupt nesting capability is limited, increasing the system load.
Instead of using "wait" or "interrupt" modes, timer interrupts are used to detect send and receive flags. The 1.5-character timer and 3.5-character timer interrupts are canceled. The timer monitors the character gaps and data frames, reducing interrupt interference and enabling asynchronous communication of multi-channel Modbus RTU.
At a baud rate of 115200bps, stability and smoothness of multi-channel Modbus RTU communication were achieved with no data loss, simplifying the system process and avoiding interrupt nesting and complex timer configuration.
Smart Images

Figure CN118820161A_ABST
Abstract
Description
Technical Field
[0001] The invention relates to the technical field of transceiver communication, and in particular to a multi-channel modbus rtu asynchronous transceiver communication method for a single-chip microcomputer. Background Art
[0002] Most of the existing Modbus RTU asynchronous communications use "interrupt" transmission and reception. In order to meet the requirements of the Modbus RTU protocol, it is necessary to configure timers to detect the 1.5-character gap and the 3.5-character gap at the same time, and each character needs to refresh the timer, which increases the load of the microcontroller MPU and increases the risk of multi-layer interrupt nesting (the interrupt nesting capability of the microcontroller is limited, which can be said to be very low and should be avoided as much as possible in applications).
[0003] In addition, this method has little impact on the system in a single-channel Modbus RTU environment, but it has a greater impact on the system in a multi-channel Modbus RTU environment. Interrupt conflicts often occur, which can easily lead to data loss and failure in sending and receiving data.
[0004] In addition, in the process of sending data through the microcontroller serial port, when writing the next byte of data to the send register after the send register flag is empty, it often causes errors on the receiving end. This phenomenon manifests itself differently for different communication objects. Some can communicate normally, some cannot communicate normally at all, and some will occasionally have communication abnormalities. In other words, the existing microcontroller serial port communication method has the problem of unstable communication quality. Summary of the invention
[0005] The embodiment of the present application provides a multi-channel Modbus RTU asynchronous transceiver communication method for a single-chip microcomputer. The asynchronous communication of all Modbus RTU channels neither uses the "wait" mode nor the "interrupt" mode to send and receive information, and cancels the respective 1.5-character timer interrupts and 3.5-character timer interrupts, thereby reducing the mutual interference and disorder caused by the frequent interruptions of multiple Modbus RTUs, avoiding multi-layer interrupt nesting, and can simultaneously complete the monitoring of the 1.5-character gap and 3.5-character gap of each channel Modbus RTU and the data frame monitoring. The baud rate can still operate normally when it reaches 115200bps, without data loss and smooth communication.
[0006] The embodiment of the present application provides a multi-channel Modbus RTU asynchronous transceiver communication method for a single-chip microcomputer, including a data sending method and a data receiving method, wherein the data sending method includes the following steps:
[0007] Configure the serial port to send state;
[0008] S1, timer interrupt, clear the timer interrupt flag, and then check whether the send completion flag is set;
[0009] S11, if yes, then add 1 to the 3.5 character gap counter, and compare whether the 3.5 character gap counter is greater than or equal to the 3.5 character gap value. If yes, then complete the frame transmission, exit the frame transmission mode, clear the transmission buffer, configure the serial port to receive state, and then execute the interrupt program to exit the timer interrupt. If no, then execute the interrupt program to exit the timer interrupt;
[0010] S12, if not, further detecting whether the character gap detection flag is set;
[0011] S121, if yes, then add 1 to the send character gap counter, and then further compare whether the send character gap counter is greater than or equal to the send character gap value;
[0012] S1211, if yes, then first write the subsequent bytes to the sending register, and add 1 to the length counter, clear the sending character gap counter, clear the character gap detection flag, and then further determine whether the length counter is greater than or equal to the frame length. If it is greater than or equal to the frame length, set the sending completion flag, clear the 3.5 character gap counter, and clear the sending character gap counter. If it is less than the frame length, execute the interrupt program to exit the timer interrupt;
[0013] S1212, if the sending character gap counter is less than the sending character gap value, the interrupt program is executed to exit the timer interrupt;
[0014] S122, if not, then check whether the empty flag of the serial port send register is set, if not, execute the interrupt program to exit the timer interrupt, if so, then check whether the length counter is equal to 0;
[0015] S1221, if the length counter is equal to 0, write the first byte to the transmit register, add 1 to the length counter, clear the transmit character gap counter, set the character gap detection flag, and then execute the interrupt program to exit the timer interrupt;
[0016] S1222, if the length counter is not equal to 0, the sending character gap counter is directly cleared, the character gap detection flag is set, and then the interrupt program is executed to exit the timer interrupt;
[0017] The method for receiving data comprises the following steps:
[0018] Configure the serial port to receive mode;
[0019] S2, timer interrupt, clear the timer interrupt flag, add 1 to the character gap counter, and then determine whether the character gap counter is greater than or equal to 1.5 character gap values;
[0020] S21, if not, further detect whether the receiving flag is set, if so, it means that valid data is received, read the receiving register data, and add 1 to the receiving length counter, clear the character gap counter, and then execute the interrupt program to exit the timer interrupt, if not, execute the interrupt program to exit the timer interrupt;
[0021] S22, if yes, then further determine whether the character gap counter is greater than or equal to the 3.5 character gap value. If yes, it means that the reception of this frame is completed, and the reception completion flag is set in sequence, the reception data valid flag is set, and the reception of this frame is exited, and then the interrupt program is executed to exit the timer interrupt; if no, then continue to determine whether the reception flag is set between 1.5 character gaps and 3.5 character gaps. If the reception flag is not detected to be set between 1.5 character gaps and 3.5 character gaps, the interrupt program is executed to exit the timer interrupt.
[0022] In one possible implementation, in step S22, if the receive flag is detected to be in position between the 1.5 character gap and the 3.5 character gap, it indicates that the reception is abnormal, invalid data is received, the receive register data is read and discarded, and then the receive completion flag is set, the receive data valid flag is cleared, the current frame reception is exited, and then the interrupt program is executed to exit the timer interrupt.
[0023] In a possible implementation, in step S22, after the reception of the current frame is completed, the receiving buffer array is first transferred to the backup array, and then the reception completion flag is set.
[0024] In a possible implementation, the timer is a us-level timer.
[0025] In a possible implementation, the timer is a 40us-50us timer.
[0026] In a possible implementation, the asynchronous receiving and transmitting communication method is used for 3-8 channel modbus rtu asynchronous receiving and transmitting communication.
[0027] Beneficial effect: Compared with the prior art, the multi-channel modbus rtu asynchronous transceiver communication method for a single-chip microcomputer provided by the present application uses only one "master timer interrupt" in a single-chip microcomputer, such as a 32-bit single-chip microcomputer. Each interrupt of the timer can detect whether the send register flag of each channel is empty and whether the receive register flag is set (non-waiting), and perform corresponding processing. The monitoring of 1.5 character intervals and 3.5 character intervals of each channel and the data frame monitoring can be completed at the same time. In the multi-channel modbus rtu asynchronous communication, the baud rate can still operate normally when it reaches 115200bps, without data loss and smooth communication.
[0028] All modbus rtu communications do not use "send interrupt and receive interrupt" and do not use timers to detect 1.5 character gaps and 3.5 character gaps. The system process is simple and intuitive, the operation is reliable, the waveform of the transmitted data stream is reliable, and complex processing work can be left outside the "interrupt".
[0029] These and other objects, features and advantages of the present invention will be fully reflected in the following detailed description. BRIEF DESCRIPTION OF THE DRAWINGS
[0030] Figure 1 A schematic diagram of the data sending process in the multi-channel Modbus RTU asynchronous receiving and sending communication method of the present application is shown.
[0031] Figure 2 A schematic diagram of the data receiving process in the multi-channel Modbus RTU asynchronous receiving and transmitting communication method of the present application is shown. DETAILED DESCRIPTION
[0032] The following description is used to disclose the present invention so that those skilled in the art can implement the present invention. The preferred embodiments described below are only examples, and those skilled in the art can think of other obvious variations. The basic principles of the present invention defined in the following description can be applied to other embodiments, variations, improvements, equivalents, and other technical solutions that do not deviate from the spirit and scope of the present invention.
[0033] Those skilled in the art should understand that, in the disclosure of the specification, the orientation or position relationship indicated by the terms "longitudinal", "lateral", "up", "down", "front", "back", "left", "right", "vertical", "horizontal", "top", "bottom", "inside", "outside", etc. are based on the orientation or position relationship shown in the drawings, which are only for the convenience of describing the present invention and simplifying the description, and do not indicate or imply that the device or element referred to must have a specific orientation, be constructed and operate in a specific orientation. Therefore, the above terms should not be understood as limiting the present invention.
[0034] It is to be understood that the term "one" should be understood as "at least one" or "one or more", that is, in one embodiment, the number of an element may be one, while in another embodiment, the number of the element may be multiple, and the term "one" should not be understood as a limitation on the quantity.
[0035] refer to Figure 1 and Figure 2 The embodiment of the present application provides a multi-channel modbus rtu asynchronous transceiver communication method for a single-chip microcomputer, including a data sending method and a data receiving method, wherein the data sending method includes the following steps:
[0036] Configure the serial port to send state;
[0037] S1, timer interrupt, clear the timer interrupt flag, and then check whether the send completion flag is set;
[0038] S11, if yes, then add 1 to the 3.5 character gap counter, and compare whether the 3.5 character gap counter is greater than or equal to the 3.5 character gap value. If yes, then complete the frame transmission, exit the frame transmission mode, clear the transmission buffer, configure the serial port to receive state, and then execute the interrupt program to exit the timer interrupt. If no, directly execute the interrupt program to exit the timer interrupt;
[0039] S12, if not, further detecting whether the character gap detection flag is set;
[0040] S121, if yes, then add 1 to the send character gap counter, and then further compare whether the send character gap counter is greater than or equal to the send character gap value;
[0041] S1211, if yes, then first write the subsequent bytes to the sending register, and add 1 to the length counter, clear the sending character gap counter, clear the character gap detection flag, and then further determine whether the length counter is greater than or equal to the frame length. If it is greater than or equal to the frame length, set the sending completion flag, clear the 3.5 character gap counter, and clear the sending character gap counter. If it is less than the frame length, execute the interrupt program to exit the timer interrupt;
[0042] S1212, if the sending character gap counter is less than the sending character gap value, directly execute the interrupt program to exit the timer interrupt;
[0043] S122, if not, then check whether the empty flag of the serial port send register is set, if not, directly execute the interrupt program to exit the timer interrupt, if so, then check whether the length counter is equal to 0;
[0044] S1221, if the length counter is equal to 0, write the first byte to the transmit register, add 1 to the length counter, clear the transmit character gap counter, set the character gap detection flag, and then execute the interrupt program to exit the timer interrupt;
[0045] S1222, if the length counter is not equal to 0, the sending character gap counter is directly cleared, the character gap detection flag is set, and then the interrupt program is executed to exit the timer interrupt;
[0046] The method for receiving data comprises the following steps:
[0047] Configure the serial port to receive mode;
[0048] S2, timer interrupt, clear the timer interrupt flag, add 1 to the character gap counter, and then determine whether the character gap counter is greater than or equal to 1.5 character gap values;
[0049] S21, if not, further check whether the receiving flag is set, if so, it means that valid data is received, read the receiving register data, and add 1 to the receiving length counter, clear the character gap counter, and then execute the interrupt program to exit the timer interrupt, if not, directly execute the interrupt program to exit the timer interrupt;
[0050] S22, if yes, then further determine whether the character gap counter is greater than or equal to the 3.5 character gap value. If yes, it means that the reception of this frame is completed, and the reception completion flag is set in sequence, the reception data valid flag is set, and the reception of this frame is exited, and then the interrupt program is executed to exit the timer interrupt; if no, then continue to determine whether the reception flag is set between 1.5 character gaps and 3.5 character gaps. If the reception flag is not detected to be set between 1.5 character gaps and 3.5 character gaps, the interrupt program is executed to exit the timer interrupt.
[0051] Therefore, the multi-channel Modbus RTU asynchronous transceiver communication method provided in the present application can complete the "time base generation" and all the "Modbus RTU asynchronous transceivers" using only one timer interrupt, and can still operate normally when the baud rate reaches 115200bps, without data loss and smooth communication.
[0052] In one embodiment, in step S22, if the receive flag is detected to be in position between 1.5 character gaps and 3.5 character gaps, it means that the reception of this frame is abnormal, the received data is invalid, the receive register data is read and discarded, then the receive completion flag is set, the receive data valid flag is cleared, the reception of this frame is exited, and then the interrupt program is executed to exit the timer interrupt.
[0053] In one embodiment, in step S22, after the reception of the current frame is completed, the receiving buffer array is first transferred to the backup array to avoid being overwritten by the newly received data, and then the reception completion flag is set.
[0054] In one embodiment, the timer is a us-level timer, and there is no need to use a dedicated timer. By utilizing the interrupt of the us-level timer, it is very simple to implement the writing of data to the register after a certain delay after the sending register flag is empty, thereby avoiding communication bus congestion and making communication smoother. However, this operation is relatively complicated to implement in conventional interrupt transmission and reception, and requires additional configuration of a dedicated timer.
[0055] Furthermore, the timer is a 40us-50us timer, such as 43.4us, which not only ensures smooth communication without data loss, but also does not affect normal operation when the baud rate reaches 115200bps; if the frequency of the timer is low, lower than 40us, the number of communication interruptions will increase, and if the frequency of the timer is high, greater than 50us, the baud rate of the communication will be reduced and the communication speed will be slower.
[0056] In one embodiment, the asynchronous transceiver communication method is used for 3-8 channel modbus rtu transceiver communication, including 3-channel modbus rtu transceiver communication, 4-channel modbus rtu transceiver communication, 5-channel modbus rtu transceiver communication, 6-channel modbus rtu transceiver communication, 7-channel modbus rtu transceiver communication and 8-channel modbus rtu transceiver communication.
[0057] In an implementation example applied to a centralized control system, there are 8 channels of modbus rtu asynchronous communication, requiring a maximum baud rate of 115200bps and a minimum baud rate of 4800bps. The channel with the highest baud rate is the master station, driving the touch screen; the remaining 4 channels are also the master station (the baud rate can be set on site), used to collect 4 channels of data; the other 3 channels are slave stations (the baud rate can be set on site), receiving online commands and data reading and writing, realizing data transmission to the outside and receiving external communication control. When the timing value is configured between 40us and 50us, data can be sent and received normally, there is no data loss, communication is smooth, and the system runs normally.
[0058] All modbus rtu communications here are asynchronous communications, and neither "send interrupt nor receive interrupt" is used for sending and receiving, nor is a timer used to detect 1.5 character gaps and 3.5 character gaps, to avoid interrupt conflicts and interrupt "nesting". Therefore, the system process can be more concise and intuitive, and the operation can be reliable.
[0059] In the previous system communication (single-chip system with up to 3-way modbus rtu asynchronous communication) using the traditional serial port transceiver interrupt plus a dedicated timer to detect 1.5 character gaps and 3.5 character gaps, communication abnormalities often occurred every 20 to 30 frames. After using this method, communication has been kept normal without frame loss.
[0060] [Note: When using a single-chip microcomputer, try to avoid interrupt nesting! ]
[0061] Since the "data frame length" is uncertain during Modbus RTU communication, it is more complicated to use DMA technology, and it is even more complicated to use DMA technology for multiple Modbus RTUs at the same time. Existing resources are also difficult to meet the needs of 8 serial ports using DMA technology at the same time. Therefore, this new method is developed to complete multi-channel Modbus RTU asynchronous communication technology.
[0062] It should be understood by those skilled in the art that the embodiments of the present invention described above and shown in the accompanying drawings are only examples and do not limit the present invention. The advantages of the present invention have been fully and effectively achieved. The functional and structural principles of the present invention have been demonstrated and explained in the embodiments, and the embodiments of the present invention may be deformed or modified in any way without departing from the principles.
Claims
1. A multi-channel modbus rtu asynchronous transceiver communication method for a single-chip microcomputer, characterized in that: The invention comprises a method for sending data and a method for receiving data, wherein the method for sending data comprises the following steps: Configure the serial port to send state; S1, timer interrupt, clear the timer interrupt flag, and then check whether the send completion flag is set; S11, if yes, then add 1 to the 3.5 character gap counter, and compare whether the 3.5 character gap counter is greater than or equal to the 3.5 character gap value. If yes, then complete the frame transmission, exit the frame transmission mode, clear the transmission buffer, configure the serial port to receive state, and then execute the interrupt program to exit the timer interrupt. If no, then execute the interrupt program to exit the timer interrupt; S12, if not, further detecting whether the character gap detection flag is set; S121, if yes, then add 1 to the send character gap counter, and then further compare whether the send character gap counter is greater than or equal to the send character gap value; S1211, if yes, then first write the subsequent bytes to the sending register, and add 1 to the length counter, clear the sending character gap counter, clear the character gap detection flag, and then further determine whether the length counter is greater than or equal to the frame length. If it is greater than or equal to the frame length, set the sending completion flag, clear the 3.5 character gap counter, and clear the sending character gap counter. If it is less than the frame length, execute the interrupt program to exit the timer interrupt; S1212, if the sending character gap counter is less than the sending character gap value, the interrupt program is executed to exit the timer interrupt; S122, if not, then check whether the empty flag of the serial port send register is set, if not, execute the interrupt program to exit the timer interrupt, if so, then check whether the length counter is equal to 0; S1221, if the length counter is equal to 0, write the first byte to the transmit register, add 1 to the length counter, clear the transmit character gap counter, set the character gap detection flag, and then execute the interrupt program to exit the timer interrupt; S1222, if the length counter is not equal to 0, the sending character gap counter is directly cleared, the character gap detection flag is set, and then the interrupt program is executed to exit the timer interrupt; The method for receiving data comprises the following steps: Configure the serial port to receive mode; S2, timer interrupt, clear the timer interrupt flag, add 1 to the character gap counter, and then determine whether the character gap counter is greater than or equal to 1.5 character gap values; S21, if not, further detect whether the receiving flag is set, if so, it means that valid data is received, read the receiving register data, and add 1 to the receiving length counter, clear the character gap counter, and then execute the interrupt program to exit the timer interrupt, if not, execute the interrupt program to exit the timer interrupt; S22, if yes, then further determine whether the character gap counter is greater than or equal to the 3.5 character gap value. If yes, it means that the reception of this frame is completed, and the reception completion flag is set in sequence, the reception data valid flag is set, and the reception of this frame is exited, and then the interrupt program is executed to exit the timer interrupt; if no, then continue to determine whether the reception flag is set between 1.5 character gaps and 3.5 character gaps. If the reception flag is not detected to be set between 1.5 character gaps and 3.5 character gaps, the interrupt program is executed to exit the timer interrupt.
2. The asynchronous receiving and sending communication method according to claim 1, wherein: In step S22, if the receive flag is detected to be in position between the 1.5 character gap and the 3.5 character gap, it indicates that the reception is abnormal, invalid data is received, the receive register data is read and discarded, and then the receive completion flag is set, the receive data valid flag is cleared, the current frame reception is exited, and then the interrupt program is executed to exit the timer interrupt.
3. The asynchronous receiving and transmitting communication method according to claim 1, wherein: In step S22, after the reception of the current frame is completed, the receiving buffer array is first transferred to the backup array, and then the reception completion flag is set.
4. The asynchronous receiving and transmitting communication method according to claim 1, wherein: The timer is a us level timer.
5. The asynchronous receiving and transmitting communication method according to claim 4, characterized in that: The timer is a 40us-50us timer.
6. The asynchronous receiving and transmitting communication method according to claim 1, wherein: The asynchronous transceiver communication method is used for 3-8 channel modbus rtu asynchronous transceiver communication.
