Data transmission method and data transmission device for environmental monitoring system

By sending the next data packet immediately after transmitting one data packet at a time in the environmental monitoring system, and by managing the port status identifier in a unified manner, the problem of data loss is solved, data transmission efficiency and bandwidth utilization are improved, and rapid recovery capability is achieved.

CN123068340APending Publication Date: 2026-11-10BAO DING SHI TIAN HE DIAN ZI JI SHU YOU XIAN GONG SI
View PDF 0 Cites 0 Cited by

Patent Information

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

AI Technical Summary

Technical Problem

In environmental monitoring systems, data loss can occur in the main control loop of a universal serial bus due to the polling mode during the data transmission phase. In particular, when waiting for the next data transmission phase, the data in the buffer may be overwritten, resulting in the loss of unsent data.

Method used

By triggering a transmission completion interrupt after each data packet is transmitted through the serial port, the next data packet is immediately sent until the preset conditions are met, thus avoiding data overwriting. Furthermore, a unified function manages the port status flags, ensuring the orderly and complete transmission of data.

Benefits of technology

It effectively reduces buffer data loss, improves data transmission efficiency and bandwidth utilization, avoids data loss and state chaos, and ensures rapid recovery capability in abnormal situations.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN123068340A_ABST
    Figure CN123068340A_ABST
Patent Text Reader

Abstract

This application provides a data transmission method and device for an environmental monitoring system, relating to the field of data processing technology. The data transmission method includes: in the current control loop of a universal serial bus device, in response to reaching the data transmission phase of the current control loop, detecting the data to be transmitted in the buffer; if the amount of data to be transmitted is not zero, transmitting a data packet from the buffer through a serial port until the serial port triggers a transmission completion interrupt; in response to the transmission completion interrupt, detecting the remaining data to be transmitted in the buffer; if the amount of remaining data to be transmitted is not zero, transmitting a data packet from the buffer through the serial port until the serial port triggers a transmission completion interrupt; and completing the current data transmission phase if the data transmission process in the current data transmission phase meets preset conditions. The method provided by this application can effectively reduce data loss in the buffer.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of data processing technology, and in particular to a data transmission method and data transmission device for an environmental monitoring system. Background Technology

[0002] In environmental monitoring systems, a universal serial bus (USB) is typically used for data transmission. USB data transmission usually employs a main control loop polling mode. This means that in the main control loop, the channel polling function checks the serial port's transmission completion flag. When the flag is 1, the data transmission phase begins, and data is sent through the serial port. The main loop then proceeds to other control phases. Even after a data packet is sent, although the transmission completion flag can be set to 1, the next data packet cannot be triggered until the main control loop polls the channel again and enters the data transmission phase.

[0003] However, the main control loop of the Universal Serial Bus (USB) includes not only data transmission phases but also operations such as digital-to-analog converter data acquisition, remote signaling processing, and telemetry. While waiting for the next data transmission phase, the buffer of the USB device is continuously being written to, potentially overwriting unsent data and causing data loss. In other words, only one data packet can be sent in each data transmission phase of the main control loop. While waiting for the next data packet, the writing of new data may overwrite unsent data, leading to data loss. Summary of the Invention

[0004] This application provides a data transmission method and data transmission device for an environmental monitoring system to solve the technical problem of data loss caused by sending data through a main control loop polling method in the prior art.

[0005] In a first aspect, this application provides a data transmission method for an environmental monitoring system. The environmental monitoring system includes a universal serial bus device, which includes at least one serial port and its corresponding buffer. The method includes: in the current control loop of the universal serial bus device, in response to the data transmission phase of the current control loop, detecting the data to be transmitted in the buffer; if the amount of data to be transmitted is not zero, transmitting a data packet from the buffer through the serial port until the serial port triggers a transmission completion interrupt, the data packet including a preset number of bytes of data to be transmitted; in response to the transmission completion interrupt, detecting the remaining data to be transmitted in the buffer; if the amount of the remaining data to be transmitted is not zero, transmitting a data packet from the buffer through the serial port until the serial port triggers a transmission completion interrupt; and completing the current data transmission phase if the data transmission process of the current data transmission phase meets preset conditions.

[0006] Based on this scheme, for the data transmission process of a universal serial bus device, in a control loop, when the data transmission stage is executed, after transmitting a data packet through the serial port, the serial port triggers a transmission completion interrupt. At this time, in response to the transmission completion interrupt, the next data packet is immediately sent until the data transmission process of the current data transmission stage meets the preset conditions, then the current data transmission stage is completed and the next control stage of the current control loop is executed.

[0007] In other words, during a data transmission phase, the serial port triggers a transmit completion interrupt after transmitting one data packet. In response to this interrupt, the next data packet is immediately sent. Once the next data packet is transmitted, another transmit completion interrupt is triggered, thus enabling batch transmission of multiple data packets. This allows for the transmission of as much buffered data as possible within a single control loop, preventing the overwriting of unsent data by new data writes and effectively reducing data loss in the buffer.

[0008] In some feasible embodiments, transmitting a data packet in the serial port buffer includes: calling a port status read function to read the transmit monitoring flag and transmit completion flag of the serial port; if the transmit monitoring flag is 1 and the transmit completion flag is 1, selecting a preset number of bytes of data to be transmitted from the current read position of the buffer's read pointer to form a data packet; copying the data packet to the transmit buffer of the Universal Serial Bus device; calling a transmit start function to set both the transmit monitoring flag and the transmit completion flag to 0; and calling the data transmission driver of the Universal Serial Bus device to transmit the data packet.

[0009] Based on this scheme, the port status reading function uniformly reads the port's transmit monitoring flag and transmit completion flag, and determines whether to execute the data transmission operation based on the flag status. The transmit monitoring flag and transmit completion flag are managed by a unified transmit start function, avoiding the status chaos and communication deadlock risks caused by directly manipulating flag bits in independent files of each channel, and realizing standardized management of flag bits.

[0010] In some feasible embodiments, before calling the port status reading function to read the serial port's transmit monitoring flag and transmit completion flag, the method further includes: obtaining the maximum number of bytes N that the serial port can carry in a single data transmission; and determining N-1 as the preset number of bytes.

[0011] Based on this scheme, by constraining the length of a single data packet to the maximum number of bytes (Maximum Packet Size, MPS) that the serial port can carry in a single transmission, minus one byte, the length of any data packet is not an integer multiple of MPS. This avoids the generation of zero-length packets (ZLP). The host computer receiving the data can correctly determine the data boundary without ZLP, thereby reducing the overhead of ZLP-related protocols to zero and significantly improving the effective bandwidth utilization.

[0012] In some feasible embodiments, if the sending monitoring flag is 1 and the sending completion flag is 1, before selecting a preset number of bytes of data to be transmitted from the current reading position of the read pointer in the buffer to form a data packet, the method further includes: saving the current reading position of the read pointer; calling the data transmission driver of the universal serial bus device to transmit the data packet; after that, the method further includes: if the data packet transmission fails, retrieving the saved current reading position; and setting the read pointer to the current reading position.

[0013] Based on this scheme, a non-blocking transmission mechanism is achieved by saving the current read position of the read pointer before transmission and restoring the read pointer to that position when data packet transmission fails. That is, when the serial port is busy, meaning the previous data packet has not yet been completely sent, the data is not discarded, the read pointer remains in its original position, and the next transmission attempt continues from the same position, avoiding data loss.

[0014] In some feasible embodiments, after calling the port status reading function to read the transmit monitoring flag and transmit completion flag of the serial port, the method further includes: if the transmit monitoring flag is 0 or the transmit completion flag is 0, keeping the read position of the read pointer at the current read position until the serial port triggers a transmit completion interrupt.

[0015] Based on this scheme, when the transmission monitoring flag or transmission completion flag is detected to be 0, meaning the serial port cannot currently send new data packets, the read pointer is kept at its current position, waiting for the serial port to trigger a transmission completion interrupt, which means waiting for the serial port to attempt to send data again. This further ensures that data is not discarded when the serial port is busy, avoiding data loss.

[0016] In some feasible embodiments, after calling the send start function and setting both the send monitoring flag and the send completion flag to 0, the method further includes: in response to the send monitoring flag switching from 1 to 0, using a timer variable preset on the serial port port to record the switching timestamp of the serial port port; recording the time difference between the current time and the switching timestamp; if the time difference is greater than or equal to the time difference threshold, calling the timeout recovery function to switch the send monitoring flag from 0 to 1; and setting the send completion flag to 1.

[0017] Based on this scheme, the time difference between the current time and the switching timestamp is recorded. When the time difference is greater than or equal to the time difference threshold, the sending monitoring flag is switched from 0 to 1, and the sending completion flag is set to 1. This forces the serial port to return to its usability through a unified timeout recovery function. Under the premise that normal communication is unaffected, this ensures rapid recovery capability in abnormal situations, keeping the data loss window within a very short time.

[0018] In some feasible embodiments, the current data transmission phase is completed when the data transmission process in the current data transmission phase meets preset conditions. This also includes completing the current data transmission phase when the amount of data remaining to be transmitted is 0.

[0019] Based on this scheme, the current data transmission phase can be completed when all the data to be transmitted in the buffer has been sent, avoiding unnecessary waiting and improving data transmission efficiency.

[0020] In some feasible embodiments, in response to a transmission completion interrupt, detecting the remaining data to be transmitted in the buffer includes: in response to a transmission completion interrupt, reading the current write position of the serial port's write pointer and the current read position of the serial port's read pointer; calling a buffer data volume calculation function to calculate the effective data volume between the current write position and the current read position; if the effective data volume is not 0, then determining that the remaining data volume to be transmitted is not 0.

[0021] Based on this scheme, by reading the current write position of the serial port's write pointer and the current read position of the serial port's read pointer, the effective data volume between the current write position and the current read position can be calculated, thereby determining the effective data volume in the buffer and thus determining the remaining data to be transmitted in the buffer.

[0022] In some feasible embodiments, before detecting the data to be transmitted in the buffer, the method further includes: establishing a critical section period by disabling the global interrupt protection of the universal serial bus device, the critical section period starting from the current write position of the write pointer of the serial port and ending after the data transmission phase of the current control loop is completed.

[0023] Based on this scheme, by disabling global interrupt protection, a critical section period is established. The critical section period starts from the current write position of the write pointer of the serial port and ends after the data transmission phase of the current control loop is completed. In this way, the data packet transmission process driven by the transmission completion interrupt is within the critical section period throughout, thereby avoiding mutual interference between the data packet transmission process driven by the transmission completion interrupt and the data transmission process driven by the data transmission phase of the current control loop.

[0024] Secondly, this application also provides a data transmission device for an environmental monitoring system. The environmental monitoring system includes a universal serial bus device, which includes at least one serial port and its corresponding buffer. The data transmission device includes: a detection unit, used to detect the data to be transmitted in the buffer in response to the data transmission stage of the current control loop of the universal serial bus device; a sending unit, used to transmit a data packet in the buffer through the serial port when the amount of data to be transmitted is not zero, until the serial port triggers a transmission completion interrupt, wherein the data packet includes a preset number of bytes of data to be transmitted; the detection unit is also used to detect the remaining data to be transmitted in the buffer in response to the transmission completion interrupt; the sending unit is also used to transmit a data packet in the buffer through the serial port when the amount of remaining data to be transmitted is not zero, until the serial port triggers a transmission completion interrupt; and an execution unit, used to complete the current data transmission stage when the data transmission process of the current data transmission stage meets preset conditions.

[0025] As can be seen from the above technical solution, the data transmission method of the environmental monitoring system provided in this application, in a data transmission phase, triggers a transmission completion interrupt after transmitting one data packet through the serial port. In response to the transmission completion interrupt, the next data packet can be sent immediately. After the next data packet is transmitted, the transmission completion interrupt is triggered again, thereby realizing the batch transmission of multiple data packets. In this way, as much data in the buffer as possible can be sent out in a control loop, avoiding the overwriting of unsent data due to the writing of new data, thus effectively reducing the loss of data in the buffer. Attached Figure Description

[0026] To more clearly illustrate the technical solution of this application, the drawings used in the embodiments will be briefly introduced below. Obviously, for those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0027] Figure 1 A schematic flowchart illustrating a data transmission method for an environmental monitoring system according to an embodiment of this application; Figure 2A flowchart illustrating a data transmission method for an environmental monitoring system, provided as another embodiment of this application; Figure 3 A schematic flowchart illustrating a data transmission method for an environmental monitoring system, provided in yet another embodiment of this application; Figure 4 A schematic flowchart illustrating a data transmission method for an environmental monitoring system, provided in yet another embodiment of this application; Figure 5 A schematic flowchart illustrating a data transmission method for an environmental monitoring system, as provided in another embodiment of this application; Figure 6 A schematic flowchart illustrating a data transmission method for an environmental monitoring system, provided in yet another embodiment of this application; Figure 7 A schematic flowchart illustrating a data transmission method for an environmental monitoring system, as provided in another embodiment of this application; Figure 8 A schematic flowchart illustrating a data transmission method for an environmental monitoring system, provided in yet another embodiment of this application; Figure 9 A schematic flowchart illustrating a data transmission method for an environmental monitoring system, provided in yet another embodiment of this application; Figure 10 This is a schematic diagram of the structure of a data transmission device for an environmental monitoring system provided in an embodiment of this application. Detailed Implementation

[0028] The embodiments will now be described in detail, examples of which are illustrated in the accompanying drawings. When the following description relates to the drawings, unless otherwise indicated, the same numbers in different drawings represent the same or similar elements. The embodiments described in the following examples do not represent all embodiments consistent with this application.

[0029] In field supervision units (FSUs), data transmission is typically achieved using a Universal Serial Bus (USB). USB devices generally employ a main control loop polling mode for data transmission. This means that in the main control loop, the channel polling function checks the serial port's transmission completion flag. When the flag is 1, the data transmission phase begins, and data is sent through the serial port. The main loop then proceeds to other control phases. Even after a data packet is transmitted, although the transmission completion flag can be set to 1, the next data packet cannot be triggered until the main control loop polls the channel again and enters the data transmission phase.

[0030] In related technologies, USB data transmission employs a main loop polling mode. After serial data is received into the circular buffer of the serial port via Direct Memory Access (DMA), the main loop checks the serial port's transmission completion flag using a channel polling function. Only when this flag is 1 can the next data packet be sent. After the previous packet is sent, the serial port triggers an interrupt, setting the transmission completion flag to 1. The next packet can only be sent after the main loop polls the channel. However, the main loop includes time-consuming operations such as digital-to-analog converter data acquisition, remote signaling processing, and telemetry. This can cause the next data packet to be delayed even after the previous packet has been sent. During this waiting window, the DMA continuously writes new serial data into the circular buffer, potentially overwriting older data that has not yet been sent, resulting in data loss.

[0031] Furthermore, when checking the serial port's transmission completion flag through the channel polling processing function in the main loop, if the flag is 0, meaning the previous data packet on the serial port has not been sent, the data read in the current main loop will be skipped directly, and the read position of the circular buffer's read pointer will not be saved. In this case, if the DMA continues to overwrite the circular buffer with new data, the skipped data will be lost.

[0032] In some related technologies, the underlying data transfer driver of USB devices lacks a fast recovery mechanism for abnormal USB transmission states. If an anomaly such as a lost serial port transmission completion interruption prevents the transmission completion flag from ever being set to 1, the system needs to wait a considerable amount of time before recovering through a timeout mechanism. During the waiting window before recovery, all newly arriving serial port data is discarded.

[0033] In some related technologies, the serial port's transmit completion flag and transmit monitoring flag are directly manipulated in independent files for each channel, lacking standardized management through a unified interface function. This can easily lead to state confusion and the risk of communication deadlock. Understandably, the transmit completion flag indicates whether the previous data packet on the serial port has been successfully transmitted. If the transmit completion flag is 0, it means the previous data packet was not successfully transmitted; if it is 1, it means the previous data packet has been successfully transmitted. The transmit monitoring flag indicates whether the serial port is currently in a data transmission state. If the transmit monitoring flag is 0, it means the serial port is currently transmitting data; if it is 1, it means the serial port is currently in an idle state.

[0034] Furthermore, the applicant discovered that the aforementioned packet loss problem is constrained by critical hardware resources, making conventional optimization approaches ineffective. For example, the APM32E103 chip's USB peripherals only provide a 512-byte dedicated data buffer, which must be shared by all endpoints of all serial ports virtually created by the USB.

[0035] For example, in FSU, the USB CDC ACM (Universal Serial Bus Communication Device Class Abstract Control Model) composite device solution is used to virtualize five CDC serial ports from a single USB physical interface. This means that with a USB device configured with five serial ports, each containing one IN endpoint and one OUT endpoint, for a total of ten data endpoints, buffer space must be allocated from a 512-byte buffer. With the 512-byte resource evenly distributed among the ten endpoints, the maximum packet size (MPS) for each endpoint is forced to be set to 32 bytes, far below the standard upper limit of 64 bytes for a BULK endpoint on a full-speed USB device. According to the USB CDC ACM protocol specification, one byte in each data packet is used for protocol control fields; therefore, the effective data payload is capped at 31 bytes, and the single-packet transmission capacity is hardware-locked at an extremely low level. This hardware constraint further highlights the problems of the existing polling mode: under the low capacity of 32 bytes / packet, any data backlog caused by polling delays is more likely to lead to a circular buffer overflow, significantly amplifying the risk of packet loss.

[0036] To address the aforementioned problems, this application provides a data transmission method for an environmental monitoring system. The environmental monitoring system includes a universal serial bus device, which includes at least one serial port and its corresponding buffer. Figure 1 This is a flowchart illustrating a data transmission method for an environmental monitoring system according to an embodiment of this application.

[0037] like Figure 1 As shown, the data transmission method of the environmental monitoring system provided in this application includes the following steps S100 to S500.

[0038] S100: In the current control loop of a Universal Serial Bus device, in response to the data transmission phase of the current control loop, detect the data to be transmitted in the buffer.

[0039] In step S100, in the current control loop of the universal serial bus device, that is, in the main loop, when the main loop executes to the data transmission stage of the current control loop for the serial port, the data to be transmitted in the buffer is first detected.

[0040] The data to be transmitted in the buffer can be detected by reading the write and read pointers of the buffer. The buffer typically uses a circular buffer data structure, and the DMA writes the serial port data to be transmitted into this circular buffer in a circular pattern. The system can determine the total amount of data waiting to be transmitted in the current buffer by calculating the effective data volume between the write and read pointers. For example, at the beginning of each data transmission phase, the system calculates the available data volume in the buffer corresponding to each serial port using a serial port polling function. If there is data to be transmitted in the buffer of a certain serial port, the data transmission process for that serial port is entered.

[0041] S200: When the amount of data to be transmitted is not zero, transmit a data packet in the buffer through the serial port until the serial port triggers a transmission completion interrupt.

[0042] The data packet includes a preset number of bytes of data to be transmitted.

[0043] If the calculated amount of data to be transmitted in the buffer is 0, it means there is no data to be sent, so the sending operation is not performed directly; if the amount of data to be transmitted is not 0, the data packet sending process is executed.

[0044] In step S200, a preset number of bytes are selected from the data to be transmitted in the buffer to form a data packet, and the data packet is transmitted through the corresponding serial port. After the data packet is transmitted, the device continues to execute other processing logic and does not block and wait for the transmission to finish; when the serial port completes the transmission of the data packet, a transmission completion interrupt is triggered, which serves as an event marker indicating that the data transmission of this packet is complete.

[0045] The preset number of bytes is the pre-defined data length carried by a single data packet, used to limit the size of the data retrieved from the buffer for a single data packet.

[0046] S300: In response to a transmission completion interrupt, detect the remaining data to be transmitted in the buffer.

[0047] In step S300, after the serial port completes the data packet transmission in the previous step and generates a transmission completion interrupt, the device responds to the interrupt event and performs a second check on the buffer to check the remaining data to be transmitted inside the buffer.

[0048] It's understandable that the remaining data to be transmitted refers to the portion of data in the buffer that hasn't been sent after the previous data packet was retrieved and transmitted. This detection action is triggered in response to a transmission completion interrupt, rather than waiting for the control loop to re-polulate and enter the data transmission phase. In other words, the interrupt event of data packet transmission completion directly drives this buffer check, without waiting for the control loop's timing. This check determines whether there is still data remaining in the buffer to be sent, providing a basis for deciding whether to continue sending the next data packet.

[0049] S400: When the amount of data remaining to be transmitted is not zero, transmit a data packet in the buffer through the serial port until the serial port triggers a transmission completion interrupt.

[0050] In step S400, after checking the remaining data to be transmitted in the buffer, if the amount of remaining data to be transmitted is not zero, it means that there is still data to be sent in the buffer. Then, another data packet is transmitted through the serial port. This data packet is also taken from the data to be transmitted in the buffer, and the data packet also contains a preset number of bytes of data to be transmitted.

[0051] After initiating the data packet transmission, it continues to wait for the serial port hardware transmission to complete and trigger the transmission completion interrupt again. In other words, after each data packet transmission is completed and a transmission completion interrupt is triggered, the remaining data in the buffer is checked; if there is still data to be transmitted, the next data packet is sent. This transmission process is driven by the transmission completion interrupt and is not directly constrained by the control loop polling rhythm.

[0052] S500: If the data transmission process in the current data transmission phase meets the preset conditions, the current data transmission phase is completed.

[0053] In step S500, during the entire data transmission process of the current data transmission phase initiated by the control loop, it is continuously determined whether the preset conditions are met. Once the data transmission process meets the preset conditions, the current data transmission phase ends and exits, and the control loop continues to execute other subsequent processing phases.

[0054] In this embodiment of the application, for the data transmission process of a universal serial bus device, in a control loop, when the data transmission stage is executed, after transmitting a data packet through the serial port, the serial port triggers a transmission completion interrupt. At this time, in response to the transmission completion interrupt, the next data packet is immediately sent until the data transmission process of the current data transmission stage meets the preset conditions, then the current data transmission stage is completed and the next control stage of the current control loop is executed.

[0055] In other words, during a data transmission phase, the serial port triggers a transmit completion interrupt after transmitting one data packet. In response to this interrupt, the next data packet is immediately sent. Once the next data packet is transmitted, another transmit completion interrupt is triggered, thus enabling batch transmission of multiple data packets. This allows for the transmission of as much buffered data as possible within a single control loop, preventing the overwriting of unsent data by new data writes and effectively reducing data loss in the buffer.

[0056] Figure 2 This is a flowchart illustrating a data transmission method for an environmental monitoring system, provided as another embodiment of this application.

[0057] like Figure 2 As shown, in step S200: transmitting a data packet in the buffer through the serial port port may include the following steps S210 to S250.

[0058] S210: Call the port status read function to read the serial port's transmit monitoring flag and transmit completion flag.

[0059] In step S210, when performing single data packet transmission processing, the port status reading function is first called to obtain the current values ​​of the transmission monitoring flag and the transmission completion flag corresponding to the serial port port.

[0060] The "Send Complete" flag indicates whether the previous data packet on the serial port has been successfully sent. A value of 0 indicates that the previous data packet was not successfully sent, while a value of 1 indicates that the previous data packet has been successfully sent. The "Send Monitoring" flag indicates whether the serial port is currently in a data transmission state. A value of 0 indicates that the serial port is currently sending data, while a value of 1 indicates that the serial port is currently idle.

[0061] S220: If the sending monitoring flag is 1 and the sending completion flag is 1, select a preset number of bytes of data to be transmitted from the current read position of the read pointer in the buffer to form a data packet.

[0062] In step S220, after obtaining the values ​​of the sending monitoring flag and the sending completion flag, the flag status is judged. When both the sending monitoring flag and the sending completion flag are 1, it is determined that the serial port is in a state that allows the initiation of new data packet transmission, and the packet assembly operation continues; otherwise, if the two flags do not meet the condition, the subsequent packet assembly and transmission process will not be executed.

[0063] It should be noted that the buffer is configured with a read pointer, which is used to mark the starting position of reading data to be transmitted in the buffer, and represents the offset address of the next data to be read within the buffer. When it is determined that the port allows data transmission, the current read position of the read pointer is used as the starting position, and data of a preset number of bytes is selected from the data to be transmitted stored in the buffer. This part of the data is then assembled into a data packet to be sent.

[0064] S230: Copy the data packet to the transmit buffer of the Universal Serial Bus device.

[0065] In step S230, after the data packet is formed, the formed data packet is copied to the transmit buffer corresponding to the Universal Serial Bus device.

[0066] It's important to note that the buffer corresponding to the serial port is used for long-term caching of data to be forwarded; while the transmit buffer of the serial port is used by the underlying transmission driver of the device. Data packets need to be copied to this area before they can be accessed by the underlying hardware, thus completing the hardware-level data output. The copy operation realizes the data migration from the service buffer to the driver-side transmit buffer, preparing the data for subsequent hardware transmission.

[0067] S240: Call the send start function and set both the send monitoring flag and the send completion flag to 0.

[0068] In step S240, after the data packet copy is completed, a dedicated send start function is called, which sets both the send monitoring flag and the send completion flag to 0.

[0069] The send start function is a dedicated, encapsulated program interface used to uniformly modify two port status flags when data packet transmission is about to begin. Setting both flags to 0 indicates that the current serial port has entered the data packet transmission working state, the port is busy, and does not currently support initiating another new data packet transmission request.

[0070] S250: Calls the data transmission driver of the Universal Serial Bus device to transmit data packets.

[0071] In step S250, after updating the status flag, the data transmission driver of the Universal Serial Bus (USB) device is invoked. The underlying data transmission driver takes over the data packets in the transmit buffer and executes the actual transmission of the data packets. The data transmission driver is a low-level driver adapted to the hardware peripheral. The driver will output the data packets in the transmit buffer to the outside through the hardware peripheral according to the USB communication protocol, thus completing the hardware transmission of the data packets.

[0072] In this embodiment, two status variables, a sending monitoring flag and a sending completion flag, are read. Two dedicated interfaces, a port status reading function and a sending start function, are set up to achieve centralized reading and setting management of the port status. Based on the flag status, it is determined whether sending is allowed. If sending is allowed, packets are assembled based on the buffer read pointer position, the data packets are copied to the driver-side sending buffer, the port busy status is updated, and finally, the underlying driver is called to complete the transmission. Managing the port status flags through a unified function interface avoids the state chaos caused by directly manipulating the flag bits, ensuring the orderly execution of the single data packet sending process logic.

[0073] Figure 3 This is a flowchart illustrating a data transmission method for an environmental monitoring system, which is provided in yet another embodiment of this application.

[0074] like Figure 3 As shown, before step S210: calling the port status reading function to read the serial port's transmit monitoring flag and transmit completion flag, the following steps S260 and S270 are also included.

[0075] S260: Get the maximum number of bytes N that the serial port can carry in a single data transmission.

[0076] In step S260, the maximum number of bytes N that the serial port port can carry in a single data transmission can first be obtained. It should be noted that the maximum number of bytes N that the serial port port can carry in a single data transmission is the maximum data length of a single data packet supported by the serial port port hardware. This parameter is determined by the hardware peripheral characteristics and port configuration parameters of the serial port port, and represents the maximum number of data bytes that the serial port port can carry in a single transmission.

[0077] Obtaining the maximum number of bytes N is the process of reading, receiving, or parsing the hardware-limited parameter. For example, it can read the configuration value of the hardware register, read the pre-stored configuration parameter, or read it directly from the port configuration information already saved by the device.

[0078] S270: Set N-1 as the preset number of bytes.

[0079] In step S270, after obtaining the maximum number of bytes N that can be transmitted in a single transmission at the serial port, a numerical calculation is performed, and the result obtained by subtracting 1 from N is used as the preset number of bytes to be used in the subsequent processes of this method.

[0080] It should be noted that the USB CDC communication protocol stipulates that when the total length of a data transmission is exactly an integer multiple of MPS (i.e., N in this application), the transmitting device must send an additional zero-length data packet (ZLP) to indicate to the receiving end that the current transmission of service data has been completed. Without this ZLP packet, the receiver cannot distinguish whether subsequent data has not yet arrived or the current transmission has been completed, which will cause parsing pauses and framing anomalies at the receiving end. The ZLP itself does not carry valid service data, but it requires occupying a USB bus transmission transaction, introducing additional protocol overhead and reducing the effective bandwidth utilization of the bus.

[0081] By constraining the length of a single data packet to the maximum number of bytes that the serial port can carry in a single transmission of data minus one byte, the length of any data packet is not an integer multiple of MPS, thus avoiding the scenario of ZLP generation. The host computer receiving the data can correctly determine the data boundary without ZLP, thereby reducing the overhead of ZLP-related protocols to zero and significantly improving the effective bandwidth utilization.

[0082] Figure 4 This is a flowchart illustrating a data transmission method for an environmental monitoring system, which is provided in yet another embodiment of this application.

[0083] like Figure 4 As shown, step S220: If the sending monitoring flag is 1 and the sending completion flag is 1, before selecting a preset number of bytes of data to be transmitted from the current reading position of the read pointer in the buffer to form a data packet, the following step S280 is also included.

[0084] S280: Saves the current read position of the read pointer.

[0085] In step S280, before forming the data packet, the current read position of the read pointer in the buffer can also be saved.

[0086] Understandably, the read pointer is a management variable for the buffer corresponding to the serial port, used to mark the offset position of the next data to be read in the buffer. Before selecting the data to be transmitted and assembling the data packet, the current read position of the read pointer is saved. Saving can be done by copying the current offset value of the read pointer to a temporary storage unit. This temporary storage unit can be a local variable or a backup variable bound to the serial port, specifically used to store the read pointer state before this transmission attempt.

[0087] like Figure 4 As shown, after step S250: calling the data transmission driver of the universal serial bus device to transmit the data packet, the following steps S290 and S2100 are also included.

[0088] S290: If data packet transmission fails, retrieve the saved current read position.

[0089] In step S290, after the underlying data transmission driver completes the send call, it returns the transmission result of this data packet, and the system will determine the transmission result. Data packet transmission failure includes various actual conditions: such as USB port hardware busy, bus abnormality, underlying driver returning an error code, and no response from the USB host. Any situation where the data packet fails to be successfully delivered to the USB host is considered a transmission failure at this point.

[0090] When a data packet transmission failure is detected, the backup value of the current read position, which was pre-saved in the temporary storage unit in the preceding steps, is retrieved. In other words, the backup value of the offset position recorded before the transmission attempt begins is read and used as the data source for subsequent recovery operations. If the data packet transmission is successful, there is no need to retrieve this backup position, and the read pointer can proceed forward according to normal logic.

[0091] S2100: Set the read pointer to the current read position.

[0092] In step S2100, after retrieving the pre-saved current read position, the read pointer of the buffer that is actually working is reassigned to the read position of the backup.

[0093] After this assignment operation, the read pointer rolls back to the offset position before the current transmission attempt was initiated. That is, although the packet assembly operation had retrieved the preset number of bytes of data from this position to assemble the data packet, because the transmission failed, the read pointer will not move forward, and the buffer logically considers that this part of the data has not yet been sent.

[0094] This application implements a non-blocking transmission mechanism by saving the current read position of the read pointer before transmission and restoring the read pointer to that position when data packet transmission fails. That is, when the serial port is busy, meaning the previous data packet has not yet been completely transmitted, the data is not discarded, the read pointer remains in its original position, and the next transmission attempt continues from the same position, avoiding data loss.

[0095] Figure 5 This is a flowchart illustrating a data transmission method for an environmental monitoring system, which is provided in yet another embodiment of this application.

[0096] like Figure 5 As shown, step S210: After calling the port status reading function to read the serial port's transmit monitoring flag and transmit completion flag, the following steps S2110 may be included.

[0097] S2110: If the sending monitoring flag is 0 or the sending completion flag is 0, the reading position of the reading pointer will be kept at the current reading position until the serial port triggers a sending completion interrupt.

[0098] In step S2110, when the sending monitoring flag is 0 or the sending completion flag is 0, it indicates that the current serial port is busy and new data packets are not yet allowed to be sent. At this time, no forward offset update operation is performed on the read pointer, and the read pointer is locked at the current reading position during this detection.

[0099] In other words, when the port is busy, the read pointer remains unchanged, and the data to be transmitted after that position in the buffer continues to be retained and will not be discarded due to the failure of this transmission attempt. Although the data transmission cannot be completed this time, the data to be transmitted still resides in the buffer, preserving the data state for subsequent retransmissions.

[0100] In this embodiment, when the transmission monitoring flag is detected to be 0 or the transmission completion flag is detected to be 0, meaning the serial port cannot currently send new data packets, the read pointer is kept at its current position, waiting for the serial port to trigger a transmission completion interrupt, which means waiting for the serial port to attempt to send data again. This further ensures that data is not discarded when the serial port is busy, avoiding data loss.

[0101] Figure 6 This is a flowchart illustrating a data transmission method for an environmental monitoring system, which is provided in yet another embodiment of this application.

[0102] like Figure 6 As shown, after step S240: calling the send start function and setting both the send monitoring flag and the send completion flag to 0, the following steps S2120 to S2150 may also be included.

[0103] S2120: In response to the monitoring flag changing from 1 to 0, the serial port port switching timestamp is recorded using a preset timing variable on the serial port port.

[0104] In step S2120, the monitoring flag is switched from 1 to 0, indicating that the serial port has changed from an idle and transmittable state to a busy transmittable state, and the device has started a hardware data packet transmission process.

[0105] When a state transition from 1 to 0 is detected in the transmission monitoring flag, the timestamp corresponding to the moment of this state transition is recorded using a preset timing variable on the serial port. The timestamp marks the start time of this busy transmission state and serves as the time reference for subsequent timeout detection. The timestamp can be taken from system time sources such as the system global clock cycle or system timer count.

[0106] S2130: Record the time difference between the current time and the switching timestamp.

[0107] In step S2130, after the switching timestamp is recorded, the current time of the system is continuously obtained, and the time difference between the current time and the aforementioned switching timestamp is calculated and recorded.

[0108] This time difference value represents the cumulative duration of the serial port being in a busy transmitting state. The calculation of this time difference can be performed periodically within the device's main control loop, eliminating the need for high-frequency calculations within the interrupt handler and reducing the processing load on the interrupt context. By continuously comparing the time difference, it can be determined whether the duration of the current busy transmitting state exceeds a reasonable service consumption range.

[0109] S2140: If the time difference is greater than or equal to the time difference threshold, the timeout recovery function is called to switch the monitoring flag from 0 to 1.

[0110] Under normal working conditions, the hardware transmission time of USB data packets is very short, and the interrupt for transmission completion will arrive in time, and the flag bit will be rewritten normally. When an abnormality occurs, the transmission completion interrupt is lost, the hardware response is abnormal, and the port remains in a busy state, and the time difference will continue to accumulate and increase.

[0111] In step S2140, the time difference threshold is a pre-configured time determination threshold used to define the maximum time that the serial port port is allowed to remain in the transmission state. This threshold can be adapted and configured according to the hardware communication rate and system scheduling characteristics.

[0112] The calculated busy duration difference is compared with the time difference threshold. If the time difference is greater than or equal to the time difference threshold, it is determined that the serial port has experienced a transmission deadlock. After the data packet transmission is started, no normal transmission completion event is received within the specified time. At this time, the preset timeout recovery function is called to perform a forced state recovery operation, forcibly switching the transmission monitoring flag from 0 back to 1.

[0113] The timeout recovery function is a dedicated, encapsulated processing interface that centrally handles the recovery logic for abnormal states, avoiding the logical confusion caused by scattered modifications to status flags in the business code. Setting the send monitoring flag to 1 indicates that the busy state lock of the port has been released, allowing the port to return to a state where it is allowed to initiate send requests.

[0114] S2150: Set the send completion flag to 1.

[0115] In step S2150, while calling the timeout recovery function and forcibly setting the sending monitoring flag to 1, the sending completion flag is simultaneously set to 1. The sending monitoring flag and the sending completion flag work together to control the port's sending permission; in abnormal scenarios, both flags need to be synchronously restored to an idle state. Setting the sending completion flag to 1, in conjunction with setting the sending monitoring flag to 1, jointly completes the port state reset.

[0116] In this embodiment, by recording the time difference between the current time and the switching timestamp, and when the time difference is greater than or equal to a time difference threshold, the sending monitoring flag is switched from 0 to 1, and the sending completion flag is set to 1, thereby forcibly restoring the serial port to an available state through a unified timeout recovery function. This ensures rapid recovery capability in abnormal situations while maintaining normal communication, and keeps the data loss window within a very short time.

[0117] In some embodiments, completing the current data transmission phase when the data transmission process in the current data transmission phase meets preset conditions may include: completing the current data transmission phase when the number of data packets transmitted in the current data transmission phase reaches a preset number.

[0118] The preset quantity is determined based on the data capacity of the buffer and the transmission rate of the serial port.

[0119] In this embodiment, the number of data packets transmitted in the current data transmission phase refers to the number of successfully transmitted data packets counted from the start of the current data transmission phase when the control loop enters the current data transmission phase. Throughout the entire processing of the data transmission phase, the program continuously counts the successfully transmitted data packets. When the counted number of transmitted data packets reaches a preset number, the sending monitoring flag is switched from 1 to 0, and the sending completion flag is set to 0, indicating that the serial port cannot send new data packets at this time. The termination condition is determined to be met, the operation of the current data transmission phase is completed, the data transmission processing flow is exited, and the program execution flow is returned to the main control loop of the universal serial bus device. The main control loop continues to execute the remaining business processing phases.

[0120] In other words, even if there is still unsent data in the buffer corresponding to the serial port, as long as the sending monitoring flag changes from 1 to 0 and the sending completion flag is set to 0, indicating that the number of data packets sent has reached the preset number, the current data transmission phase will still end. Data that has not yet been sent in the buffer will not be discarded and will be processed again when the main control loop re-enters the data transmission phase.

[0121] Furthermore, the preset quantity is determined based on the buffer's data capacity and the serial port's transmission rate. The buffer's data capacity refers to the maximum number of bytes that the entire circular buffer corresponding to the serial port can store, determining the upper limit of the backlog of data the buffer can hold. A larger buffer capacity allows more data packets to be sent in a single data transmission phase; a smaller buffer capacity prevents the sending of too many data packets at once, requiring reserved buffer space to receive subsequent incoming serial data. The serial port's transmission rate characterizes the data transmission capability that the serial port can complete per unit time, reflecting the speed of data packet transmission. In USB full-speed mode, constrained by the USB frame period, the maximum number of data packets that can be carried per 1ms frame window is limited; a higher transmission rate results in more data packets being transmitted per unit time; a lower transmission rate results in a relatively longer time to send a single packet.

[0122] For example, in the environmental monitoring extension system, the APM32E103 chip adopts a USB-CDC-ACM composite device solution, which virtualizes a single USB physical interface into five CDC serial ports. The USB peripheral of the APM32E103 chip only provides a dedicated data buffer of 512 bytes, which needs to be shared by all endpoints of all serial ports virtualized by the USB. Based on this, the selection of the preset number needs to take into account three dimensions simultaneously: buffer capacity adaptation, USB frame period timing adaptation, and multi-channel scheduling fairness.

[0123] First, the buffer size is adapted to the ring buffer capacity. Each serial port channel in this application is configured with an independent ring buffer; for example, the buffer size for a single channel is 512 bytes. If the preset value is too large, the buffer consumption in a single batch transmission will be too high, squeezing the buffer space for newly received data. If the value is too small, it will not be able to fully digest the new data continuously written by DMA between two main loops, easily causing data backlog in the buffer. Taking the preferred preset value of 4 as an example, combined with a single packet payload of 31 bytes, a maximum of 124 bytes are sent in a single data transmission phase, accounting for approximately 24% of the total 512-byte buffer capacity. This can digest most of the backlogged data while reserving sufficient buffer space for newly incoming data.

[0124] Second, it needs to be compatible with the USB frame period. The frame period in USB full-speed mode is 1ms, and there is a theoretical upper limit to the number of data packets that a single port can carry within one frame period. The preset value needs to be controlled within the range of the number of packets that can be processed in a single frame to avoid sending a batch of data across too many USB frames, which would introduce additional waiting latency. When the value is preferably 4, the overall transmission time of 4 data packets is about 0.2 to 0.5ms, which only occupies a part of the 1ms frame period. This allows time slices within the frame to be reserved for other multiple CDC ports, ensuring that multiple ports can time-share the USB bus.

[0125] Third, the fairness of scheduling among multiple channels. The system runs multiple CDC serial ports simultaneously. If the preset number is set too high, such as 8, a single channel will occupy a large amount of CPU and USB bus time at once, causing the data of other channels to not be scheduled and processed in time, resulting in data backlog in other channels. If the preset number is set too low, such as 2, the number of bytes sent at one time is limited, the ability to digest backlogged data is insufficient, and the risk of packet loss increases.

[0126] This application embodiment, by setting a preset number, can send multiple data packets in batches within a control loop, reducing data backlog in the buffer, lowering the risk of unsent data being overwritten by new data entering the buffer, and thus reducing data loss.

[0127] Figure 7 This is a flowchart illustrating a data transmission method for an environmental monitoring system, which is provided in yet another embodiment of this application.

[0128] like Figure 7 As shown, step S500: If the data transmission process in the current data transmission stage meets the preset conditions, the current data transmission stage is completed, which may include the following step S520.

[0129] S520: When the amount of data remaining to be transmitted is 0, complete the current data transmission phase.

[0130] It is understandable that the remaining amount of data to be transmitted is 0, indicating that there is no longer any valid business data that has not yet been sent out in the buffer corresponding to the serial port.

[0131] In other words, throughout the entire processing flow of the current data transmission phase, the remaining amount of data to be transmitted in the buffer is continuously assessed. When the amount of data to be transmitted remaining is detected to be 0, it indicates that all service data belonging to this serial port has been sent through the serial port, and there is no backlog of data in the buffer. At this point, there is no need to continue sending data packets, and the current data transmission phase can be completed and exited directly. After exiting, the program execution flow returns to the main control loop, which schedules and executes other service processing phases such as ADC acquisition, remote signaling, and telemetry.

[0132] In this embodiment, when all the data to be transmitted in the buffer has been sent, the current data transmission phase is completed even if the preset number of data packets to be sent has not been reached, thus avoiding unnecessary waiting and improving data transmission efficiency.

[0133] Figure 8 This is a flowchart illustrating a data transmission method for an environmental monitoring system, which is provided in yet another embodiment of this application.

[0134] like Figure 8 As shown, step S300: In response to a transmission completion interruption, detect the remaining data to be transmitted in the buffer, which may include the following steps S310 to S330.

[0135] S310: In response to a transmit completion interrupt, read the current write position of the serial port's write pointer and the current read position of the serial port's read pointer.

[0136] In step S310, the current write position of the write pointer and the current read position of the read pointer of the buffer corresponding to the local serial port are read.

[0137] The write pointer indicates the latest offset position of the data written by the DMA to the circular buffer, representing the end point of storing the newly received data; the read pointer indicates the starting offset position of the USB transmit logic reading the data to be transmitted. Each serial port has an independent circular buffer, and the read and write pointers of each channel are independent and do not interfere with each other.

[0138] S320: Call the buffer data volume calculation function to calculate the effective data volume between the current write position and the current read position.

[0139] In step S320, after obtaining the positions of the read pointer and the write pointer, a specially encapsulated buffer data volume calculation function is called, using the positions of the read pointer and the write pointer as input parameters to calculate the effective data volume between them.

[0140] This function for calculating the buffer size is an independently encapsulated interface adapted to the wraparound characteristic of circular buffers. When the write pointer reaches the end of the circular buffer, it wraps back to the beginning of the buffer to continue writing. Therefore, a simple subtraction cannot be used to calculate the number of bytes. Internally, this function implements the wraparound detection logic for the circular buffer and outputs the number of valid bytes yet to be transmitted in the buffer.

[0141] S330: If the effective data volume is not 0, then it is determined that the remaining data volume to be transmitted is not 0.

[0142] In step S330, the effective data volume output by the buffer data volume calculation function is used as the basis for judgment: if the effective data volume is not equal to 0, it is determined that there is still unsent business data inside the buffer, that is, the remaining data volume to be transmitted is not 0, and the logic of continuing to send data packets can continue to be executed; if the effective data volume is equal to 0, it is determined that there is no remaining data to be transmitted in the buffer, and no new data packet is initiated.

[0143] Figure 9 This is a flowchart illustrating a data transmission method for an environmental monitoring system, which is provided in yet another embodiment of this application.

[0144] like Figure 9As shown, step S100: In the current control loop of the universal serial bus device, before detecting the data to be transmitted in the buffer in response to the data transmission phase of the current control loop, the following step S600 is also included.

[0145] S600: Establishes a critical section period by disabling global interrupt protection for Universal Serial Bus devices.

[0146] The critical section period starts from the current write position of the write pointer of the serial port and ends after the data transmission phase of the current control loop is completed.

[0147] In step S600, by disabling global interrupt protection, a critical section period can be established. The critical section period starts from the current write position of the write pointer of the serial port and ends after the data transmission phase of the current control loop is completed. In this way, the data packet transmission process driven by the transmission completion interrupt is within the critical section period throughout, thereby avoiding mutual interference between the data packet transmission process driven by the transmission completion interrupt and the data transmission process driven by the data transmission phase of the current control loop.

[0148] Corresponding to the aforementioned embodiments of the data transmission method for the environmental monitoring system, this application also provides an embodiment of a data transmission device for the environmental monitoring system. Figure 10 This is a schematic diagram of the structure of a data transmission device for an environmental monitoring system provided in an embodiment of this application, as shown below. Figure 10 The data transmission device 1000 of the environmental monitoring system shown includes a detection unit 1100, a transmission unit 1200, and an execution unit 1300.

[0149] The detection unit 1100 is used to detect the data to be transmitted in the buffer in response to the data transmission phase of the current control cycle in the current control cycle of the universal serial bus device. The sending unit 1200 is used to transmit a data packet in the buffer through the serial port port when the amount of data to be transmitted is not 0, until the serial port port triggers a transmission completion interrupt. The data packet includes a preset number of bytes of data to be transmitted. The detection unit 1100 is also used to detect the remaining data to be transmitted in the buffer in response to a transmission completion interrupt; The transmitting unit 1200 is also used to transmit a data packet in the buffer through the serial port port when the amount of remaining data to be transmitted is not 0, until the serial port port triggers a transmission completion interrupt; The execution unit 1300 is used to complete the current data transmission stage when the data transmission process in the current data transmission stage meets the preset conditions.

[0150] Similar parts between the embodiments provided in this application can be referred to mutually. The specific implementation methods provided above are only a few examples under the overall concept of this application and do not constitute a limitation on the scope of protection of this application. For those skilled in the art, any other implementation methods extended from the solution of this application without creative effort shall fall within the scope of protection of this application.

Claims

1. A data transmission method for an environmental monitoring system, characterized in that, The environmental monitoring system includes a universal serial bus device, which includes at least one serial port and its corresponding buffer. The method includes: In the current control loop of the Universal Serial Bus device, in response to the data transmission phase of the current control loop, the data to be transmitted in the buffer is detected; If the amount of data to be transmitted is not zero, a data packet in the buffer is transmitted through the serial port until the serial port triggers a transmission completion interrupt. The data packet includes a preset number of bytes of the data to be transmitted. In response to the transmission completion interruption, detect the remaining data to be transmitted in the buffer; If the amount of remaining data to be transmitted is not zero, a data packet in the buffer is transmitted through the serial port until the serial port triggers the transmission completion interrupt. If the data transmission process in the current data transmission phase meets the preset conditions, the current data transmission phase is completed.

2. The data transmission method of the environmental monitoring system according to claim 1, characterized in that, The transmission of a data packet in the buffer through the serial port includes: Call the port status reading function to read the transmit monitoring flag and transmit completion flag of the serial port; If the sending monitoring flag is 1 and the sending completion flag is 1, starting from the current read position where the read pointer of the buffer is located, select the preset number of bytes of the data to be transmitted to form the data packet; Copy the data packet to the transmit buffer of the Universal Serial Bus device; Call the send start function to set both the send monitoring flag and the send completion flag to 0; The data transmission driver of the universal serial bus device is invoked to transmit the data packet.

3. The data transmission method of the environmental monitoring system according to claim 2, characterized in that, Before calling the port status reading function to read the transmission monitoring flag and transmission completion flag of the serial port, the method further includes: Obtain the maximum number of bytes N that the serial port can carry in a single data transmission; N-1 is determined as the preset number of bytes.

4. The data transmission method of the environmental monitoring system according to claim 2, characterized in that, If the sending monitoring flag is 1 and the sending completion flag is 1, before selecting the preset number of bytes of data to be transmitted from the current read position of the read pointer in the buffer to form the data packet, the method further includes: Save the current read position of the read pointer; After the data transmission driver of the universal serial bus device is invoked to transmit the data packet, the method further includes: If the data packet transmission fails, the saved current read position is retrieved; Place the read pointer at the current read position.

5. The data transmission method of the environmental monitoring system according to claim 2, characterized in that, After the method calls the port status reading function and reads the transmission monitoring flag and transmission completion flag of the serial port, the method further includes: If the transmission monitoring flag is 0 or the transmission completion flag is 0, the read position of the read pointer is maintained at the current read position until the serial port triggers the transmission completion interrupt.

6. The data transmission method of the environmental monitoring system according to claim 2, characterized in that, After calling the send start function and setting both the send monitoring flag and the send completion flag to 0, the method further includes: In response to the transmission monitoring flag switching from 1 to 0, the switching timestamp of the serial port is recorded using the preset timing variable of the serial port. Record the time difference between the current time and the switching timestamp; If the time difference is greater than or equal to the time difference threshold, the timeout recovery function is called to switch the sending monitoring flag from 0 to 1; Set the sending completion flag to 1.

7. The data transmission method of the environmental monitoring system according to claim 1, characterized in that, The step of completing the current data transmission phase when the data transmission process in the current data transmission phase meets preset conditions further includes: If the remaining amount of data to be transmitted is 0, the current data transmission phase is completed.

8. The data transmission method of the environmental monitoring system according to claim 1, characterized in that, In response to the interruption of transmission completion, detecting the remaining data to be transmitted in the buffer includes: In response to the transmission completion interrupt, read the current write position of the write pointer and the current read position of the read pointer of the serial port. Call the buffer data volume calculation function to calculate the effective data volume between the current write position and the current read position; If the effective data volume is not 0, then it is determined that the remaining data volume to be transmitted is not 0.

9. The data transmission method of the environmental monitoring system according to claim 8, characterized in that, Before detecting the data to be transmitted in the buffer, the method further includes: A critical section period is established by disabling the global interrupt protection of the universal serial bus device. The critical section period starts from the current write position of the write pointer of the serial port and ends after the data transmission phase of the current control loop is completed.

10. A data transmission device for an environmental monitoring system, characterized in that, The environmental monitoring system includes a universal serial bus device, which includes at least one serial port and its corresponding buffer. The device includes: A detection unit is configured to detect the data to be transmitted in the buffer in response to a data transmission phase in the current control loop of the universal serial bus device. The sending unit is used to transmit a data packet in the buffer through the serial port when the amount of data to be transmitted is not 0, until the serial port triggers a transmission completion interrupt, wherein the data packet includes the data to be transmitted in a preset number of bytes; The detection unit is also configured to detect the remaining data to be transmitted in the buffer in response to the transmission completion interruption; The sending unit is also used to transmit a data packet in the buffer through the serial port port when the amount of remaining data to be transmitted is not 0, until the serial port port triggers the transmission completion interrupt. An execution unit is used to complete the current data transmission phase when the data transmission process in the current data transmission phase meets preset conditions.