Serial communication frame parser and parsing method

By combining the frame header byte sequence, frame tail byte sequence, and state machine, the parsing problem of serial communication protocols without length information is solved, achieving universal adaptation across protocols and high fault tolerance, and reducing maintenance costs.

CN123145348APending Publication Date: 2026-11-20WUHAN LINGJIU MICROELECTRONICS CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202611622845.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-10-20
Publication Date
2026-11-20

AI Technical Summary

Technical Problem

Existing technologies suffer from frame parsing correctness and reliability issues when processing serial communication protocols that do not contain length information. They lack fault tolerance and require modification of the parsing code when the protocol changes, making it impossible to achieve universal adaptation across protocols.

Method used

By using parameter configurations such as frame header byte sequence, frame tail byte sequence, shared first byte identifier, maximum frame length threshold, and frame timeout threshold, combined with state machine and fault tolerance mechanism, byte-by-byte parsing and frame boundary identification are achieved, avoiding reliance on length field.

Benefits of technology

It achieves universal adaptation across protocols, improves the correctness and reliability of frame parsing, enhances fault tolerance, reduces the maintenance cost of protocol changes, and ensures system stability.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN123145348A_ABST
    Figure CN123145348A_ABST
Patent Text Reader

Abstract

The application relates to a serial communication frame parser, which comprises a parameter configuration unit configured to define parser parameters, the parser parameters comprising a frame header byte sequence, a frame tail byte sequence, a common first byte identifier, a maximum frame length threshold and a framing timeout threshold; a state machine generation unit configured to obtain a state set and a state number of a state machine according to the length of the frame header byte sequence and the length of the frame tail byte sequence; a parsing unit configured to define a byte-by-byte parsing logic according to the state set and the state number of the state machine, and set a fault tolerance mechanism according to the maximum frame length threshold and the framing timeout threshold; and a data input interface configured to receive a data block of an arbitrary length, call the byte-by-byte parsing logic to perform byte-by-byte parsing on the data block of the arbitrary length, and obtain a complete data frame set in combination with the fault tolerance mechanism. The application can realize cross-protocol universal adaptation, guarantee parsing correctness, reliability and high fault tolerance capability.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of computer information processing, and more specifically to a serial communication frame parser and parsing method. Background Technology

[0002] In the semiconductor chip industry, chip testing equipment, wafer probe stations, automated testing equipment, or semiconductor manufacturing equipment widely transmit control commands and test data in byte stream format via serial communication interfaces (serial ports) or network sockets (Ethernet ports) between internal components and between devices. Within this field, communication protocols exhibit significant diversity: different equipment manufacturers, different models, or different business modules often use different frame formats, and variations may exist in the number and specific values ​​of bytes in the frame header and trailer, as well as the presence or absence of a length field.

[0003] Some communication protocols, for the sake of simplifying the sender's implementation and reducing protocol overhead, do not include a length field: the data field length is not pre-declared, and the end of a frame is uniquely determined only by the frame tail delimiter. The receiver must rely on recognizing the frame tail sequence to determine whether a frame has ended. If a dedicated set of parsing code is written for each of these protocol families that lack length information, use only frame tail delimiters, and have variable data field lengths, it will lead to significant redundant development and maintenance costs, and a high risk of errors when protocols are changed or new device models are added. More importantly, the absence of length information also means that the receiver lacks prior information about the length of the current frame, and can only determine the start and end of a frame by relying on the delimiter sequence in a continuous, asynchronous, and potentially noisy byte stream. This leads to three problems that directly affect correctness and reliability: First, the same byte as the first byte of the frame tail may appear in the data field. It is impossible to determine whether it is data or frame tail based on a single byte. Misjudgment will cause the frame to be truncated prematurely. Second, when the frame header and frame tail share the same first byte, the meaning of this byte is hierarchical and context-dependent. Misjudgment will cause the frame to never close and stick to subsequent frames. Third, once the frame tail is lost, the link is interrupted, or the sending end is abnormal, the receiving end may remain in the frame assembly state for a long time. It will not be able to restore synchronization on its own, and the buffer that temporarily stores the current frame may grow indefinitely.

[0004] To address the aforementioned issues, existing technical solutions employ a general parsing method that includes data length: Protocol elements are divided into frame header 1, frame header 2, master station address, slave station address, data length identifier, and data buffer identifier, with a parsing status flag set for each element and parsing progressed byte by byte. This allows for the trimming of optional elements (frame header 2, master station address, slave station address, and data length) in the complete protocol format based on actual applications, thus adapting a single state machine logic to multiple protocol formats. In this solution, the data length identifier is mandatory. After the state machine jumps to the data buffer identifier, it relies entirely on comparing the data length value with the receive counter to determine whether a frame has been received completely. In other words, this solution completely entrusts the determination of "where a frame ends" to the length field, which has the following inherent drawbacks: First, its universality is built upon the length field: the data length identifier is a mandatory element, and the length value is used to define the frame boundary and drive the receive counter. It cannot be directly applied to variable-length protocols that do not contain length information and whose frame ends are only defined by the frame tail; if it is to be applied forcibly, the protocol format must be modified to add a length field.

[0005] Second, its parsing correctness relies entirely on the length field, lacking independent verification and fault tolerance mechanisms. The length field itself is also transmitted on the link; if its value is incorrect due to interference, the receiving end will receive the wrong number of bytes, causing the frame to be misaligned and contaminating all subsequent frames. Furthermore, it lacks automatic resynchronization mechanisms. In other words, the length field is both the fulcrum of its universality and the single point of failure for its correctness.

[0006] Third, its pruning mechanism targets the number of frame headers and the presence or absence of address elements (frame header 2, master station address, slave station address), and does not provide the ability to arbitrarily configure the frame delimiter sequence itself, nor does it involve the handling of special structures such as variable length of frame header / frame tail sequence and shared first byte of frame header and frame tail. Therefore, it cannot resolve the resulting hierarchical ambiguity when facing such structures.

[0007] Fourth, the scheme lacks fault tolerance and boundary protection measures to complement the absence of length information: because it naturally has an upper frame bound due to its reliance on the length field, it does not propose, nor is it necessary to propose, timeout reset, buffer out-of-bounds protection and automatic resynchronization mechanisms for frame tail loss or link anomalies; once the length field is removed, all these protections are lost.

[0008] Fifth, the universality remains at the code level rather than the parameter level: when the protocol changes, the scanning and matching logic needs to be modified or the parsing branch needs to be rewritten, and it is impossible to complete the adaptation simply by changing the configuration.

[0009] Therefore, there is a need to provide a universal serial port data parsing method without length fields, which can solve the problems of cross-protocol universal adaptation, as well as the problems of correct parsing, reliability, and high fault tolerance. Summary of the Invention

[0010] The purpose of this invention is to provide a serial communication frame parser and parsing method to achieve a universal serial port data parsing scheme without length information, effectively ensuring the correctness and reliability of serial port data parsing, while improving the fault tolerance of serial port communication.

[0011] This invention provides a serial communication frame parser, comprising: The parameter configuration unit is configured to define parser parameters, which include frame header byte sequence, frame tail byte sequence, common first byte identifier, maximum frame length threshold, and frame assembly timeout threshold. The state machine generation unit is configured to obtain the state set and the number of states of the state machine based on the length of the frame header byte sequence and the length of the frame tail byte sequence; the state set includes M states waiting for the i-th byte of the frame header, one data receiving state, and K-1 states waiting for the j-th byte of the frame tail, and the number of states is M+K; where i = 1,...,M, M≥1; j = 2,...,K, K≥2; The parsing unit is configured to define byte-by-byte parsing logic based on the state set and number of states of the state machine; and to set a fault tolerance mechanism based on the maximum frame length threshold and the frame timeout threshold. The data input interface is configured to receive data blocks of arbitrary length, call the byte-by-byte parsing logic to parse the data blocks of arbitrary length byte by byte, and combine the fault tolerance mechanism to obtain a complete set of data frames.

[0012] Furthermore, the byte-by-byte parsing logic is defined according to the following rules: In the state of waiting for the i-th byte of the frame header, i = 1, ..., M-1, if the input byte is equal to the i-th byte of the frame header, then the input byte is written into the buffer and the state of waiting for the (i+1)-th byte of the frame header is transitioned; otherwise, the current state is maintained and the waiting continues. While waiting for the Mth byte of the frame header, if the input byte is equal to the Mth byte of the frame header, the input byte is written into the buffer and the process is switched to the data receiving state; if the input byte is equal to the 1st byte of the frame header and the shared first byte flag is true, the process remains in the current state and continues to wait; if the input byte is equal to the 1st byte of the frame header and the shared first byte flag is false, the process returns to the state of waiting for the first byte of the frame header and the buffer is cleared. In the data receiving state, the input bytes are written into the buffer sequentially. If the input byte is equal to the j-th byte of the frame end, j=2,..,K-1, wait for the status of the (j+1)-th byte of the frame end. While waiting for the Kth byte of the frame tail, the input byte is written to the buffer. If the input byte is equal to the Kth byte of the frame tail, it is determined that a complete data frame has been received, the complete data frame is output, and the state is cleared. If the input byte is equal to the (K-1)th byte of the frame tail, and the shared first byte flag is true, the current state is maintained and waiting continues. If the input byte is equal to the (K-1)th byte of the frame tail, and the shared first byte flag is false, the state is returned to receive data, and the input byte is retained in the buffer.

[0013] Furthermore, the fault tolerance mechanism is set as follows: When the number of bytes written in the buffer reaches the maximum frame length threshold, but a complete data frame has not yet been received, the current frame is determined to be an abnormal frame, the buffer is cleared, and the state machine is reset to the state of waiting for the first byte of the frame header.

[0014] Furthermore, the fault tolerance mechanism is also configured as follows: If the duration of the framing process reaches the framing timeout threshold, but a complete data frame has not yet been received, the current frame is discarded, the buffer is cleared, and the state machine is reset to the state of waiting for the first byte of the frame header.

[0015] Furthermore, the parser parameters also include frame verification methods and their corresponding parameters, wherein the frame verification methods include one of no verification, cumulative summation, and cyclic redundancy check.

[0016] Furthermore, the parsing unit is also configured as follows: After a complete data frame has been received but before it is output, a check is performed on the complete data frame. If the check fails, the data frame is discarded.

[0017] The present invention also provides a serial communication frame parsing method, comprising: Step S10: Define parser parameters, which include frame header byte sequence, frame tail byte sequence, common first byte identifier, maximum frame length threshold, and frame assembly timeout threshold. Step S20: Based on the length of the frame header byte sequence and the length of the frame tail byte sequence, obtain the state set and the number of states of the state machine. The state set includes M states waiting for the i-th byte of the frame header, one data receiving state, and K-1 states waiting for the j-th byte of the frame tail. The number of states is M+K. Where i = 1,...,M, M≥1; j = 2,...,K, K≥2. Step S30: Define byte-by-byte parsing logic based on the state set and number of states of the state machine; and set a fault tolerance mechanism based on the maximum frame length threshold and the frame timeout threshold. Step S40: Receive data blocks of arbitrary length, call the byte-by-byte parsing logic to parse the data blocks of arbitrary length byte by byte, and combine the fault tolerance mechanism to obtain a complete set of data frames.

[0018] Furthermore, the definition of byte-by-byte parsing logic in step S30 includes: Step S31: In the state of waiting for the i-th byte of the frame header, i = 1,..,M-1, if the input byte is equal to the i-th byte of the frame header, then write the input byte into the buffer and transfer to the state of waiting for the (i+1)-th byte of the frame header; otherwise, remain in the current state and continue to wait. Step S32: In the state of waiting for the Mth byte of the frame header, if the input byte is equal to the Mth byte of the frame header, the input byte is written into the buffer and the process is transferred to the data receiving state; if the input byte is equal to the 1st byte of the frame header and the shared first byte flag is true, the process remains in the current state and continues to wait; if the input byte is equal to the 1st byte of the frame header and the shared first byte flag is false, the process returns to the state of waiting for the first byte of the frame header and the buffer is cleared. Step S33: In the data receiving state, write the input bytes into the buffer in sequence. If the input byte is equal to the j-th byte of the frame end, j = 2, ..., K-1, wait for the status of the (j+1)-th byte of the frame end. Step S34: While waiting for the Kth byte of the frame end, write the input byte into the buffer. If the input byte is equal to the Kth byte of the frame end, it is determined that a complete data frame has been received, the complete data frame is output, and the state is cleared. If the input byte is equal to the (K-1)th byte of the frame end, and the shared first byte identifier is true, then maintain the current state and continue waiting. If the input byte is equal to the (K-1)th byte of the frame end, and the shared first byte identifier is false, then return to the data reception state and keep the input byte in the buffer.

[0019] Furthermore, the fault tolerance mechanism set in step S30 includes: Step S35: When the number of bytes written in the buffer reaches the maximum frame length threshold, but a complete data frame has not yet been received, the current frame is determined to be an abnormal frame, the buffer is cleared, and the state machine is reset to the state of waiting for the first byte of the frame header.

[0020] Furthermore, the fault tolerance mechanism set in step S30 also includes: Step S36: When the duration of the framing process reaches the framing timeout threshold, but a complete data frame has not yet been received, the current frame is discarded, the buffer is cleared, and the state machine is reset to the state of waiting for the first byte of the frame header.

[0021] Furthermore, the parser parameters also include frame verification methods and their corresponding parameters, wherein the frame verification methods include no verification, cumulative verification, and / or cyclic redundancy check.

[0022] Furthermore, step S30 also includes: Step S37: After determining that a complete data frame has been received but before outputting the complete data frame, perform a verification on the complete data frame. If the verification fails, discard the data frame.

[0023] This invention achieves cross-protocol universal adaptation through frame header byte sequence, frame tail byte sequence, and shared first byte identifier. It replaces the length field with the maximum frame length threshold and the framing timeout threshold to assume the boundary protection function. Through the mechanism of frame tail multi-byte sequence confirmation, shared first byte holding and waiting, no frame header search within the frame, and automatic resynchronization in case of anomalies, it ensures parsing correctness, reliability and high fault tolerance. Attached Figure Description

[0024] Figure 1 This is a structural block diagram of a serial communication frame parser according to Embodiment 1 of the present invention.

[0025] Figure 2 This is a state transition diagram of a four-state finite state machine according to Embodiment 1 of the present invention. Detailed Implementation

[0026] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, the technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. The components of the embodiments of the present invention described and shown in the accompanying drawings can generally be arranged and designed in various different configurations.

[0027] Therefore, the following detailed description of the embodiments of the invention provided in the accompanying drawings is not intended to limit the scope of the claimed invention, but merely to illustrate selected embodiments of the invention. All other embodiments obtained by those skilled in the art based on the embodiments of the invention without inventive effort are within the scope of protection of the invention.

[0028] It should be noted that similar labels and letters in the following figures indicate similar items. Therefore, once an item is defined in one figure, it does not need to be further defined and explained in subsequent figures.

[0029] In the description of this invention, it should be noted that the terms "center," "upper," "lower," "left," "right," "vertical," "horizontal," "inner," and "outer," etc., indicate the orientation or positional relationship based on the orientation or positional relationship shown in the accompanying drawings, or the orientation or positional relationship commonly used when the product is in use. These terms are used only for the convenience of describing the invention and for simplifying the description, and do not indicate or imply that the device or element referred to must have a specific orientation, or be constructed and operated in a specific orientation. Therefore, they should not be construed as limitations on the invention. Furthermore, the terms "first," "second," and "third," etc., are used only to distinguish descriptions and should not be construed as indicating or implying relative importance.

[0030] Furthermore, terms such as "horizontal," "vertical," and "sag" do not imply that components must be absolutely horizontal or suspended, but rather that they can be slightly tilted. For example, "horizontal" simply means that its direction is more horizontal relative to "vertical," and does not mean that the structure must be completely horizontal, but can be slightly tilted.

[0031] In the description of this invention, it should also be noted that, unless otherwise explicitly specified and limited, the terms "set," "install," "connect," and "link" should be interpreted broadly. For example, they can refer to a fixed connection, a detachable connection, or an integral connection; they can refer to a mechanical connection or an electrical connection; they can refer to a direct connection or an indirect connection through an intermediate medium; and they can refer to the internal connection of two components. Those skilled in the art can understand the specific meaning of the above terms in this invention based on the specific circumstances.

[0032] The following detailed description of some embodiments of the present invention is provided in conjunction with the accompanying drawings. Unless otherwise specified, the following embodiments and features can be combined with each other.

[0033] Example 1: A serial communication frame parser like Figure 1 As shown, the serial communication frame parser provided in this embodiment is used to identify frame boundaries of serial communication data received in byte stream mode. It includes a parameter configuration unit 10, a state machine generation unit 20, a parsing unit 30 and a data input interface 40 connected in sequence.

[0034] The parameter configuration unit 10 is configured to define parser parameters, which include frame header byte sequence, frame tail byte sequence, common first byte identifier, maximum frame length threshold, and frame timeout threshold. Specifically, the frame header byte sequence is used to define the start of a frame, and can be represented as HEAD[0..M-1], where M≥1 and M is the length of the frame header byte sequence; the frame tail byte sequence is used to define the end of a frame, and can be represented as TAIL[0..K-1], where K≥2 and K is the length of the frame tail byte sequence; the shared first byte identifier is used to indicate whether the first byte of the frame header byte sequence and the first byte of the frame tail byte sequence have the same byte value, and can be represented as SHARED. If the first byte of the frame header byte sequence and the first byte of the frame tail byte sequence are the same, then the shared first byte identifier is true (SHARED=true); if the first byte of the frame header byte sequence and the first byte of the frame tail byte sequence are different, then the shared first byte identifier is false (SHARED=false); the maximum frame length threshold represents the length of the byte buffer of the current frame, and also serves as the upper bound constraint of a frame, and can be represented as Lmax; the frame assembly timeout threshold represents the longest allowed duration of the frame assembly process, and can be represented as T.

[0035] The above five parameters together constitute a protocol description. It can be seen that this parameter set does not contain any length field element. That is, the protocol family adapted to by the serial communication frame parser provided in this embodiment uses the frame tail sequence as the unique end mark of the frame. When changing the protocol, only this protocol description needs to be changed, and there is no need to modify the parsing logic code.

[0036] To improve the correctness and reliability of frame parsing, the defined parser parameters may also include frame verification methods and their corresponding parameters. Frame verification methods include no verification, cumulative verification, and / or cyclic redundancy check.

[0037] The state machine generation unit 20 is configured to obtain the state set and number of states of the state machine based on the length M of the frame header byte sequence and the length K of the frame tail byte sequence. The state set includes M states waiting for the i-th byte of the frame header (i = 1, ..., M), one data receiving state, and K-1 states waiting for the j-th byte of the frame tail (j = 2, ..., K), for a total of M+1 + (K-1) = M+K states. In this way, the state machine structure automatically matches the configured delimiter sequence length, eliminating the need for manual design of state machines for different protocols.

[0038] For ease of understanding, this embodiment uses a specific example: the frame header byte sequence is 0xFF, 0x4F, where M=2; the frame tail byte sequence is 0xFF, 0xD9, where K=2; and the shared first byte identifier is true (shared 0xFF). Therefore, the state machine's state set includes four states: waiting for the first byte of the frame header, waiting for the second byte of the frame header, receiving data, and waiting for the second byte of the frame tail. It should be understood that these four states are merely specific value examples for this embodiment and do not constitute a limitation on the scope of protection. In other embodiments, the frame header and frame tail byte sequences can have other values ​​and lengths, and the shared first byte identifier can be true or false.

[0039] The parsing unit 30 is configured to define byte-by-byte parsing logic based on the state set and number of states of the state machine; and to set a fault tolerance mechanism based on the maximum frame length threshold and the frame timeout threshold.

[0040] The byte-by-byte parsing logic is defined according to the following rules: In the state of waiting for the i-th byte of the frame header, i = 1, ..., M-1, if the input byte is equal to the i-th byte of the frame header, then write the byte into the buffer and switch to the state of waiting for the (i+1)-th byte of the frame header; otherwise, remain in the current state and continue to wait.

[0041] In the state of waiting for the Mth byte of the frame header, if the input byte is equal to the Mth byte of the frame header, then the byte is written into the buffer and the state of receiving data is transferred; if the input byte is equal to the 1st byte of the frame header and the shared first byte flag configured by the parameter configuration unit 10 is true, then the current state is maintained and the waiting continues; if the input byte is equal to the 1st byte of the frame header and the shared first byte flag is false, then the state of waiting for the first byte of the frame header is returned and the buffer is cleared.

[0042] In the data reception state, input bytes are written sequentially into the buffer. When the input byte equals the j-th byte of the frame tail, j = 2, ..., K-1, the system waits for the (j+1)-th byte of the frame tail status. Furthermore, in this state, the frame header byte sequence is not searched; that is, the frame header byte sequence appearing in the data field does not trigger the start of a new frame. In other words, once the data reception state is entered, only the frame tail byte sequence can terminate the current frame; the frame header byte sequence is not recognized as the start of a new frame, thus avoiding false synchronization caused by accidentally triggering new frames in the data field.

[0043] While waiting for the Kth byte of the frame end, the input byte is written to the buffer. If the input byte is equal to the Kth byte of the frame end, a complete data frame is considered to have been received, the complete data frame is output, and the state is cleared. If the input byte is equal to the (K-1)th byte of the frame end, and the shared first byte flag is true, the current state is maintained and the waiting continues. If the input byte is equal to the (K-1)th byte of the frame end, and the shared first byte flag is false, the state is returned to receive data, and the input byte is kept in the buffer as part of the data field.

[0044] like Figure 2 As shown, taking the four-state finite machine in the above example as an example, its byte-by-byte parsing logic is as follows: In the state of waiting for the first byte of the frame header, if the input byte is equal to the first byte of the frame header HEAD[0], 0xFF, then the buffer is cleared, the input byte is written, and the process is transferred to the state of waiting for the second byte of the frame header; if the input byte is not equal to the first byte of the frame header, the process remains in this state and the buffer is not processed.

[0045] While waiting for the second byte of the frame header: if the input byte is equal to the second byte of the frame header HEAD[1], 0x4F, then the input byte is written to the buffer and the data receiving state is switched; if the input byte is equal to HEAD[0], 0xFF, since the shared first byte identifier is true at this time, the state is maintained and the waiting continues.

[0046] In the data receiving state, the input bytes are written into the buffer in sequence. When the input byte is equal to the first byte of the frame tail TAIL[0], 0xFF, the frame is not immediately determined to be over. Instead, the state is transferred to wait for the second byte of the frame tail, waiting for the confirmation of the second byte of the frame tail.

[0047] While waiting for the second byte of the frame tail, the input byte is written into the buffer. If the input byte is equal to the second byte of the frame tail, TAIL[1], 0xD9, then a complete data frame is received, the frame is output and the state is cleared. If the input byte is equal to TAIL[0], 0xFF, since the shared first byte identifier is true at this time, it remains in this state and continues to wait until the input byte is equal to the second byte of the frame tail.

[0048] In this embodiment, the end of a frame is not determined by a single byte, but must be confirmed by a complete sequence of frame tail bytes (such as the two consecutive bytes mentioned above). When the first byte of the frame tail appears alone in the data field, the system only enters the confirmation state to wait, without immediately receiving the frame, thereby reducing the probability of an accidental byte in the data field being misjudged as the frame tail from 1 / 256 to 1 / 65536. Furthermore, when the frame header and frame tail share the same first byte, if the state machine is in either the delimiter confirmation state (frame header confirmation state or frame tail confirmation state) and encounters the shared byte again, it neither backs up nor clears it, but remains in the confirmation state to continue waiting for the next byte—this byte could be a mistriggered prefix of the previous delimiter or the first byte of the current real delimiter; only by continuing to wait can both be compatible. This eliminates the ambiguity caused by the last byte of the data field being a shared byte, resulting in the real frame tail being completely swallowed, the current frame never closing, and interference bytes superimposed on the real frame header causing frame header to be missed.

[0049] To limit failures to a single frame, the parsing unit 30 also incorporates the following parameterized fault tolerance mechanism: When the number of bytes written to the buffer reaches the maximum frame length threshold Lmax, but a complete data frame has not yet been received, the current frame is determined to be an abnormal frame, and the execution state is cleared to zero, thereby preventing the buffer from growing unbounded. Clearing the execution state to zero means: emptying the buffer and resetting the state machine to the state of waiting for the first byte of the frame header.

[0050] When the duration of the framing process reaches the framing timeout threshold T, but a complete data frame has not yet been received, the current frame content is forcibly discarded and the execution status is cleared to zero, thereby avoiding long-term loss of synchronization when the frame tail is lost or the link is interrupted.

[0051] In this embodiment, for any of the above-mentioned abnormal situations, the status is cleared, and the frame header search restarts from the next byte. Therefore, any abnormality only affects the current frame, and synchronization can be automatically re-established in the next frame, preventing the failure from propagating to the next frame.

[0052] To further improve fault tolerance, the parsing unit 30 can also be configured as follows: While waiting for the i-th byte of the frame header, if the input byte is not equal to the i-th byte of the frame header, the entire header is not cleared directly. Instead, the system backtracks to the corresponding intermediate state (the waiting state corresponding to the i-th byte of the frame header that is equal to the input byte) based on the longest common prefix and suffix of the matched prefix and continues to try. This avoids missing the detection of the real frame header due to clearing the entire header when the delimiter contains duplicate bytes (such as AA AA 55).

[0053] If the parser parameters defined above include the frame check method and its corresponding parameters, then the parsing unit 30 can also be configured as follows: After a complete data frame has been received but before it is output, a check is performed on the complete data frame. If the check fails, the data frame is discarded to prevent erroneous data frames from being sent to the upper-layer service.

[0054] The data input interface 40 is configured to receive data blocks of arbitrary length from the serial port or network port, call the byte-by-byte parsing logic defined by the parsing unit 30 to parse the data blocks of arbitrary length byte by byte, and obtain the complete data frame set by combining the fault tolerance mechanism.

[0055] More specifically, the data input interface 40 can be configured such that, for each byte of a data block, if a complete data frame has been formed according to the byte-by-byte parsing logic, the complete data frame is collected into the output set; when the entire data block is parsed, the collected set of complete data frames is returned to the caller. In this way, regardless of whether a single input contains part of a frame, a whole frame, or multiple frames joined together, zero or one or more complete data frames can be correctly segmented within a data block.

[0056] The above mechanisms are all configured as parameters along with the protocol, allowing for the setting of appropriate protection strengths under different protocols. When adapting to a new protocol without a length field, only a new set of frame header byte sequences, frame tail byte sequences, shared first byte identifiers, maximum frame length thresholds, and framing timeout thresholds need to be injected into the parser. The parser will then automatically reconstruct its state machine according to the new delimiter sequence length and begin operation, without requiring modification or recompilation of any parsing logic code. The same parser can instantiate multiple objects simultaneously with different parameters, enabling parallel adaptation of multiple protocols on the same device.

[0057] Example 2: A method for parsing serial communication frames The serial communication frame parsing method provided in this embodiment is used to identify frame boundaries of serial communication data received in byte stream mode, and includes the following steps: Step S10: Define parser parameters, which include frame header byte sequence, frame tail byte sequence, common first byte identifier, maximum frame length threshold, and frame assembly timeout threshold.

[0058] Specifically, the frame header byte sequence is used to define the start of a frame, and can be represented as HEAD[0..M-1], where M≥1, and M is the length of the frame header byte sequence; the frame tail byte sequence is used to define the end of a frame, and can be represented as TAIL[0..K-1], where K≥1, and K is the length of the frame tail byte sequence; the shared first byte identifier is used to indicate whether the first byte of the frame header byte sequence and the first byte of the frame tail byte sequence have the same byte value, and can be represented as SHARED. If the first byte of the frame header byte sequence and the first byte of the frame tail byte sequence are the same, then the shared first byte identifier is true (SHARED=true); if the first byte of the frame header byte sequence and the first byte of the frame tail byte sequence are different, then the shared first byte identifier is false (SHARED=false); the maximum frame length threshold represents the length of the byte buffer of the current frame, and also serves as the upper bound constraint of a frame, and can be represented as Lmax; the frame assembly timeout threshold represents the longest allowed duration of the frame assembly process, and can be represented as T.

[0059] To improve the correctness and reliability of frame parsing, the defined parser parameters may also include frame verification methods and their corresponding parameters. Frame verification methods include no verification, cumulative verification, and / or cyclic redundancy check.

[0060] Step S20: Based on the length M of the frame header byte sequence and the length K of the frame tail byte sequence, obtain the state set and the number of states of the state machine. The state set includes M states waiting for the i-th byte of the frame header (i = 1, ..., M), one data receiving state, and K-1 states waiting for the j-th byte of the frame tail (j = 2, ..., K), for a total of M+1 + (K-1) = M+K states.

[0061] Step S30: Define byte-by-byte parsing logic based on the state set and number of states of the state machine; and set a fault tolerance mechanism based on the maximum frame length threshold and the frame timeout threshold.

[0062] The definition of byte-by-byte parsing logic includes: Step S31: In the state of waiting for the i-th byte of the frame header, i = 1,..,M-1, if the input byte is equal to the i-th byte of the frame header, then write the byte into the buffer and transfer to the state of waiting for the (i+1)-th byte of the frame header; otherwise, remain in the current state and continue to wait.

[0063] In step S32, while waiting for the Mth byte of the frame header, if the input byte is equal to the Mth byte of the frame header, then write the byte into the buffer and transfer to the data receiving state; if the input byte is equal to the 1st byte of the frame header and the shared first byte flag configured by the parameter configuration unit 10 is true, then remain in the current state and continue waiting; if the input byte is equal to the 1st byte of the frame header and the shared first byte flag is false, then return to the waiting for the first byte of the frame header state and clear the buffer.

[0064] Step S33: In the data receiving state, input bytes are written sequentially into the buffer. When the input byte equals the j-th byte of the frame tail, j = 2, ..., K-1, wait for the (j+1)-th byte of the frame tail status. Furthermore, in this state, the frame header byte sequence is not searched; that is, the frame header byte sequence appearing in the data field does not trigger the start of a new frame. In other words, once the data receiving state is entered, only the frame tail byte sequence can terminate the current frame; the frame header byte sequence is not recognized as the start of a new frame, thus avoiding false synchronization caused by erroneously triggering new frames in the data field.

[0065] In step S34, while waiting for the Kth byte of the frame end, the input byte is written into the buffer. If the input byte is equal to the Kth byte of the frame end, it is determined that a complete data frame has been received, the complete data frame is output, and the state is cleared. If the input byte is equal to the (K-1)th byte of the frame end, and the shared first byte identifier is true, the current state is maintained and waiting continues. If the input byte is equal to the (K-1)th byte of the frame end, and the shared first byte identifier is false, the state is returned to the data reception state, and the input byte is kept in the buffer as part of the data field.

[0066] Setting up a fault tolerance mechanism includes: In step S35, when the number of bytes written to the buffer reaches the maximum frame length threshold Lmax, but a complete data frame has not yet been received, the current frame is determined to be an abnormal frame, and the execution state is cleared to zero, thereby preventing the buffer from growing unbounded. Clearing the execution state to zero means: emptying the buffer and resetting the state machine to the state of waiting for the first byte of the frame header.

[0067] Step S36: When the duration of the framing process reaches the framing timeout threshold T, but a complete data frame has not yet been received, the current frame content is forcibly discarded and the execution status is cleared to zero, thereby avoiding long-term loss of synchronization when the frame tail is lost or the link is interrupted.

[0068] If the parser parameters include the frame check method and its corresponding parameters, then step S30 further includes: Step S37: After determining that a complete data frame has been received and before outputting the complete data frame, perform a verification on the complete data frame. If the verification fails, discard the data frame to prevent erroneous data frames from being sent to the upper layer service.

[0069] Step S40: Receive data blocks of arbitrary length from serial port or network port, call byte-by-byte parsing logic to parse the data blocks of arbitrary length byte by byte, and obtain the complete data frame set by combining fault tolerance mechanism.

[0070] It should be noted that the principles and functions not described in this embodiment are the same as in Embodiment 1, and therefore will not be repeated here.

[0071] The beneficial effects of the above embodiments include at least the following: (1) Achieving universality without relying on the length field: Using frame header sequence, frame tail sequence, shared first byte identifier, maximum frame length threshold and frame timeout threshold as configuration parameters, it adapts to protocol families that do not contain length information, have pure frame tail delimitation and variable data field length, filling the gap where existing general parsing schemes fail in the face of this protocol family.

[0072] (2) Effectively ensures the correctness of parsing: By confirming the multi-byte sequence at the end of the frame, the probability of an accidental byte in the data field being misjudged as the end of the frame is reduced from 1 / 256 to the order of 1 / 65536; by sharing the first byte to maintain the waiting branch, the two types of ambiguity, namely, the frame never closing and the frame header being missed, are eliminated; by not searching the frame header within the frame, the accidental triggering of new frames in the data field is avoided. All three mechanisms are automatically enabled or disabled with the protocol under parameter control.

[0073] (3) Significantly improve the fault tolerance of serial communication: The parameterized maximum frame length threshold and framing timeout threshold replace the length field to assume the boundary protection function, which can prevent the buffer from growing without limit and exhausting memory, and can also prevent long-term loss of synchronization when the frame tail is lost or the link is interrupted; in conjunction with parameterized frame verification, it further prevents erroneous frames from being sent to the upper layer service.

[0074] (4) Failures are strictly limited to a single frame: Any anomaly is reset to the first byte of the waiting frame header by a unified state clearing, and then the search for the frame header is restarted from the next byte. Synchronization is automatically re-established in the next frame, and the failure does not spread to the next frame, thus improving the stability of the system in long-term operation.

[0075] (5) Avoids the single point of failure source of the length field: Since there is no length field, there is no failure mode that causes the frame to be misaligned and pollute all subsequent frames due to the transmission error of the length field itself.

[0076] (6) The state machine structure is automatically generated with parameters: The parser determines the number of states and transition conditions based on the configured frame header / frame tail sequence length. When changing protocols, only the protocol description needs to be changed, without modifying or recompiling the parsing logic code. The generality level is improved from the code level to the parameter level.

[0077] (7) The same parser can instantiate multiple objects simultaneously according to different protocol descriptions, adapting multiple protocols in parallel on the same device, significantly reducing the amount of code and maintenance costs in multi-protocol coexistence scenarios.

[0078] (8) The data input interface supports batch input of data blocks of arbitrary length and can correctly divide zero or one or more complete data frames in a single scan without the need for additional splicing buffers and secondary scans.

[0079] The above description is merely a preferred embodiment of the present invention and is not intended to limit the scope of the invention. Various variations can be made to the above embodiments of the present invention. That is, all simple and equivalent changes and modifications made based on the claims and description of this invention fall within the protection scope of the claims of this patent. All aspects not described in detail in this invention are conventional technical content.

Claims

1. A serial communication frame parser, characterized in that, include: The parameter configuration unit is configured to define parser parameters, which include frame header byte sequence, frame tail byte sequence, common first byte identifier, maximum frame length threshold, and frame assembly timeout threshold. The state machine generation unit is configured to obtain the state set and the number of states of the state machine based on the length of the frame header byte sequence and the length of the frame tail byte sequence; the state set includes M states waiting for the i-th byte of the frame header, one data receiving state, and K-1 states waiting for the j-th byte of the frame tail, and the number of states is M+K; where i = 1,...,M, M≥1; j = 2,...,K, K≥2; The parsing unit is configured to define byte-by-byte parsing logic based on the state set and number of states of the state machine; and to set a fault tolerance mechanism based on the maximum frame length threshold and the frame timeout threshold. The data input interface is configured to receive data blocks of arbitrary length, call the byte-by-byte parsing logic to parse the data blocks of arbitrary length byte by byte, and combine the fault tolerance mechanism to obtain a complete set of data frames.

2. The serial communication frame parser according to claim 1, characterized in that, The byte-by-byte parsing logic is defined according to the following rules: In the state of waiting for the i-th byte of the frame header, i = 1, ..., M-1, if the input byte is equal to the i-th byte of the frame header, then the input byte is written into the buffer and the state of waiting for the (i+1)-th byte of the frame header is transitioned; otherwise, the current state is maintained and the waiting continues. While waiting for the Mth byte of the frame header, if the input byte is equal to the Mth byte of the frame header, the input byte is written into the buffer and the process is switched to the data receiving state; if the input byte is equal to the 1st byte of the frame header and the shared first byte flag is true, the process remains in the current state and continues to wait; if the input byte is equal to the 1st byte of the frame header and the shared first byte flag is false, the process returns to the state of waiting for the first byte of the frame header and the buffer is cleared. In the data receiving state, the input bytes are written into the buffer sequentially. If the input byte is equal to the j-th byte of the frame end, j = 2, ..., K-1, wait for the j+1-th byte of the frame end status. While waiting for the Kth byte of the frame tail, the input byte is written to the buffer. If the input byte is equal to the Kth byte of the frame tail, it is determined that a complete data frame has been received, the complete data frame is output, and the state is cleared. If the input byte is equal to the (K-1)th byte of the frame tail, and the shared first byte flag is true, the current state is maintained and waiting continues. If the input byte is equal to the (K-1)th byte of the frame tail, and the shared first byte flag is false, the state is returned to receive data, and the input byte is retained in the buffer.

3. The serial communication frame parser according to claim 1, characterized in that, The fault tolerance mechanism is set as follows: When the number of bytes written in the buffer reaches the maximum frame length threshold, but a complete data frame has not yet been received, the current frame is determined to be an abnormal frame, the buffer is cleared, and the state machine is reset to the state of waiting for the first byte of the frame header.

4. The serial communication frame parser according to claim 1, characterized in that, The fault tolerance mechanism is also configured as follows: If the duration of the framing process reaches the framing timeout threshold, but a complete data frame has not yet been received, the current frame is discarded, the buffer is cleared, and the state machine is reset to the state of waiting for the first byte of the frame header.

5. The serial communication frame parser according to claim 1, characterized in that, The parser parameters also include frame verification methods and their corresponding parameters. The frame verification methods include one of no verification, cumulative summation, and cyclic redundancy check.

6. The serial communication frame parser according to claim 5, characterized in that, The parsing unit is also configured to: After a complete data frame has been received but before it is output, a check is performed on the complete data frame. If the check fails, the data frame is discarded.

7. A serial communication frame parsing method, characterized in that, include: Step S10: Define parser parameters, which include frame header byte sequence, frame tail byte sequence, common first byte identifier, maximum frame length threshold, and frame assembly timeout threshold. Step S20: Based on the length of the frame header byte sequence and the length of the frame tail byte sequence, obtain the state set and the number of states of the state machine. The state set includes M states waiting for the i-th byte of the frame header, one data receiving state, and K-1 states waiting for the j-th byte of the frame tail. The number of states is M+K. Where i = 1,...,M, M≥1; j = 2,...,K, K≥2. Step S30: Define byte-by-byte parsing logic based on the state set and number of states of the state machine; and set a fault tolerance mechanism based on the maximum frame length threshold and the frame timeout threshold. Step S40: Receive data blocks of arbitrary length, call the byte-by-byte parsing logic to parse the data blocks of arbitrary length byte by byte, and combine the fault tolerance mechanism to obtain a complete set of data frames.

8. The serial communication frame parsing method according to claim 7, characterized in that, The byte-by-byte parsing logic defined in step S30 includes: Step S31: In the state of waiting for the i-th byte of the frame header, i = 1,..,M-1, if the input byte is equal to the i-th byte of the frame header, then write the input byte into the buffer and transfer to the state of waiting for the (i+1)-th byte of the frame header; otherwise, remain in the current state and continue to wait. Step S32: In the state of waiting for the Mth byte of the frame header, if the input byte is equal to the Mth byte of the frame header, the input byte is written into the buffer and the process is transferred to the data receiving state; if the input byte is equal to the 1st byte of the frame header and the shared first byte flag is true, the process remains in the current state and continues to wait; if the input byte is equal to the 1st byte of the frame header and the shared first byte flag is false, the process returns to the state of waiting for the first byte of the frame header and the buffer is cleared. Step S33: In the data receiving state, write the input bytes into the buffer in sequence. If the input byte is equal to the j-th byte of the frame end, j = 2, ..., K-1, wait for the status of the (j+1)-th byte of the frame end. Step S34: While waiting for the Kth byte of the frame end, write the input byte into the buffer. If the input byte is equal to the Kth byte of the frame end, it is determined that a complete data frame has been received, the complete data frame is output, and the state is cleared. If the input byte is equal to the (K-1)th byte of the frame end, and the shared first byte identifier is true, then maintain the current state and continue waiting. If the input byte is equal to the (K-1)th byte of the frame end, and the shared first byte identifier is false, then return to the data reception state and keep the input byte in the buffer.

9. The serial communication frame parsing method according to claim 7, characterized in that, The fault tolerance mechanism set in step S30 includes: Step S35: When the number of bytes written in the buffer reaches the maximum frame length threshold, but a complete data frame has not yet been received, the current frame is determined to be an abnormal frame, the buffer is cleared, and the state machine is reset to the state of waiting for the first byte of the frame header.

10. The serial communication frame parsing method according to claim 7, characterized in that, The fault tolerance mechanism set in step S30 also includes: Step S36: When the duration of the framing process reaches the framing timeout threshold, but a complete data frame has not yet been received, the current frame is discarded, the buffer is cleared, and the state machine is reset to the state of waiting for the first byte of the frame header.

11. The serial communication frame parsing method according to claim 7, characterized in that, The parser parameters also include frame verification methods and their corresponding parameters. The frame verification methods include no verification, cumulative verification, and / or cyclic redundancy check.

12. The serial communication frame parsing method according to claim 11, characterized in that, Step S30 further includes: Step S37: After determining that a complete data frame has been received but before outputting the complete data frame, perform a verification on the complete data frame. If the verification fails, discard the data frame.