A data caching and analyzing method, device and equipment suitable for underwater robots and a medium
By constructing a circular queue and a general data frame lookup and parsing function, the problem of abnormal serial port data transmission for underwater robots was solved, achieving efficient and reliable data caching and parsing, and improving the security and real-time performance of data processing.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- TIANJIN UNIV OF SCI & TECH
- Filing Date
- 2026-05-19
- Publication Date
- 2026-06-19
AI Technical Summary
When underwater robots are performing tasks, serial port data transmission is prone to problems such as byte loss, out-of-order delivery, and interference. Traditional buffer storage methods suffer from numerous data fragments, complex overflow handling, and low search and parsing efficiency. Furthermore, the data protocol formats of different sensors/communication modules vary greatly, leading to parsing errors and processing delays, which affect operational stability and data acquisition reliability.
A circular queue is constructed for basic configuration. The serial port interrupt service function responds to data reception interrupt requests. The circular queue stores the byte data of the sensor or communication module. A general data frame lookup and parsing function is constructed to perform valid length judgment, frame header matching detection, data frame format and length verification, frame tail matching detection, and data verification calculation. An independent data buffer is set up to store complete and valid data frames. Data in the independent buffer is read during parsing and processing.
It achieves fragment-free and boundary-free serial port data storage, provides a general search process to automatically filter invalid data, locate valid frames and verify data integrity, separates the parsing process from the search process, improves the security and efficiency of data processing, and enhances the real-time performance and reliability of underwater robot data processing.
Smart Images

Figure CN122248034A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of underwater robot data processing, and in particular to a data caching and parsing method, apparatus, device and medium suitable for underwater robots. Background Technology
[0002] When underwater robots perform underwater exploration and operation tasks, they need to receive data streams from various sensors (GPS, attitude sensors, compass, battery management module, etc.) and communication modules via serial ports. The underwater environment is complex, and serial data transmission is prone to problems such as byte loss, out-of-order delivery, and interference. Traditional linear buffer storage methods suffer from numerous data fragments, complex overflow handling, and low search and parsing efficiency. At the same time, the data protocol formats of different sensors / communication modules vary greatly, and general data parsing methods cannot balance parsing accuracy and real-time performance, which can easily lead to data parsing errors and processing delays, affecting the operational stability and data acquisition reliability of underwater robots.
[0003] Circular queues, as an efficient data caching structure, feature fragment-free storage and circular reuse of the head and tail, making them suitable for buffering streaming data from serial port interrupts. However, existing circular queue applications are mostly designed for single-protocol data and have the following drawbacks: (1) Traditional linear buffer storage is prone to data fragmentation, overflow, and overwriting, and cannot adapt to interrupted streaming data; (2) The data lookup and parsing are highly coupled. Directly parsing byte by byte in the receiving buffer can easily cause data corruption and access conflicts. (3) There is a lack of a unified and universal parsing framework. Each sensor requires its own parsing code, resulting in poor scalability. (4) It has weak fault tolerance and is prone to parsing dead or data loss when encountering garbled characters, interference, or packet loss. (5) The system lacks stability and real-time performance because it does not use an independent cache to isolate the lookup and parsing process.
[0004] To address the shortcomings of the existing technologies, there is an urgent need for a reliable and efficient method for underwater robot data caching and parsing, in order to solve the problems of abnormal serial port data transmission, low caching efficiency, insufficient parsing accuracy and real-time performance, and poor scalability in underwater environments. Summary of the Invention
[0005] The purpose of this application is to provide a data caching and parsing method, apparatus, device and medium suitable for underwater robots, which can improve the efficiency and reliability of serial port data caching for underwater robots.
[0006] To achieve the above objectives, this application provides the following solution: Firstly, this application provides a data caching and parsing method suitable for underwater robots, including: Construct a circular queue and perform basic configuration on the circular queue; In the serial port interrupt service function, respond to the serial port data reception interrupt request, and write the byte data received through the serial port and sent by the sensor module or communication module carried by the underwater robot into the circular queue in sequence to obtain a real-time updated buffer circular queue. Construct a general data frame lookup and parsing function; Based on the general data frame lookup and parsing function, the data cached in the real-time updated cache circular queue is sequentially subjected to effective length judgment, frame header matching detection, data frame format and length verification, frame tail matching detection, and data verification calculation to obtain a complete and valid data frame. Set up an independent data cache area; The complete and valid data frame is copied to the independent data buffer area, and the complete and valid data frame is removed from the real-time updated buffer circular queue; Read the data from the independent data cache and parse it.
[0007] Optionally, a circular queue is constructed, and the circular queue is configured in a basic manner, specifically including: Construct a circular queue containing a head pointer, a tail pointer, and a fixed-length data buffer array; the head pointer is used to indicate the starting position of valid data, the tail pointer is used to indicate the data writing position, and the data buffer array is used to store serial port data; The basic configuration of the circular queue includes: queue initialization, queue full judgment, queue empty judgment, data enqueue configuration, data dequeue configuration, effective length query configuration, and data reading configuration at a specified position.
[0008] Optionally, the queue initialization is used to configure the positions of the head pointer and tail pointer to the initial state and clear the data cache array; The queue full determination is used to determine whether the result of incrementing the tail pointer and taking the modulo is equal to the head pointer to determine whether the queue is full of data; if they are equal, the queue is determined to be full and a full status flag is returned; otherwise, a non-full status flag is returned. The queue empty check is used to determine whether the tail pointer and the head pointer are equal to confirm whether the queue is empty. If they are equal, the queue is determined to be empty and an empty status flag is returned; otherwise, a non-empty status flag is returned. The data enqueue configuration is used to determine whether the queue is not full. If the queue is not full, the data is written to the storage unit pointed to by the tail pointer, and the tail pointer is moved one position to the right according to the modulo rule. If the queue is full, the head pointer is automatically moved one position to the right, and the new data is written to the storage unit pointed to by the tail pointer, and the tail pointer is moved one position to the right. This achieves cyclic writing or discarding of new data, without inserting it into the queue again. The data dequeue configuration is used to determine whether the queue is not empty. If the queue is not empty, the data in the storage unit pointed to by the head pointer is read, and the head pointer is moved one position to the right according to the modulo rule. If the queue is empty, no data reading operation is performed, and the queue pointer state remains unchanged to achieve safe reading. The effective length query configuration is used to obtain the effective data length in the current queue by taking the modulo operation of the difference between the tail pointer, the head pointer, and the total capacity of the queue; The specified location data reading configuration is used to locate and read the corresponding valid data by combining the offset with the head pointer and modulo operation, without changing the state of the queue pointer.
[0009] Optionally, the serial port interrupt service function is used to respond to a serial port data reception interrupt request and determine whether the serial port data reception interrupt request is a serial port data reception ready interrupt; if it is a serial port data reception ready interrupt, the byte data received by the serial port and sent by the sensor module or communication module carried by the underwater robot is stored in the circular queue in real time for buffering, and then the interrupt flag is cleared and the process is exited; if it is not a serial port data reception ready interrupt, no processing is performed.
[0010] Optionally, based on the general data frame lookup and parsing function, the data cached in the real-time updated buffer circular queue is sequentially subjected to valid length judgment, frame header matching detection, data frame format and length verification, frame tail matching detection, and data verification calculation to obtain a complete and valid data frame, specifically including: Step 401: Determine the effective length of the data cached in the real-time updated cached circular queue; if the length of the real-time cached data in the current circular queue is greater than or equal to the preset minimum effective data frame length, proceed to the next step; if the length of the real-time cached data in the current circular queue is less than the preset minimum effective data frame length, determine that the current queue data is insufficient to support parsing, exit the current parsing cycle and wait for the next data enqueue. Step 402: Read the header data of the circular queue and perform frame header matching verification on the header data; if the frame header does not match, remove the current header data and return to step 401; if the frame header matches, proceed to the next step. Step 403: Read the data of a specific number of consecutive bytes after the head of the circular queue according to the protocol format, and verify the data according to the frame format. If the frame format verification fails, remove the current head data and return to step 401; if the frame format verification passes, determine whether the protocol specifies the frame length. If the frame length is not specified, proceed to step 407; if the frame length is specified, proceed to the next step. Step 404: Determine whether the frame length specified by the protocol is fixed; if the frame length is fixed, proceed to step 405; if the frame length is not fixed, proceed to step 406. Step 405: Perform frame length verification on the data length in the circular queue. If the data length in the circular queue is less than the fixed length specified in the protocol, the frame length verification fails and the process returns to step 401. If the data length in the circular queue is greater than or equal to the fixed length specified in the protocol, the frame length verification passes and the process proceeds to step 408. Step 406: Perform frame length verification on the data length in the circular queue. If the data length in the circular queue is less than the dynamic length specified by the protocol, the frame length verification fails and the process returns to step 401. If the data length in the circular queue is greater than or equal to the dynamic length specified by the protocol, the frame length verification passes and the process proceeds to step 409. Step 407: Perform frame tail matching on the data in the circular queue in order from the head to the tail of the queue. If no valid frame tail is found after traversing to the tail of the queue, exit the current parsing cycle and wait for the next parsing; if the frame tail matching is successful, proceed to step 410. Step 408: Locate the data at the corresponding position of the frame tail according to the fixed length specified in the protocol, and perform frame tail matching on the data. If the frame tail does not match, remove the current header data and return to step 401; if the frame tail matches, proceed to step 410. Step 409: Locate the data at the corresponding position of the frame tail according to the dynamic length specified in the protocol, and perform frame tail matching on the data. If the frame tail does not match, remove the current header data and return to step 401; if the frame tail matches, proceed to step 410. Step 410: Verify the complete data frame in the circular queue based on the preset verification algorithm. If the verification fails, remove the current header data and return to step 401; if the verification passes, a complete and valid data frame is obtained.
[0011] Optionally, the independent data buffer is 200 bytes in length and is used to store the complete and valid data frame.
[0012] Optionally, the data caching and parsing method for underwater robots is compatible with UART, RS232, RS485 and RS422 serial port devices and supports multi-protocol extension.
[0013] Secondly, this application provides a data caching and parsing device suitable for underwater robots, the data caching and parsing device suitable for underwater robots comprising: A circular queue construction module is used to construct a circular queue and perform basic configuration on the circular queue; The data caching module is used to respond to the serial port data reception interrupt request in the serial port interrupt service function, and write the byte data received through the serial port and sent by the sensor module or communication module carried by the underwater robot into the circular queue in sequence to obtain a real-time updated cache circular queue. A general data frame lookup and parsing function building module is used to construct general data frame lookup and parsing functions; The complete and valid data frame acquisition module is used to perform valid length judgment, frame header matching detection, data frame format and length verification, frame tail matching detection and data verification calculation on the data cached in the real-time updated cache circular queue based on the general data frame lookup and parsing function, so as to obtain a complete and valid data frame. The independent data cache setting module is used to configure an independent data cache. A complete and valid data frame processing module is used to copy the complete and valid data frame to the independent data buffer area and remove the complete and valid data frame from the real-time updated buffer circular queue; The data reading and parsing processing module is used to read and parse the data in the independent data cache.
[0014] Thirdly, this application provides a computer device, including: a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the computer program to implement the steps of the data caching and parsing method for an underwater robot as described in any one of the above.
[0015] Fourthly, this application provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the steps of the data caching and parsing method for an underwater robot as described above.
[0016] This application provides a data caching and parsing method, apparatus, device, and medium suitable for underwater robots, which has the following technical advantages: (1) By constructing a circular queue, the circular queue is configured in a basic manner; in the serial port interrupt service function, the serial port data receiving interrupt request is responded to, and the byte data received through the serial port and sent by the sensor module or communication module carried by the underwater robot is written into the circular queue in sequence to obtain a real-time updated cached circular queue, which can store serial port data safely, without fragmentation, and without going out of bounds.
[0017] (2) By constructing a general data frame lookup and parsing function, the data cached in the real-time updated cache circular queue is sequentially subjected to valid length judgment, frame header matching detection, data frame format and length verification, frame tail matching detection and data verification calculation to obtain a complete and valid data frame. A general lookup process is provided, which automatically filters invalid data, locates valid frames and verifies the integrity of the data.
[0018] (3) By setting up an independent data cache area, the complete and valid data frame is copied to the independent data cache area and removed from the real-time updated cache circular queue; the data in the independent data cache area is read and parsed, realizing the separation of lookup and parsing. The upper-layer business directly reads the data in the independent data cache area without being in the operation queue, which improves the security and efficiency of data processing. Attached Figure Description
[0019] To more clearly illustrate the technical solutions in the embodiments of this application or the prior art, the drawings used in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0020] Figure 1 This is a flowchart illustrating a data caching and parsing method for underwater robots according to an embodiment of this application; Figure 2 This is a schematic diagram of the basic configuration logic of a circular queue provided in an embodiment of this application; Figure 3 for Figure 1 The detailed flowchart illustrates the steps of performing the following on the data cached in the real-time updated buffer circular queue based on the general data frame lookup and parsing function: valid length judgment, frame header matching detection, data frame format and length verification, frame tail matching detection, and data verification calculation, to obtain a complete and valid data frame. Figure 4 This is a schematic diagram of the structure of a computer device provided in an embodiment of this application. Detailed Implementation
[0021] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0022] To make the above-mentioned objectives, features and advantages of this application more apparent and understandable, the application will be further described in detail below with reference to the accompanying drawings and specific embodiments.
[0023] In one exemplary embodiment, such as Figure 1 As shown, a data caching and parsing method suitable for underwater robots is provided. This method is executed by a computer device, specifically by a terminal or server alone, or by both a terminal and a server. In this embodiment, the data caching and parsing method for underwater robots includes steps 1 to 7. Wherein: Step 1: Construct a circular queue and perform basic configuration on the circular queue.
[0024] In this embodiment, the logic for basic configuration of the circular queue is as follows: Figure 2 As shown, the circular queue structure consists of a head pointer `top`, a tail pointer `end`, and a fixed-length data buffer array `qlist`. The basic operations of the circular queue include queue initialization (`QInit`), dequeueing (`QDelete`), and enqueueing (`QInsert`). Operations related to length and state include queue length calculation (`QLenth`) and checking if the queue is full (`QFull`). Data access operations primarily involve random data retrieval (`QItem`). Queue initialization involves the head pointer `top`, the tail pointer `end`, and the data buffer array `qlist`. Dequeueing only involves the head pointer `top`. Enqueueing only involves the tail pointer `end`. Queue length calculation involves both the head pointer `top` and the tail pointer `end`. Checking if the queue is full involves both the head pointer `top` and the tail pointer `end`. Random data retrieval involves only the data buffer array `qlist`.
[0025] Specifically, the construction of the circular queue and the basic configuration of the circular queue include: A circular queue is constructed, comprising a head pointer, a tail pointer, and a fixed-length data buffer array. The head pointer indicates the starting position of valid data, the tail pointer indicates the data writing position, and the data buffer array stores serial port data. This structure is defined as a QUEUE type, and the queue length can be modified according to actual needs. The data types of the head and tail pointers can be set to 8 bits (uint8) or 16 bits (uint16) as needed. In this embodiment, all pointer operations use the buffer length modulo operation to ensure that the pointer moves cyclically within the array range, preventing memory overflow.
[0026] The basic configuration of the circular queue includes: queue initialization, queue full judgment, queue empty judgment, data enqueue configuration, data dequeue configuration, effective length query configuration, and specified position data reading configuration; based on the basic configuration, the circular queue receives and buffers streaming byte data in the serial port interrupt service function.
[0027] Specifically, in this embodiment, queue initialization is used to configure the positions of the head and tail pointers to their initial states and clear the data cache array. The queue full check is used to determine whether the circular queue is at full capacity. The queue full check determines whether the result of incrementing the tail pointer and taking the modulo is equal to the head pointer, thus determining if the queue is full of data; if they are equal, the queue is considered full and a full status flag is returned; otherwise, a non-full status flag is returned. The queue empty check determines whether the tail pointer and head pointer are equal, confirming whether the queue is empty; if they are equal, the queue is considered empty and an empty status flag is returned; otherwise, a non-empty status flag is returned. The data enqueue configuration determines whether the queue is not full. If the queue is not full, data is written to the storage unit pointed to by the tail pointer, and the tail pointer is moved one position forward according to the modulo rule. If the queue is full, the head pointer is automatically moved one position forward (overwriting the oldest data), and new data is written to the storage unit pointed to by the tail pointer, and the tail pointer is moved one position forward, achieving cyclic writing. If the queue is full, new data can be prevented from being inserted into the queue to avoid loss of historical data. The data dequeue configuration determines whether the queue is not empty. If the queue is not empty, data is read from the storage unit pointed to by the head pointer, and the head pointer is moved one position forward according to the modulo rule. If the queue is empty, no data reading operation is performed, and the queue pointer state remains unchanged to achieve safe reading. The effective length query configuration obtains the effective data length in the current queue by performing a modulo operation on the difference between the tail pointer, the head pointer, and the total queue capacity. The specified position data reading configuration locates and reads the corresponding effective data by combining the offset with the head pointer and the modulo operation, without changing the state of the queue pointer.
[0028] More specifically, in this embodiment, queue initialization sets the head pointer and tail pointer to their initial values of 0; it iterates through all storage units of the data cache array, clearing all bytes to zero, thus emptying and resetting the queue. The dequeue configuration first checks if the queue is empty. If not, it retrieves and deletes one byte of data from the head of the queue; it then reads the data from the storage unit pointed to by the head pointer and moves the head pointer one position forward according to the modulo rule, thus dequeuing the data. The specified position data reading configuration is used to locate the corresponding data storage unit based on the input offset number combined with the head pointer and the modulo operation, directly returning the data stored in that unit. The data types of the head, tail, length, etc., can be set to 8 bits (uint8) or 16 bits (uint16) as needed.
[0029] Step 2: In the serial port interrupt service function, respond to the serial port data reception interrupt request, and write the byte data received through the serial port and sent by the sensor module or communication module carried by the underwater robot into the circular queue in sequence to obtain a real-time updated buffer circular queue.
[0030] Specifically, the serial port interrupt service function is used to respond to the serial port data reception interrupt request and determine whether the serial port data reception interrupt request is a serial port data reception ready interrupt; if it is a serial port data reception ready interrupt, the byte data received by the serial port and sent by the sensor module or communication module carried by the underwater robot is stored in the circular queue in real time for buffering, and then the interrupt flag is cleared and the process is exited; if it is not a serial port data reception ready interrupt, no processing is performed.
[0031] In this embodiment, the system constructs an interrupt service function to respond to serial port data reception interrupt requests: The function first defines a temporary byte variable to temporarily store the received data; by reading the serial port interrupt flag register, it determines whether the current interrupt is a serial port data reception ready interrupt; if it is determined to be a serial port data reception ready interrupt, it reads a single byte of data from the serial port receive buffer register into the temporary variable, and then calls the data insertion interface of the circular queue to write the byte of data into the serial port receive circular queue to complete the buffering; finally, it clears the interrupt response flag, exits the interrupt service function, and waits for the next interrupt trigger; if it is not a serial port data reception ready interrupt, no processing is performed.
[0032] More specifically, after the circular queue is configured, the underwater robot's sensor / communication module sends data through the serial port, triggering the serial port interrupt service function. The interrupt service function reads the single-byte data sent from the serial port and calls the enqueue operation of the configured circular queue to store the data into the queue in real time.
[0033] Step 3: Construct a general data frame lookup and parsing function; the general data frame lookup and parsing function is used to perform complete data frame verification, extraction and cleanup on the circular queue of continuously cached data.
[0034] Step 4: Based on the general data frame lookup and parsing function, the data cached in the real-time updated buffer circular queue is sequentially subjected to valid length judgment, frame header matching detection, data frame format and length verification, frame tail matching detection, and data verification calculation to obtain a complete and valid data frame; such as Figure 3 As shown, the specific steps include 401-410: Step 401: Determine the effective length of the data cached in the real-time updated cached circular queue; if the length of the real-time cached data in the current circular queue is greater than or equal to the preset minimum effective data frame length, proceed to the next step; if the length of the real-time cached data in the current circular queue is less than the preset minimum effective data frame length, determine that the current queue data is insufficient to support parsing, exit the current parsing cycle and wait for the next data enqueue.
[0035] In the specific implementation process, the length of valid data in the circular queue is obtained in real time, and the current length of valid data is checked repeatedly to see if it is greater than or equal to the preset minimum data frame length. If the minimum frame length is not reached, the function is exited and a flag indicating that no valid frame was found is returned.
[0036] Step 402: Read the header data of the circular queue and perform frame header matching verification on the header data; if the frame header does not match, remove the current header data and return to step 401; if the frame header matches, proceed to the next step.
[0037] Step 403: Read the data of a specific number of consecutive bytes after the head of the circular queue according to the protocol format, and verify the data according to the frame format. If the frame format verification fails, remove the current head data and return to step 401; if the frame format verification passes, determine whether the protocol specifies the frame length. If the frame length is not specified, proceed to step 407; if the frame length is specified, proceed to the next step. Step 404: Determine whether the frame length specified by the protocol is fixed; if the frame length is fixed, proceed to step 405; if the frame length is not fixed, proceed to step 406. Step 405: Perform frame length verification on the data length in the circular queue. If the data length in the circular queue is less than the fixed length specified in the protocol, the frame length verification fails and the process returns to step 401. If the data length in the circular queue is greater than or equal to the fixed length specified in the protocol, the frame length verification passes and the process proceeds to step 408. Step 406: Perform frame length verification on the data length in the circular queue. If the data length in the circular queue is less than the dynamic length specified by the protocol, the frame length verification fails and the process returns to step 401. If the data length in the circular queue is greater than or equal to the dynamic length specified by the protocol, the frame length verification passes and the process proceeds to step 409. Step 407: Perform frame tail matching on the data in the circular queue in order from the head to the tail of the queue. If no valid frame tail is found after traversing to the tail of the queue, exit the current parsing cycle and wait for the next parsing; if the frame tail matching is successful, proceed to step 410. Step 408: Locate the data at the corresponding position of the frame tail according to the fixed length specified in the protocol, and perform frame tail matching on the data. If the frame tail does not match, remove the current header data and return to step 401; if the frame tail matches, proceed to step 410. Step 409: Locate the data at the corresponding position of the frame tail according to the dynamic length specified in the protocol, and perform frame tail matching on the data. If the frame tail does not match, remove the current header data and return to step 401; if the frame tail matches, proceed to step 410. Specifically, in this embodiment, if there is a defined frame length, it is determined whether a specific byte after the frame header in the queue matches the frame tail feature; if there is no defined frame length, the protocol frame tail feature is matched byte by byte starting from the frame header until the end of the queue.
[0038] Step 410: Verify the complete data frame in the circular queue based on the preset verification algorithm. If the verification fails, remove the current header data and return to step 401; if the verification passes, a complete and valid data frame is obtained.
[0039] Specifically, in this embodiment, for protocols with verification, a verification value is calculated according to the protocol rules and compared with the verification bit to verify data integrity.
[0040] Furthermore, data verification can be used to verify whether the received data is correct. There are various verification methods, such as summation and CRC. For example, if the frame length is fixed at 10, the frame header is 0xAA, the frame trailer is 0x55, and the 9th byte is the checksum, the verification method is summation, which accumulates from the 2nd byte to the 8th byte and takes the lower 8 bits. If the frame header and frame trailer verifications pass, but the checksum is inconsistent with the sum of the 2nd to 8th bytes of the read data, it indicates that the data may have been corrupted due to interference or other factors during reception. In this case, the frame is discarded, the current header data is removed, and the loop detection restarts.
[0041] In this embodiment, frame length verification supports both fixed frame length verification and variable frame length verification.
[0042] In this embodiment, frame format verification refers to whether the data at a specific position other than the frame header and footer matches the protocol specifications. For example, the protocol specifies that the range of the fifth byte is 0x00-0x09. If it exceeds this range, the verification fails. Frame length verification has two cases: 1. Fixed frame length: If the frame length is 10, and the data length in the queue is less than 10, it needs to continue waiting. If it is greater than 10, it searches for a valid frame among these data. 2. Variable frame length: For example, the third byte represents the frame length. The length of the data in the queue is compared with the length of the third byte. If the frame length is not reached, it needs to continue waiting. If the frame length is reached, it searches for a valid frame among these data.
[0043] The "remove current header data and return to step 401" operation in steps 402, 403, 405, and 408-410 of this embodiment can automatically filter serial port garbled characters, interference data, and incomplete frames, and is highly adaptable to complex underwater communication environments.
[0044] Steps 401-410 in this embodiment have strong versatility and fault tolerance. All search functions (such as Seek_XXX) follow the standardized process of "byte-by-byte filtering → frame header verification → length verification → frame tail verification → checksum verification → extraction of valid data". The core logic and general implementation methods are shown in Table 1.
[0045] Table 1. General Data Lookup and Parsing Methods: General Data Frame Lookup and Parsing Logic and General Implementation.
[0046] Step 5: Set up an independent data buffer. The independent data buffer is 200 bytes long and is used to store the complete and valid data frame. The length of the independent data buffer in this application can be changed as needed. In this embodiment, the independent data buffer serves as the input parameter of the lookup function, used to store the complete and valid data frame extracted from the queue. After the data is stored in the buffer, the corresponding data in the queue is cleared, and the upper-layer parsing module directly reads the contents of the buffer without accessing the circular queue again, thus separating the lookup process from the parsing process and improving system stability and real-time performance.
[0047] Step 6: Copy the complete and valid data frame to the independent data buffer area, and remove the complete and valid data frame from the real-time updated buffer circular queue.
[0048] Step 7: Read the data from the independent data cache and parse it.
[0049] In this embodiment, the upper-layer business directly reads the data in the independent cache area to complete the final parsing, without accessing the circular queue. This decouples data lookup from business parsing, improving security and efficiency.
[0050] In this embodiment, the independent buffer serves as the entry parameter of the lookup function and plays the following key roles: (1) storing complete and valid data frames extracted from the queue; (2) removing the corresponding data from the queue to avoid repeated parsing; (3) the upper-level parser directly reads the independent buffer and no longer accesses the queue; (4) decoupling the lookup process from the parsing process; (5) avoiding conflicts caused by multiple modules accessing the queue simultaneously; (6) improving code readability and maintainability; and (7) enhancing system stability.
[0051] This embodiment relates to a data caching and parsing method for underwater robots that is compatible with UART, RS232, RS485, and RS422 serial port devices. It supports multiple protocol extensions and only requires modification of configuration parameters such as frame header, frame tail, length rules, verification rules, and verification range. Without changing the core lookup process and circular queue code, it can adapt to the data parsing requirements of different sensors and different communication protocols.
[0052] This embodiment discloses a data caching and parsing method suitable for underwater robots, which can be directly integrated into the embedded control system of underwater robots (such as STM32, DSP, etc.). It implements functions for initialization, enqueueing, dequeueing, and lookup / parsing of a circular queue using C language. It automatically filters garbled characters, interference, and incomplete frames, and performs modulo operations throughout, eliminating the risk of out-of-bounds errors. The queue handles storage, while an independent buffer handles output. The lookup and parsing processes are separated, enabling non-blocking lookups without affecting interrupt reception. It can be implemented purely in C language, is suitable for various embedded MCUs, adapts to the serial port data acquisition scenarios of underwater robots, is easily expandable, solves the core problem of multi-protocol data storage and parsing, significantly improves the real-time performance and reliability of underwater robot data processing, and has strong industrial application value.
[0053] Based on the same inventive concept, this application also provides a data caching and parsing device for underwater robots to implement the aforementioned data caching and parsing method for underwater robots. The solution provided by this device is similar to the solution described in the above method. Therefore, the specific limitations of one or more embodiments of the data caching and parsing device for underwater robots provided below can be found in the limitations of the data caching and parsing method for underwater robots described above, and will not be repeated here.
[0054] In one exemplary embodiment, a data caching and parsing device suitable for underwater robots is provided, comprising: a circular queue construction module, a data caching module, a general data frame lookup and parsing function construction module, a complete and valid data frame acquisition module, an independent data buffer setting module, a complete and valid data frame processing module, and a data reading and parsing processing module. Wherein: The circular queue construction module is used to construct a circular queue and perform basic configuration on the circular queue.
[0055] The data caching module is used to respond to serial port data reception interrupt requests in the serial port interrupt service function. It sequentially writes the byte data received through the serial port and sent by the sensor module or communication module carried by the underwater robot into the circular queue to obtain a real-time updated cache circular queue.
[0056] The general data frame lookup and parsing function building module is used to construct general data frame lookup and parsing functions.
[0057] The complete and valid data frame acquisition module is used to perform valid length judgment, frame header matching detection, data frame format and length verification, frame tail matching detection, and data verification calculation on the data cached in the real-time updated cache circular queue based on the general data frame lookup and parsing function, so as to obtain a complete and valid data frame.
[0058] The independent data cache setting module is used to configure an independent data cache.
[0059] The complete and valid data frame processing module is used to copy the complete and valid data frame to the independent data buffer area and remove the complete and valid data frame from the real-time updated buffer circular queue.
[0060] The data reading and parsing processing module is used to read and parse the data in the independent data cache.
[0061] In one exemplary embodiment, a computer device is provided, which may be a server or a terminal, and its internal structure diagram may be as follows. Figure 4As shown, this computer device includes a processor, memory, input / output (I / O) interfaces, and a communication interface. The processor, memory, and I / O interfaces are connected via a system bus, and the communication interface is also connected to the system bus via the I / O interfaces. The processor provides computational and control capabilities. The memory includes non-volatile storage media and internal memory. The non-volatile storage media stores the operating system, computer programs, and a database. The internal memory provides the environment for the operating system and computer programs stored in the non-volatile storage media. The database stores data for the underwater robot. The I / O interfaces are used for exchanging information between the processor and external devices. The communication interface is used for communicating with external terminals via a network. When executed by the processor, the computer program implements a data caching and parsing method suitable for underwater robots.
[0062] Those skilled in the art will understand that Figure 4 The structures shown are merely block diagrams of some structures related to the present application and do not constitute a limitation on the computer device to which the present application is applied. Specific computer devices may include more or fewer components than shown in the figures, or combine certain components, or have different component arrangements. In an exemplary embodiment, a computer device is provided, including a memory and a processor. The memory stores a computer program, and the processor executes the computer program to implement the steps in the above-described method embodiments.
[0063] In one exemplary embodiment, a computer-readable storage medium is provided storing a computer program that, when executed by a processor, implements the steps in the above-described method embodiments.
[0064] This application relates to a data caching and parsing method, apparatus, device, and medium suitable for underwater robots. It can be directly applied to embedded control systems of underwater robots and underwater detection equipment, and is compatible with various serial port devices such as UART, RS232, RS485, and RS422. It can quickly adapt to various custom protocols, sensor protocols, and communication protocols, and features low resource consumption, stable operation, strong real-time performance, and high fault tolerance, possessing significant engineering practical value. This application has at least the following technical effects or advantages: 1. Highly efficient and reliable data storage: It adopts a fixed-capacity circular queue to store serial port interrupt streaming data, with no storage fragmentation. The head and tail pointers are reused in a circular manner, which solves the problems of linear buffer overflow and fragmentation, and is adapted to the burst transmission characteristics of serial port data of underwater robots. 2. Strong multi-protocol adaptability: Designed with differentiated frame header matching, length adaptation, and verification logic to meet the protocol characteristics of various data types commonly used in underwater robots, such as GPS, attitude sensors, compasses, batteries, and communication modules, to meet the data parsing requirements of different formats; 3. High real-time parsing performance: Based on pointer offset and modulo operation of circular queue, the queue length and data position are calculated quickly. When frame header matching fails, only invalid bytes are removed without clearing the entire buffer, which greatly improves the efficiency of data lookup and parsing. 4. Accurate data verification: Combining multiple verification methods such as cumulative sum, XOR sum, and 16-bit checksum, the validity of the parsed data is guaranteed, and the parsing error rate caused by data interference in complex underwater environments is reduced; 5. Easy to implement in engineering: All operations are based on simple byte-level operations (modulo operation, accumulation, XOR), requiring no complex hardware resources. It can be directly ported to the embedded control system of underwater robots, making it highly practical for engineering applications.
[0065] Those skilled in the art will understand that all or part of the processes in the above embodiments can be implemented by a computer program instructing related hardware. The computer program can be stored in a non-volatile computer-readable storage medium. When executed, the computer program can include the processes of the embodiments described above. Any references to memory, databases, or other media used in the embodiments provided in this application can include at least one of non-volatile and volatile memory. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, optical memory, high-density embedded non-volatile memory, resistive random access memory (ReRAM), magnetic random access memory (MRAM), ferroelectric random access memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory can include random access memory (RAM) or external cache memory, etc. By way of illustration and not limitation, RAM can take many forms, such as Static Random Access Memory (SRAM) or Dynamic Random Access Memory (DRAM).
[0066] The technical features of the above embodiments can be combined in any way. For the sake of brevity, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0067] This document uses specific examples to illustrate the principles and implementation methods of this application. The descriptions of the above embodiments are only for the purpose of helping to understand the methods and core ideas of this application. Furthermore, those skilled in the art will recognize that, based on the ideas of this application, there will be changes in the specific implementation methods and application scope. Therefore, the content of this specification should not be construed as a limitation of this application.
Claims
1. A data caching and parsing method suitable for underwater robots, characterized in that, The data caching and parsing method applicable to underwater robots includes: Construct a circular queue and perform basic configuration on the circular queue; In the serial port interrupt service function, respond to the serial port data reception interrupt request, and write the byte data received through the serial port and sent by the sensor module or communication module carried by the underwater robot into the circular queue in sequence to obtain a real-time updated buffer circular queue. Construct a general data frame lookup and parsing function; Based on the general data frame lookup and parsing function, the data cached in the real-time updated cache circular queue is sequentially subjected to effective length judgment, frame header matching detection, data frame format and length verification, frame tail matching detection, and data verification calculation to obtain a complete and valid data frame. Set up an independent data cache area; The complete and valid data frame is copied to the independent data buffer area, and the complete and valid data frame is removed from the real-time updated buffer circular queue; Read the data from the independent data cache and parse it.
2. The data caching and parsing method for underwater robots according to claim 1, characterized in that, The construction of the circular queue, including basic configuration of the circular queue, specifically includes: Construct a circular queue containing a head pointer and a tail pointer, and a fixed-length data buffer array; the head pointer is used to indicate the starting position of valid data, the tail pointer is used to indicate the data writing position, and the data buffer array is used to store serial port data; The basic configuration of the circular queue includes: queue initialization, queue full judgment, queue empty judgment, data enqueue configuration, data dequeue configuration, effective length query configuration, and data reading configuration at a specified position.
3. The data caching and parsing method for underwater robots according to claim 2, characterized in that, The queue initialization is used to configure the positions of the head pointer and tail pointer to the initial state and clear the data cache array; The queue full determination is used to determine whether the result of incrementing the tail pointer and taking the modulo is equal to the head pointer to determine whether the queue is full of data; if they are equal, the queue is determined to be full and a full status flag is returned; otherwise, a non-full status flag is returned. The queue empty check is used to determine whether the tail pointer and the head pointer are equal, and to confirm whether the queue is empty. If they are equal, the queue is determined to be empty and an empty status flag is returned; otherwise, a non-empty status flag is returned. The data enqueue configuration is used to determine whether the queue is not full. If the queue is not full, the data is written to the storage unit pointed to by the tail pointer, and the tail pointer is moved one position to the right according to the modulo rule. If the queue is full, the head pointer is automatically moved one position to the right, and the new data is written to the storage unit pointed to by the tail pointer, and the tail pointer is moved one position to the right. This achieves cyclic writing or discarding of new data, without inserting it into the queue again. The data dequeue configuration is used to determine whether the queue is not empty. If the queue is not empty, the data in the storage unit pointed to by the head pointer is read, and the head pointer is moved one position to the right according to the modulo rule. If the queue is empty, no data reading operation is performed, and the queue pointer state remains unchanged to achieve safe reading. The effective length query configuration is used to obtain the effective data length in the current queue by taking the modulo operation of the difference between the tail pointer, the head pointer, and the total capacity of the queue; The specified location data reading configuration is used to locate and read the corresponding valid data by combining the offset with the head pointer and modulo operation, without changing the state of the queue pointer.
4. The data caching and parsing method for underwater robots according to claim 1, characterized in that, The serial port interrupt service function is used to respond to serial port data reception interrupt requests and determine whether the serial port data reception interrupt request is a serial port data reception ready interrupt. If it is a serial port data reception ready interrupt, the byte data received by the serial port and sent by the sensor module or communication module carried by the underwater robot is stored in the circular queue in real time for buffering, and then the interrupt flag is cleared and the process is exited. If it is not a serial port data reception ready interrupt, no processing is performed.
5. The data caching and parsing method for underwater robots according to claim 1, characterized in that, Based on the general data frame lookup and parsing function, the data cached in the real-time updated buffer circular queue is sequentially subjected to valid length judgment, frame header matching detection, data frame format and length verification, frame tail matching detection, and data verification calculation to obtain a complete and valid data frame, specifically including: Step 401: Determine the effective length of the data cached in the real-time updated cached circular queue; if the length of the real-time cached data in the current circular queue is greater than or equal to the preset minimum effective data frame length, proceed to the next step; if the length of the real-time cached data in the current circular queue is less than the preset minimum effective data frame length, determine that the current queue data is insufficient to support parsing, exit the current parsing cycle and wait for the next data enqueue. Step 402: Read the header data of the circular queue and perform frame header matching verification on the header data; if the frame header does not match, remove the current header data and return to step 401; if the frame header matches, proceed to the next step. Step 403: Read the data of a specific number of consecutive bytes after the head of the circular queue according to the protocol format, and verify the data according to the frame format. If the frame format verification fails, remove the current head data and return to step 401; if the frame format verification passes, determine whether the protocol specifies the frame length. If the frame length is not specified, proceed to step 407; if the frame length is specified, proceed to the next step. Step 404: Determine whether the frame length specified by the protocol is fixed; if the frame length is fixed, proceed to step 405; if the frame length is not fixed, proceed to step 406. Step 405: Perform frame length verification on the data length in the circular queue. If the data length in the circular queue is less than the fixed length specified in the protocol, the frame length verification fails and the process returns to step 401. If the data length in the circular queue is greater than or equal to the fixed length specified in the protocol, the frame length verification passes and the process proceeds to step 408. Step 406: Perform frame length verification on the data length in the circular queue. If the data length in the circular queue is less than the dynamic length specified by the protocol, the frame length verification fails and the process returns to step 401. If the data length in the circular queue is greater than or equal to the dynamic length specified by the protocol, the frame length verification passes and the process proceeds to step 409. Step 407: Perform frame tail matching on the data in the circular queue in order from the head to the tail of the queue. If no valid frame tail is found after traversing to the tail of the queue, exit the current parsing cycle and wait for the next parsing; if the frame tail matching is successful, proceed to step 410. Step 408: Locate the data at the corresponding position of the frame tail according to the fixed length specified in the protocol, and perform frame tail matching on the data. If the frame tail does not match, remove the current header data and return to step 401; if the frame tail matches, proceed to step 410. Step 409: Locate the data at the corresponding position of the frame tail according to the dynamic length specified in the protocol, and perform frame tail matching on the data. If the frame tail does not match, remove the current header data and return to step 401; if the frame tail matches, proceed to step 410. Step 410: Verify the complete data frame in the circular queue based on the preset verification algorithm. If the verification fails, remove the current header data and return to step 401; if the verification passes, a complete and valid data frame is obtained.
6. The data caching and parsing method for underwater robots according to claim 1, characterized in that, The independent data buffer is 200 bytes in length and is used to store the complete and valid data frame.
7. The data caching and parsing method for underwater robots according to claim 1, characterized in that, The data caching and parsing method for underwater robots is compatible with UART, RS232, RS485 and RS422 serial port devices and supports multiple protocol extensions.
8. A data caching and parsing device suitable for underwater robots, characterized in that, The data caching and parsing device for underwater robots includes: A circular queue construction module is used to construct a circular queue and perform basic configuration on the circular queue; The data caching module is used to respond to the serial port data reception interrupt request in the serial port interrupt service function, and write the byte data received through the serial port and sent by the sensor module or communication module carried by the underwater robot into the circular queue in sequence to obtain a real-time updated cache circular queue. A general data frame lookup and parsing function building module is used to construct general data frame lookup and parsing functions; The complete and valid data frame acquisition module is used to perform valid length judgment, frame header matching detection, data frame format and length verification, frame tail matching detection and data verification calculation on the data cached in the real-time updated cache circular queue based on the general data frame lookup and parsing function, so as to obtain a complete and valid data frame. The independent data cache setting module is used to configure an independent data cache. A complete and valid data frame processing module is used to copy the complete and valid data frame to the independent data buffer area and remove the complete and valid data frame from the real-time updated buffer circular queue; The data reading and parsing processing module is used to read and parse the data in the independent data cache.
9. A computer device, comprising: A memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that the processor executes the computer program to implement the data caching and parsing method for an underwater robot as described in any one of claims 1-7.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that, When executed by a processor, the computer program implements the data caching and parsing method for underwater robots as described in any one of claims 1-7.