Message analysis method and system based on circular queue and state machine
By using the message analysis method of cyclic queues and state machines in industrial communication, processing data by byte byte and dynamically adjusting the analysis process, the problem of missing frames and multiple frames in traditional methods is solved, and the indefinite-length data transmission environment is realized Accuracy and stability adapt to multiple protocol types and reduce latency.
Patent Information
- Application Number
- CN202411667209.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-21
- Publication Date
- 2024-12-20
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
Traditional message analysis methods have difficulties in determining missing frames, multiple frames and frame boundaries when dealing with real-time communication and uncertain data frames, resulting in instability and inefficiency of communication protocols.
Using a message analysis method based on a loop queue and a state machine, the data is analyzed byte byte, the analysis process is dynamically adjusted, and the state machine is automatically switched to the parsing state to ensure the integrity and accuracy of the frame structure, and temporarily store it when there is insufficient data. And continue to parse.
It realizes accuracy and stability in an indefinite-length data transmission environment, avoids missing frames and multi-frame problems, improves the accuracy and flexibility of analysis, adapts to multiple protocol types, and reduces delays.
Smart Images

Figure CN119172454A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of industrial communication and embedded systems, and in particular to a message parsing method and system based on a circular queue and a state machine. Background Art
[0002] In modern industrial communications and embedded systems, message parsing is a critical part of the communication protocol. Common communication protocols, such as IEC101, IEC104, MODBUS, DLT / 645, MQTT, and custom communication protocols, are all based on frame structures for data transmission. The frame structure usually contains fields such as frame header, length, frame type, data, and checksum, so accurate parsing of each frame of data is crucial to ensure the stability and reliability of communication.
[0003] However, in traditional message parsing methods, devices usually receive data through serial ports, Ethernet and other communication methods, and the received data is temporarily stored in a cache or circular queue. When parsing begins, all data in the cache or queue is usually taken out at once, and then the data is parsed. The parsing steps of this method are usually as follows: 1. Receive data: The device saves all received data through the serial port or Ethernet into a temporary buffer or circular queue.
[0004] 2. One-time reading: During parsing, all data is read from the cache or queue at one time and processed as a whole data block.
[0005] 3. Parsing process: gradually parse out fields such as frame header, length, frame type and data content from this data block.
[0006] Although this traditional parsing method seems simple, it has many shortcomings when dealing with actual communications, especially in real-time communications or when dealing with data frames of variable length. Specific problems include: 1. Incomplete data or multiple frames: In real-time communication, the received data may be a combination of partial frames or multiple frames, and the data does not arrive neatly in frames. The traditional parsing method is to extract all the data at once for parsing, and may only obtain an incomplete frame, which will cause parsing errors or frame omissions. Similarly, if the data extracted at one time contains multiple frames of data, the traditional parsing method may have difficulty in correctly distinguishing and parsing multiple frames, resulting in multiple frame overlap or frame boundary destruction.
[0007] 2. Parsing problem without frame boundaries: The frames in many protocols are not of fixed length, and the frame boundaries need to be determined by the frame header and length field. Traditional methods may directly read the entire cache without finding a clear frame boundary, resulting in the destruction of the frame structure. If the frame header or length field is not correctly parsed in the received data, the parsing of the entire frame will fail.
[0008] 3. Frame loss or leaking: In the case of high-frequency data transmission, the cache may overflow or overwrite data, and the traditional method sometimes fails to parse the data frame in time, resulting in frame leakage. Especially in the scenario of large data volume and fast transmission speed, the mismatch between the parsing speed and the data receiving rate can easily lead to data loss.
[0009] In summary, the traditional message parsing method has limitations in processing data frames of indefinite length, cannot accurately distinguish frame boundaries, is prone to frame leakage or loss, and has low processing efficiency and cannot meet the needs of high-frequency real-time communication. These problems can directly affect the reliability and stability of the communication protocol. Summary of the invention
[0010] The purpose of the present invention is to provide a message parsing method and system based on a circular queue and a state machine, so as to solve the problems of frame leakage, multiple frames, inability to determine frame boundaries, etc. caused by reading all data at one time in traditional parsing methods.
[0011] To achieve the above object, on the one hand, the present invention provides a message parsing method based on a circular queue and a state machine, characterized in that the method comprises the following steps: Create and initialize a circular queue to store received message data, and initialize the state machine according to the protocol type; Determine whether there is data in the circular queue, and parse the data in the circular queue byte by byte using the state machine; Determine the message type based on the parsed frame header information; The parsing process is dynamically adjusted according to the message type until the complete message is parsed.
[0012] Preferably, in the message parsing method based on the circular queue and the state machine, the state machine includes a plurality of parsing states, each of the parsing states corresponds to a specific field of the message, and the state machine automatically switches to the next parsing state according to the current parsing field.
[0013] Preferably, in the message parsing method based on the circular queue and the state machine, the method also includes during the parsing process, when the received data is insufficient to form a complete frame, the system temporarily interrupts the parsing and saves the parsed data into the memory; when new data arrives, the system continues to parse from the position of the last interruption.
[0014] Preferably, in the message parsing method based on the circular queue and the state machine, the method also includes that during the parsing process, if a frame error or unclear frame boundary is detected, the system will discard the current data and restart the parsing.
[0015] Preferably, in the message parsing method based on circular queue and state machine, the protocol types include IEC101, IEC104, MODBUS, DLT / 645, MQTT and a custom communication protocol.
[0016] Preferably, in the message parsing method based on the circular queue and the state machine, the method further comprises: According to the frame structure of the protocol, the fields parsed step by step include frame header, length, frame type and data content, and only the currently available data segment is processed at any time.
[0017] In addition, in another aspect of the present invention, a message parsing system based on a circular queue and a state machine is also proposed, characterized in that the system comprises: A circular queue module, used to store received message data; The state machine module is used to parse the data in the circular queue byte by byte; A frame header parsing unit, used to determine the message type according to the parsed frame header information; The process control unit is used to dynamically adjust the parsing process according to the message type.
[0018] Preferably, in the message parsing system based on the circular queue and the state machine, the state machine module includes a plurality of parsing states, each state corresponds to a specific field of the message, and the state machine automatically switches to the next parsing state according to the current parsing field.
[0019] Preferably, in the message parsing system based on the circular queue and the state machine, the system also includes a temporary storage module for temporarily storing the parsed data when the received data is insufficient to form a complete frame during the parsing process, and continuing the parsing after receiving more data.
[0020] Compared with the prior art, the present invention has at least the following technical effects: By introducing a circular queue and a state machine, it is possible to accurately parse the message byte by byte, avoid the problems of multi-frame confusion and frame leakage in traditional parsing, and ensure accuracy and stability in an environment of variable-length data transmission. In addition, the state machine parses each protocol field in steps, and the system can process each protocol field in sequence correctly, while dynamically adjusting the parsing process according to the frame type, which can improve the accuracy and flexibility of the parsing. Moreover, byte-by-byte processing can reduce delays. The present invention has good scalability and can adapt to a variety of protocol types. BRIEF DESCRIPTION OF THE DRAWINGS
[0021] Figure 1 It is a flow chart of a message parsing method based on a circular queue and a state machine in one embodiment of the present invention; Figure 2 It is a detailed flow chart of message parsing taking IEC104 protocol as an example in an embodiment of the present invention. DETAILED DESCRIPTION
[0022] A message parsing method and system based on a circular queue and a state machine of the present invention will be described in more detail below in conjunction with a schematic diagram, wherein a preferred embodiment of the present invention is shown, and it should be understood that those skilled in the art can modify the present invention described herein while still achieving the advantageous effects of the present invention. Therefore, the following description should be understood as being widely known to those skilled in the art, and not as a limitation of the present invention.
[0023] For the sake of clarity, not all features of the actual embodiments are described. In the following description, well-known functions and structures are not described in detail because they would clutter the invention with unnecessary detail. It should be recognized that in the development of any actual embodiment, a large number of implementation details must be made to achieve the developer's specific goals, such as changing from one embodiment to another according to the limitations of the relevant system or the relevant business. In addition, it should be recognized that such development work may be complex and time-consuming, but it is just a routine task for those skilled in the art.
[0024] The present invention is described in more detail in the following paragraphs by way of example with reference to the accompanying drawings. The advantages and features of the present invention will become more apparent from the following description. It should be noted that the accompanying drawings are in very simplified form and are not in exact proportions, and are only used to facilitate and clearly assist in illustrating the purpose of the embodiments of the present invention.
[0025] As mentioned in the background technology, traditional message parsing methods have limitations in processing data frames of indefinite length, cannot accurately distinguish frame boundaries, are prone to frame omissions or frame losses, and have low processing efficiency, which cannot meet the needs of high-frequency real-time communications.
[0026] In view of this, if Figure 1As shown, the present invention provides a message parsing method based on a circular queue and a state machine, the method comprising the following steps: Create and initialize a circular queue to store received message data, and initialize the state machine according to the protocol type; Determine whether there is data in the circular queue, and parse the data in the circular queue byte by byte using the state machine; Determine the message type based on the parsed frame header information; The parsing process is dynamically adjusted according to the message type until the complete message is parsed.
[0027] Specifically, this embodiment takes the IEC104 protocol as an example for explanation. Figure 2 As shown, when the program starts, a circular queue is first created and initialized, and the circular queue is used to receive and store the upcoming message data. At the same time, the state machine is initialized according to the protocol type to be parsed. The state machine is responsible for tracking the current parsing state and contains the parsed IEC104 frame data. The parsing state is divided into: frame header, message length, control field 1, control field 2, control field 3, control field 4, type identification, variable structure qualifier, transmission reason, public address, information body according to the IEC104 message format. After initialization, the parsing state is set to the frame header.
[0028] In this embodiment, the message is first parsed. After the program is run, the parsing function is entered by polling to check whether there is data in the circular queue. If there is data in the circular queue, one byte is taken out from the circular queue each time and processed or stored in the parsed data frame structure variable according to the current parsing state. When the current state is parsed, the parsing state of the state machine will be switched to the next state until a complete EIC104 message is parsed, and then the next parsing is performed according to the frame type. The frame types include U frame, I frame and S frame.
[0029] It should be noted that: during the parsing process, the state machine uses the Switch statement (or similar control structure) to select and execute the corresponding parsing code block according to the current parsing state. The Switch statement ensures that the state machine can parse the data byte by byte according to the requirements of the message format, and automatically switch to the next parsing state after parsing the current field. For frame-based communication protocols such as the IEC104 protocol, the state machine will parse the frame header, message length, control field, type identifier, variable structure qualifier, transmission reason, public address and information body one by one according to the definition of the message format. During the parsing process, the state machine uses the Switch statement to decide whether to jump to the next parsing state based on the parsed data and the current state.
[0030] In addition, Figure 2 In the figure, Case 1 to Case 11 represent different states or conditions that the state machine may encounter when parsing the IEC104 protocol message, and each Case corresponds to a field in the message parsing process.
[0031] In this embodiment, Figure 2 The step of parsing the frame header in Case 1 is as follows: check whether there is data in the circular queue, if there is data, take out a byte to determine whether it is a frame header. If it is a frame header, store the frame header data in the IEC104 frame data parsed in the state machine, and switch the parsing state of the state machine to the parsing message length state; if it is not a frame header, discard the byte, and continue to take out the next byte from the circular queue for judgment, and repeat this cycle until the frame header is found.
[0032] In this embodiment, Figure 2 The step of parsing the message length in Case 2 is: check whether there is data in the circular queue, if there is data, take out a byte, store the length data in the IEC104 frame data parsed in the state machine, and switch the state machine parsing state to the parsing control domain 1 state.
[0033] In this embodiment, the above process is repeated to parse control domains 1 to 4, namely Case 3 to Case 6. After each control domain is parsed, the state machine switches to the parsing state of the next control domain.
[0034] Specifically, Case 3 parses control field 1: checks whether there is data in the circular queue, if there is data, takes out a byte, stores the control field 1 data into the IEC104 frame data parsed in the state machine, and switches the state machine parsing state to the parsing control field 2 state.
[0035] Case 4 Analysis of Control Domain 2: Check whether there is data in the circular queue. If there is data, take out a byte, store the control field 2 data in the IEC104 frame data parsed in the state machine, and switch the state machine parsing state to the parsing control field 3 state.
[0036] Case5 Analysis Control Domain 3: Check whether there is data in the circular queue. If there is data, take out a byte, store the control field 3 data in the IEC104 frame data parsed in the state machine, and switch the state machine parsing state to the parsing control field 4 state.
[0037] Case6 Analysis of Control Domain 4: Check whether there is data in the circular queue. If there is data, take out a byte and store the control field 4 data in the IEC104 frame data parsed in the state machine. So far, the 4-byte control field has been parsed, and the frame type (U frame, I frame, S frame) can be judged by these 4 bytes, and the current frame type is marked in the parsed IEC104 frame data. If the current frame is a U frame and an S frame, since the U frame and the S frame do not have an ASDU part, the current frame has been parsed and the next step of data frame processing can be performed, and the parsing state of the state machine is switched to the state of parsing the frame header. If the current frame is an I frame, the parsing state of the state machine is switched to the state of parsing the type identifier, and then continue to parse the variable structure qualifier, the reason for transmission, the public address and the information body.
[0038] Furthermore, Figure 2 The step of parsing the type identifier in Case 7 is: check whether there is data in the circular queue, if there is data, take out a byte, store the type identifier data in the IEC104 frame data parsed in the state machine, and switch the parsing state of the state machine to parsing variable structure qualifiers.
[0039] Furthermore, Figure 2 The step of parsing the variable structure qualifier in Case 8 is as follows: check whether there is data in the circular queue, if there is data, take out one byte, store the variable structure qualifier data in the IEC104 frame data parsed in the state machine, and switch the parsing state of the state machine to parsing the transmission reason. Since the transmission reason contains 2 bytes, the number of parsed data in the current state needs to be cleared.
[0040] Furthermore, Figure 2 The steps of parsing the transmission reason in Case 9 are as follows: check whether there is data in the circular queue, if there is data, take out a byte, store the transmission reason data in the IEC104 frame data parsed in the state machine, add 1 to the number of data parsed in the current state, and determine whether the number of data parsed in the current state is equal to 2 (because the current transmission reason is 2 bytes), if not equal to 2, continue to parse the data in the current state, when the number of data parsed in the current state is equal to 2, switch the state machine parsing state to the public address, and clear the number of data parsed in the current state.
[0041] Furthermore, Figure 2The steps of parsing the public address in Case 10 are as follows: check whether there is data in the circular queue, if there is data, take out a byte, store the public address data in the IEC104 frame data parsed in the state machine, add 1 to the number of data parsed in the current state, and determine whether the number of data parsed in the current state is equal to 2 (because the current public address is 2 bytes), if not equal to 2, continue to parse the data in the current state, when the number of data parsed in the current state is equal to 2, switch the state machine parsing state to the information body, and clear the number of data parsed in the current state.
[0042] Furthermore, Figure 2 The step of parsing the information body in Case 11 is as follows: check whether there is data in the circular queue, if there is data, take out a byte as the information body data, store the information body data in the IEC104 frame data parsed in the state machine, add 1 to the number of data parsed in the current state, and judge whether the number of data parsed in the current state is equal to the length of the information body (the length of the information body can be calculated based on the message length and the length of the data received above), if it is not equal to the length of the information body, continue to parse the data in the current state, when the number of data parsed in the current state is equal to the length of the information body, and clear the number of data parsed in the current state, at this time, a complete I frame is parsed.
[0043] In this embodiment, after parsing the complete message, the message processing function is entered, and the I frame is processed again according to the type identification of the IEC104 message protocol, including data storage and replying a confirmation frame to the master station. After the complete IEC104 message parsed is processed, the state machine parsing state is switched back to the frame header to prepare for parsing a new data frame.
[0044] It should be noted that Figure 2 The red solid arrows, blue solid arrows and blue dotted arrows clearly show the data flow and state transition process in the message parsing method based on the circular queue and state machine. These lines not only help to understand the detailed steps and processes of message parsing, but also reflect the advantages of this method in processing variable-length data frames and ensuring parsing accuracy. The following is a detailed description of all the lines: Meaning of the red solid arrow: The red solid arrow indicates the main framework of the entire message parsing, including: checking whether there is data in the circular queue, taking out a byte, the switch statement enters the state machine parsing, and continuing the previous polling operation after parsing a byte.
[0045] Meaning of blue solid arrows: The blue solid arrows represent the parsing process of each state after the state machine switches, and parse out a complete IEC104 message and complete the processing, and then start a new parsing process from the frame header.
[0046] Meaning of blue dotted arrows: Blue dotted arrows indicate the switching order and state switching of the state machine. The state machine will switch from top to bottom in case1-case11. When the current state is parsed, it will switch to the next state. When the control domain 4 state is parsed, it will be judged according to the frame type, and then further parsed state switching will be performed. It should be noted that the state switching needs to meet certain conditions, and the specific conditions are judged according to the parsing process of each state.
[0047] In addition, on the other hand of this embodiment, a message parsing system based on a circular queue and a state machine is proposed, and the system includes: a circular queue module, used to store received message data; a state machine module, used to parse the data in the circular queue byte by byte; a frame header parsing unit, used to determine the message type based on the parsed frame header information; and a process control unit, used to dynamically adjust the parsing process according to the message type.
[0048] Furthermore, the state machine module includes a plurality of parsing states, each state corresponds to a specific field of the message, and the state machine automatically switches to the next parsing state according to the current parsing field.
[0049] Furthermore, the system also includes a temporary storage module for temporarily storing the parsed data when the received data is insufficient to form a complete frame during the parsing process, and continuing the parsing after receiving more data.
[0050] In summary, in a message parsing method and system based on a circular queue and a state machine provided in an embodiment of the present invention, by introducing a circular queue and a state machine, it is possible to accurately parse the message byte by byte, avoid the problems of multi-frame confusion and frame leakage in traditional parsing, and ensure accuracy and stability in an environment of variable-length data transmission. In addition, the state machine parses each protocol field in steps, and the system can process each protocol field in sequence correctly, while dynamically adjusting the parsing process according to the frame type, which can improve the accuracy and flexibility of the parsing. Moreover, byte-by-byte processing can reduce delays. The present invention has good scalability and can adapt to a variety of protocol types.
[0051] The above is only a preferred embodiment of the present invention and does not limit the present invention in any way. Any technician in the relevant technical field, without departing from the scope of the technical solution of the present invention, makes any form of equivalent replacement or modification to the technical solution and technical content disclosed in the present invention, which does not depart from the content of the technical solution of the present invention and still falls within the protection scope of the present invention.
Claims
1. A message parsing method based on a circular queue and a state machine, characterized in that: The method comprises the following steps: Create and initialize a circular queue to store received message data, and initialize the state machine according to the protocol type; Determine whether there is data in the circular queue, and parse the data in the circular queue byte by byte using the state machine; Determine the message type based on the parsed frame header information; The parsing process is dynamically adjusted according to the message type until the complete message is parsed.
2. The method according to claim 1, characterized in that The state machine includes a plurality of parsing states, each of the parsing states corresponds to a specific field of a message, and the state machine automatically switches to a next parsing state according to a current parsing field.
3. The method according to claim 2, characterized in that The method also includes that during the parsing process, when the received data is insufficient to form a complete frame, the system temporarily interrupts the parsing and saves the parsed data into the memory; when new data arrives, the system continues to parse from the position of the last interruption.
4. The method according to claim 1, characterized in that: The method also includes that during the parsing process, if a frame error or unclear frame boundary is detected, the system will discard the current data and restart the parsing.
5. The method according to claim 1, characterized in that: The protocol types include IEC101, IEC104, MODBUS, DLT / 645, MQTT and custom communication protocols.
6. The method according to claim 1, characterized in that The method also includes parsing the fields including frame header, length, frame type and data content in steps according to the frame structure of the protocol, and only processing the currently available data segment at any time.
7. A message parsing system based on a circular queue and a state machine, characterized in that: The system comprises: A circular queue module, used to store received message data; The state machine module is used to parse the data in the circular queue byte by byte; A frame header parsing unit, used to determine the message type according to the parsed frame header information; The process control unit is used to dynamically adjust the parsing process according to the message type.
8. The message parsing system according to claim 7, characterized in that: The state machine module includes a plurality of parsing states, each state corresponds to a specific field of the message, and the state machine automatically switches to the next parsing state according to the current parsing field.
9. The message parsing system according to claim 7, characterized in that: The system also includes a temporary storage module, which is used to temporarily store the parsed data when the received data is insufficient to form a complete frame during the parsing process, and continue parsing after receiving more data.
Citation Information
Patent Citations
IP clock packet processing method, equipment and system
CN101453308A
Serial port command analysis method based on finite state machine and annular queue
CN107302531A
Message analysis method, device and equipment and computer readable storage medium
CN110381054A
Deep analysis method and system for real-time interaction protocol of source network load system
CN111163043A
IEC104 master station simulation system
CN113890830A
