A programming method for receiving data packets via serial asynchronous communication

By using the programming method of receiving data packets by serial port asynchronous communication in MCU serial port communication, and using the timeout timer to determine the packet integrity, the problems of waste of MCU resources and reduced response speed in the prior art are solved, and fast and accurate packet reception is achieved.

CN115437292BActive Publication Date: 2025-04-15SHENZHEN HABO ZHILIAN ELECTRONICS
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202211180091.2
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-09-26
Publication Date
2025-04-15
Estimated Expiration
2042-09-26

AI Technical Summary

Technical Problem

In the existing programming method of receiving data packets by serial ports of MCU, the method of receiving data is interrupted, resulting in waste of MCU resources and reduced response speed.

Method used

The programming method of receiving data packets by serial port asynchronous communication is adopted. By using the serial port interrupt as a branch, data is received independently, each byte of data is interrupted once, and a 10ms timeout timer is started in the main program until the data packet is fully received, and the analysis is carried out.

Benefits of technology

Reduces interrupt processing time, improves the MCU's response speed to other events, and achieves fast and accurate packet reception.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115437292B_ABST
    Figure CN115437292B_ABST
Patent Text Reader

Abstract

The invention relates to the technical field of a programming method for receiving a data packet of a USART serial communication, and discloses a programming method for receiving a data packet of a serial asynchronous communication. The programming method uses a serial interrupt as a branch to independently receive data, interrupts once for each byte of data, and uses a main program as a branch to judge the received complete data packet and parse the data packet. The programming method comprises the following steps: for a data receiving party, the time interval between data packets is used, and the shortest time interval between two data packets is 10ms; the programming method for receiving a data packet of a serial asynchronous communication of the invention separates a data packet bottom layer driver and an application layer when a USART port receives data, has clear logic, can quickly and accurately receive data, is convenient for programming, and improves the response speed of an MCU.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The invention relates to the technical field of communication tools, in particular to a programming method for receiving data packets in serial asynchronous communication. Background Art

[0002] In the previous programming method of receiving data packets through the MCU serial port, the interrupt receiving method is generally used. When a byte of data is received, the MCU serial port is interrupted once. The MCU will determine whether the received byte of data is the specified protocol packet data. If it is, it will continue to receive the serial port interrupt data until all the data is received, and then determine the integrity and accuracy of the data packet. In fact, this programming of judging data packets in the serial port interrupt program greatly wastes MCU resources and reduces the MCU's response speed to other events. We know that the longer the interrupt processing time of the MCU is, the slower the timeliness of the response will be for the main program or other interrupt processing. Therefore, we require that the interrupt processing time be reduced as much as possible, and the same is true for the serial port receiving data interrupt. Therefore, there is an urgent need for a programming method for receiving data packets through serial port asynchronous communication to solve the above shortcomings. Summary of the invention

[0003] In view of the shortcomings of the prior art, the present invention provides a programming method for receiving data packets in serial asynchronous communication, which solves the problem that the programming of judging data packets in the serial interrupt program greatly wastes MCU resources and reduces the response speed of MCU to other events. We know that the longer the processing time of a certain interrupt of the MCU is, the slower the timeliness of the response will be for the main program or other interrupt processing.

[0004] The present invention provides the following technical solution: a programming method for receiving data packets in serial asynchronous communication, which uses the serial port interrupt as a branch to independently receive data, interrupts once for each byte of data, and uses the main program as a branch to judge the received complete data packet and parse the data packet.

[0005] Preferably, the programming method comprises the following steps:

[0006] Step 1: For the data receiving party, the time interval between data packets is used, and the shortest time interval between two data packets is 10ms;

[0007] Step 2: When a byte of data is received at the serial port, a 10ms timeout timer is started. Each time a byte of data is received at the serial port, the timeout timer is started until there is no next byte of data, and the timeout takes effect. This can immediately tell the main program that a complete data packet has been received;

[0008] Step 3: The main program immediately parses the data packet to determine whether it is a specified protocol packet and processes it. If it is illegal data, it is discarded.

[0009] Preferably, in the MCU serial port asynchronous communication method, when the serial port is interrupted to receive data, each time a byte of data is received, a timeout timer is started until the timer times out, the data packet is completely received, and the main program is informed, thereby quickly responding to the data packet.

[0010] Preferably, in MCU serial port communication, when receiving data, the underlying driver and application layer of the data packet are separated.

[0011] Preferably, the programming method is applicable to a STM32F103C8T6 microprocessor.

[0012] Compared with the prior art, the present invention has the following beneficial effects: The beneficial effect created by the present invention is that, in the MCU serial port asynchronous communication method, when the serial port interrupts to receive data, each time a byte of data is received, a timeout timer is started until the timer times out, the data packet is completely received, and the main program is informed, thereby quickly responding to the data packet. BRIEF DESCRIPTION OF THE DRAWINGS

[0013] Other features, objects and advantages of the present application will become more apparent by reading the detailed description of non-limiting embodiments made with reference to the following drawings:

[0014] Figure 1 A flowchart of a programming method for receiving data packets by serial asynchronous communication of the present invention;

[0015] Figure 2 It is the main program flow chart of the present invention; DETAILED DESCRIPTION

[0016] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. The drawings are only for illustrative purposes and represent only schematic diagrams, not physical drawings, and cannot be understood as limitations on this patent. In order to better illustrate the specific implementation methods of the present invention, some parts of the drawings may be omitted, enlarged or reduced, and do not represent the size of the actual product. It is understandable to those skilled in the art that some well-known structures, parts and their descriptions in the drawings may be omitted. Based on the embodiments in the present invention, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of the present invention.

[0017] In the description of the present invention, it is also necessary to explain that, unless otherwise clearly specified and limited, the terms "setting" and "connection" should be understood in a broad sense, for example, it can be a fixed connection, a movable connection, a detachable connection, or an integral connection, a mechanical connection, an electrical connection, a direct connection, an indirect connection through an intermediate medium, or a connection between the two components. For ordinary technicians in this field, the specific meanings of the above terms in the present invention can be understood according to specific circumstances. The present application will be described in detail below with reference to the accompanying drawings and in combination with embodiments.

[0018] See also Figure 1-2 A programming method for receiving data packets in serial asynchronous communication, the programming method uses the serial port interrupt as a branch to independently receive data, interrupts once for each byte of data, and the main program as a branch to judge the received complete data packet and parse the data packet.

[0019] The programming method includes the following steps:

[0020] Step 1: For the data receiving party, the time interval between data packets is used, and the shortest time interval between two data packets is 10ms;

[0021] Step 2: When a byte of data is received at the serial port, a 10ms timeout timer is started. Each time a byte of data is received at the serial port, the timeout timer is started until there is no next byte of data, and the timeout takes effect. This can immediately tell the main program that a complete data packet has been received;

[0022] Step 3: The main program immediately parses the data packet to determine whether it is a specified protocol packet and processes it. If it is illegal data, it is discarded.

[0023] Preferably, in the MCU serial port asynchronous communication method, when the serial port is interrupted to receive data, each time a byte of data is received, a timeout timer is started until the timer times out, the data packet is completely received, and the main program is informed, thereby quickly responding to the data packet.

[0024] In MCU serial communication, when receiving data, the underlying driver and application layer of the data packet are separated.

[0025] This programming method is suitable for STM32F103C8T6 microprocessor.

[0026] It should be noted that the working principle of the present invention is: in the previous programming method of MCU serial port receiving data packets, the method of interrupting data reception is generally adopted. When a byte of data is received, the MCU serial port is interrupted once, and the MCU will determine whether the received byte data is the specified protocol packet data. If it is, continue to receive the serial port interrupt data until all the data are received, and then judge the integrity and accuracy of the data packet. In fact, this programming of judging the data packet in the serial port interrupt program greatly wastes MCU resources and reduces the response speed of the MCU to other events. We know that the longer the interrupt processing time of a certain MCU is, the slower the timeliness of the response will be for the main program or other interrupt processing. Therefore, we require to reduce the interrupt processing time as much as possible, and the same is true for the serial port receiving data interrupt.

[0027] In the MCU serial port asynchronous communication method, when the serial port is interrupted to receive data, each time a byte of data is received, a timeout timer is started until the timer times out, the data packet is completely received, and the main program is informed, so that the data packet can be responded to quickly;

[0028] In MCU serial communication, when receiving data, the underlying driver and application layer of the data packet are separated, the logic is clear, data can be received quickly and accurately, and the response speed to data is improved.

[0029] The contents not described in detail in this specification belong to the prior art known to professionals in this field.

[0030] It should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device.

[0031] Although embodiments of the present invention have been shown and described, it will be appreciated by those skilled in the art that various changes, modifications, substitutions and variations may be made to the embodiments without departing from the principles and spirit of the present invention, and that the scope of the present invention is defined by the appended claims and their equivalents.

Claims

1. A programming method for receiving data packets via serial asynchronous communication, characterized in that: The programming method uses the serial port interrupt as a branch to independently receive data, interrupting once for each byte of data, and the main program as a branch to judge the received complete data packet and parse the data packet; The programming method includes the following steps: Step 1: For the data receiving party, the time interval between data packets is used, and the shortest time interval between two data packets is 10ms; Step 2: When a byte of data is received at the serial port, a 10ms timeout timer is started. Each time a byte of data is received at the serial port, the timeout timer is started until there is no next byte of data, and the timeout takes effect. This can immediately tell the main program that a complete data packet has been received; Step 3: The main program immediately parses the data packet to determine whether it is a specified protocol packet and processes it. If it is illegal data, it is discarded.

2. A method for programming a serial asynchronous communication receiving data packet according to claim 1, characterized in that: In MCU serial communication, when receiving data, the underlying driver and application layer of the data packet are separated.

3. The method for programming a serial asynchronous communication receiving data packet according to claim 1, characterized in that: This programming method is suitable for STM32F103C8T6 microprocessor.

Citation Information

Patent Citations

  • Serial communication redundancy self-checking design method

    CN105512074A

  • Method for receiving serial port length-unfixed message through DMA

    CN108304335A