Universal serial data sliding window method
By introducing a data buffer and frame verification mechanism into serial communication, the complexity of parsing different types of serial port data frames is solved, enabling accurate extraction of data frames and improving the system's versatility.
Patent Information
- Application Number
- CN202311167945.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-09-12
- Publication Date
- 2026-02-24
- Estimated Expiration
- 2043-09-12
AI Technical Summary
Existing technologies suffer from complex parsing and lack of universality when processing serial port data frames of different types and lengths, resulting in the inability to correctly parse valid data frames.
A general serial port data sliding window method is adopted. By initializing the receive and unprocessed data buffers and setting the data processing cycle, the serial port data can be accurately extracted by using the joint judgment of frame header, frame length, frame tail and checksum.
It enables accurate extraction of various types of data frames from complex serial port data, improving the reliability and versatility of the system.
Smart Images

Figure CN117131091B_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of serial communication technology, specifically relating to a general serial data sliding window method. Background Technology
[0002] Serial interfaces are common data interfaces in computer application systems. Serial communication lines are simple and particularly suitable for long-distance communication, hence their widespread use. Due to their relatively weak anti-interference capability, both communicating parties typically establish a corresponding serial port protocol when sending valid data. The protocol includes a frame header, valid data length, valid data, data checksum, and frame trailer. The frame header usually indicates the type of the serial data frame, while the data checksum and frame trailer ensure the accuracy and integrity of the serial data frame. Since serial transmission is byte-by-byte, a single received serial data packet is usually a combination of multiple packets. Furthermore, due to transmission errors, a sliding window process is required to extract the valid serial data from the received data.
[0003] Currently, there are various methods for parsing serial port data. These methods can effectively parse different types of serial port data frames with the same effective data length and header length. However, when there are many types of serial port data frames, and the effective data length and header length vary, the program design logic becomes complex, the fault tolerance mechanism is incomplete, and there is a defect where the effective data frame cannot be parsed correctly. Furthermore, most of these methods are specific to particular serial port protocols and therefore lack universality. Summary of the Invention
[0004] (a) Technical problems to be solved
[0005] The technical problem to be solved by this invention is to propose a general serial port data sliding window method that can completely and accurately extract various types of data frames from complex serial port data, thereby improving the reliability and versatility of the system.
[0006] (II) Technical Solution
[0007] To address the aforementioned technical problems, this invention provides a general serial port data sliding window method, comprising the following steps:
[0008] Step 1: Initialize the receive data buffer recvDataBuff, the pending data buffer dealDataBuff, the valid data buffer dataBuff, and the valid data length MessLen. Define the frame header, frame length, and frame trailer of the serial port protocol specified by both communicating parties in the header file.
[0009] Step 2: Set the data processing cycle. When the data processing cycle is over, read all serial port data through the serial port module and store it in the receiving data buffer. Copy the data in the valid data buffer to the data to be processed buffer, and then append the data in the receiving data buffer to the data to be processed buffer.
[0010] Step 3: Starting from the posDeal-th data in the pending data buffer, extract data into the valid data buffer, extracting one byte at a time;
[0011] Step 4: Determine if there is a frame header in the valid data buffer. If there is no frame header, remove the first byte in the valid data buffer, move all other bytes in the valid data buffer forward by one position, increment posDeal by 1, return to step 3 to continue extracting single bytes until the frame header is found, and then proceed to step 5.
[0012] Step 5: The frame length can be determined from the frame header. Continue to extract data from the data buffer to be processed into the valid data buffer until the data length in the valid data buffer is equal to the frame length, then proceed to step 6.
[0013] Step 6: Determine if the frame tail in the valid data buffer is correct. If the frame tail is incorrect, proceed to step 7. If the frame tail is correct, proceed to step 8.
[0014] Step 7: When a frame tail or frame check error is found, remove the first byte from the valid data buffer, move the first framHeadLen-1 bytes of the remaining bytes in the valid data buffer forward by one position, assign the valid data length to framHeadLen-1, assign posDeal to posDeal-framLen+freamHeadLen, and return to step 4 to search for the frame header again.
[0015] Step 8: Perform frame verification on the data frames in the valid data buffer area to determine whether the frame verification in the valid data buffer area is correct. If the frame verification is incorrect, proceed to step 7; if the frame verification is correct, proceed to step 9.
[0016] Step 9: At this point, the data in the valid data buffer is a complete and accurate serial port data frame. After processing the serial port data frame according to business needs, clear the valid data buffer and set the valid data length to 0, then return to step 3 to find the next frame of data.
[0017] Preferably, in step one, in the serial communication protocol established by the two communicating parties, the maximum value of the serial frame data is Lmax, the length of the data buffer in the serial module is ComLmax, the length of the received data buffer is initialized to ComLmax, the length of the valid data buffer is initialized to Lmax, and the length of the data buffer to be processed is initialized to ComLmax+Lmax.
[0018] Preferably, in step one, the effective data buffer and the effective data length are initialized as global variables.
[0019] Preferably, in step one, the effective data buffer and the effective data length are initialized as global variables.
[0020] Preferably, in step two, the data processing cycle is set to 12.5ms.
[0021] Preferably, the serial port data format is: frame header + frame length + valid data + checksum + frame tail.
[0022] Preferably, the initial value of posDeal is equal to MessLen.
[0023] The present invention also provides a system for implementing the method.
[0024] The present invention also provides an application of the method in serial communication.
[0025] The present invention also provides an application of the system in serial communication.
[0026] (III) Beneficial Effects
[0027] 1. By setting up a receive data buffer and a pending data buffer, the received data and pending data are stored separately, which can ensure the integrity of the received data.
[0028] 2. By jointly judging the frame header, effective length, frame tail, and checksum, it is possible to accurately extract effective data from complex data. Attached Figure Description
[0029] Figure 1 This is a general flowchart of a serial port data sliding window method according to the present invention;
[0030] Figure 2a , Figure 2b This is a detailed flowchart of a general serial port data sliding window method according to the present invention. Detailed Implementation
[0031] To make the objectives, contents, and advantages of the present invention clearer, the specific embodiments of the present invention will be described in further detail below with reference to the accompanying drawings and examples.
[0032] Because serial port data transmission is in the form of a single-byte stream, the serial port buffered data received at one time may consist of multiple frames, and incomplete or erroneous frames are common. Therefore, it is necessary to extract complete and accurate serial port data frames for actual business use. The serial port data frame format is: frame header + frame length + valid data + checksum + frame trailer. First, the frame header is checked for correctness. Then, a data frame is extracted based on the frame length, and the frame trailer and checksum are also checked for correctness. When the frame header, frame trailer, and checksum are all correct, a complete serial port data frame has been extracted. If any of these three are incorrect, sliding window processing is required. This invention proposes a general serial port data sliding window method capable of handling various types and lengths of serial port data frames.
[0033] refer to Figure 1 , Figure 2a , Figure 2b This invention proposes a general serial port data sliding window method, which enables the extraction of different types of serial port messages from a complex set of data via the serial port protocol. Specifically, it includes the following steps:
[0034] Step 1: Initialize the receive data buffer recvDataBuff, the pending data buffer dealDataBuff, the valid data buffer dataBuff, and the valid data length MessLen. Define the frame header, frame length, and frame trailer of the serial port protocol specified by both communicating parties in the header file.
[0035] In step one, it is known beforehand that in the serial communication protocol agreed upon by both parties, the maximum value of the serial frame data is Lmax, and the length of the data buffer in the serial module is ComLmax. Therefore, to avoid array out-of-bounds errors, the length of the receive data buffer is initialized to ComLmax, the length of the valid data buffer is initialized to Lmax, and the length of the data buffer to be processed is initialized to ComLmax + Lmax. To handle situations where the received serial data in a data cycle is not a complete frame, the valid data buffer and the valid data length must be initialized as global variables. The advantage of macro definitions is that changes to the serial protocol only require modification of the header file.
[0036] Step 2: The data processing period is set to 12.5ms. When the processing period expires, all serial port data is read through the serial port module and stored in the receive data buffer. First, the data in the valid data buffer is copied to the data to be processed buffer, and then the data in the receive data buffer is appended and copied to the data to be processed buffer.
[0037] At this time, the data buffer contains the valid data that was not processed in the previous data cycle and all the serial port data acquired in this cycle.
[0038] Step 3: Extract data from the posDeal-th data item in the pending data buffer to the valid data buffer, extracting one byte at a time. The serial port data format is: frame header + frame length + valid data + checksum + frame trailer.
[0039] Since the data buffer to be processed contains the valid data that was not processed in the previous data cycle and all the serial port data obtained in this time, in order to improve the efficiency of the algorithm, we only need to extract one byte at a time from the posDeal data in the data buffer to be processed and extract one byte at a time into the valid data buffer, where the initial value of posDeal is equal to MessLen.
[0040] Step 4: Determine if there is a frame header in the valid data buffer. If there is no frame header, remove the first byte from the valid data buffer, move all other bytes in the valid data buffer forward by one position, and set posDeal = posDeal + 1. Return to Step 3 to continue extracting individual bytes until the frame header is found, then proceed to Step 5.
[0041] Step 5: The frame length can be determined from the frame header. Continue to extract data from the data buffer to be processed into the valid data buffer until the data length in the valid data buffer is equal to the frame length, then proceed to step 6.
[0042] Step 6: Determine if the frame tail in the valid data buffer is correct. If the frame tail is incorrect, proceed to Step 7; if the frame tail is correct, proceed to Step 8.
[0043] Step 7: For a (serial port) data frame with a correct frame header, the frame length is known to be framLen and the frame header length is framHeadLen, according to the protocol. When a frame tail or frame check error is found, the first byte is removed from the valid data buffer, and the first framHeadLen-1 bytes of the remaining bytes in the valid data buffer are shifted forward one position. At this time, the valid data length is assigned the value framHeadLen-1. The value posDeal is assigned posDeal-framLen+framHeadLen, and the process returns to Step 4 to search for the frame header again.
[0044] Step 8: Perform frame verification on the data frames in the valid data buffer area to determine whether the frame verification in the valid data buffer area is correct. If the frame verification is incorrect, proceed to step 7; if the frame verification is correct, proceed to step 9.
[0045] Step 9: At this point, the data in the valid data buffer is a complete and accurate serial port data frame. After processing the serial port data frame according to business needs, clear the valid data buffer and set the valid data length to 0, then return to step 3 to find the next frame of data.
[0046] As can be seen, this invention proposes a general serial port data sliding window method. The method has clear logic and simple code implementation. It can completely and accurately extract various types of data frames from complex serial port data, thereby improving the reliability and versatility of the system.
[0047] The above description is only a preferred embodiment of the present invention. It should be noted that for those skilled in the art, several improvements and modifications can be made without departing from the technical principles of the present invention, and these improvements and modifications should also be considered within the scope of protection of the present invention.
Claims
1. A general serial port data sliding window method, characterized in that, Includes the following steps: Includes the following steps: Step 1: Initialize the receive data buffer recvDataBuff, the pending data buffer dealDataBuff, the valid data buffer dataBuff, and the valid data length MessLen. Define the frame header, frame length, and frame trailer of the serial port protocol specified by both communicating parties in the header file. Step 2: Set the data processing cycle. When the data processing cycle is over, read all serial port data through the serial port module and store it in the receiving data buffer. Copy the data in the valid data buffer to the data to be processed buffer, and then append the data in the receiving data buffer to the data to be processed buffer. Step 3: Starting from the posDeal-th data in the pending data buffer, extract data into the valid data buffer, extracting one byte at a time; Step 4: Determine if there is a frame header in the valid data buffer. If there is no frame header, remove the first byte in the valid data buffer, move all other bytes in the valid data buffer forward by one position, increment posDeal by 1, return to step 3 to continue extracting single bytes until the frame header is found, and then proceed to step 5. Step 5: The frame length can be determined from the frame header. Continue to extract data from the data buffer to be processed into the valid data buffer until the data length in the valid data buffer is equal to the frame length, then proceed to step 6. Step 6: Determine if the frame tail in the valid data buffer is correct. If the frame tail is incorrect, proceed to step 7. If the frame tail is correct, proceed to step 8. Step 7: When a frame tail or frame check error is found, remove the first byte from the valid data buffer, move the first framHeadLen-1 bytes of the remaining bytes in the valid data buffer forward by one position, assign the valid data length to framHeadLen-1, assign posDeal to posDeal-framLen+freamHeadLen, and return to step 4 to search for the frame header again. Step 8: Perform frame verification on the data frames in the valid data buffer area to determine whether the frame verification in the valid data buffer area is correct. If the frame verification is incorrect, proceed to step 7; if the frame verification is correct, proceed to step 9. Step 9: At this point, the data in the valid data buffer is a complete and accurate serial port data frame. After processing the serial port data frame according to business needs, clear the valid data buffer and set the valid data length to 0, then return to step 3 to find the next frame of data. In step one, it is known in advance that in the serial communication protocol established by both parties, the maximum value of serial frame data is Lmax, the length of the data buffer in the serial module is ComLmax, the length of the receive data buffer is initialized to ComLmax, the length of the valid data buffer is initialized to Lmax, and the length of the data buffer to be processed is initialized to ComLmax+Lmax.
2. The method as described in claim 1, characterized in that, In step one, the valid data buffer and the valid data length are initialized as global variables.
3. The method as described in claim 1, characterized in that, In step two, the data processing cycle is set to 12.5ms.
4. The method as described in claim 1, characterized in that, The serial port data format is: frame header + frame length + valid data + checksum + frame tail.
5. The method as described in claim 1, characterized in that, The initial value of posDeal is equal to MessLen.
Citation Information
Patent Citations
Universal serial port data receiving and processing method
CN113392060A