A method for serial data transmission using time fragments
By using a circular memory buffer and serial port transmission interrupts, the problem of waiting for transmission in serial communication is solved, enabling parallel execution of tasks during data transmission and improving program efficiency and real-time performance.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-10
- Publication Date
- 2026-03-03
AI Technical Summary
Existing serial communication methods have a waiting state during data transmission, resulting in low program execution efficiency and poor real-time performance, which is particularly prominent in scenarios with frequent log transmission.
Data transmission is performed by using a circular memory buffer in blocks and by utilizing serial port transmission interrupts, allowing other tasks to be executed while data is being sent, thus avoiding infinite loops.
It improves program execution efficiency, avoids unnecessary waiting time, is suitable for high-speed and frequent log transmission operations, and meets system real-time requirements.
Smart Images

Figure CN116069705B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to a method for serial port data transmission using time fragments. Background Technology
[0002] Serial data transmission is a very common method in embedded data transmission. Most existing serial communication methods employ a wait-to-send, continuously polling approach, such as... Figure 1 As shown, a buffer is first created, with pointer variable P0 pointing to the transmit buffer. The data to be transmitted is written to the buffer, and the length of the data to be transmitted is recorded and written to L0. When L0 is not equal to 0, data transmission is initiated. P0 points to the address of the next data to be transmitted. When the serial port is idle, the first data to be transmitted is written to the data register, L0 is decremented by 1, and the system enters an infinite loop for transmitting the next data. This loop continues until the serial port is idle, at which point the next data is transmitted, and so on, until all data has been transmitted before exiting the data transmission process and executing the next piece of code. During this process, data cannot be written to the buffer; new data must be refilled only after all data has been transmitted. Throughout this process, the program spends a period of time in a waiting state, resulting in a single, inefficient, and poor real-time performance, especially in scenarios where the serial port is frequently used for log transmission, where these drawbacks are even more pronounced. Summary of the Invention
[0003] To address the aforementioned problems, this invention provides a method for serial port data transmission using time fragments. It designs a circular buffer with block-based buffering and employs a serial port transmission interrupt method to send data. While sending data, writing data or other system tasks can be performed simultaneously, significantly improving program execution efficiency and avoiding unnecessary waiting time. This method is particularly suitable for high-speed, frequent log transmission operations and multi-tasking scenarios with high real-time requirements.
[0004] To achieve the above-mentioned technical objectives and effects, the present invention is implemented through the following technical solution:
[0005] A method for serial port data transmission using time fragments includes the following steps:
[0006] S1. Create a circular memory buffer and divide it into blocks:
[0007] Create a circular memory buffer A, and divide the circular memory buffer into N equal blocks, each block being numbered sequentially as A. x Each buffer creates a length variable L. x Calculate the length of data to be sent in this block, and create a pointer P for each buffer. xPointing to the starting address of this block, X = 1, 2, ..., N, let's say the current transmission is the Ath block. C Data in the block buffer, Ath C There are still L0 data items to be sent in the block, and the address of the data to be sent is the address pointed to by the send pointer P0;
[0008] S2. When data needs to be sent, the specific steps include the following:
[0009] S201, when sending A C When the first data in the block is:
[0010] 201-1, Determine the case of A. C Check if the length of the data to be sent in the block is greater than 0. If yes, proceed to step 201-2; otherwise, end.
[0011] 201-2, Send pointer P0 points to A C The starting address P of the block c Send counter L0 = L c ;
[0012] 201-3. Write the data pointed to by the transmit pointer P0 into the transmit data register and send it out;
[0013] 201-4. Update L0 to (L0-1), update P0 to (P0+1), enable the serial port transmit interrupt, and then proceed to step S202 to send the remaining data using the serial port transmit interrupt.
[0014] S202, when sending A C When there is remaining data in the block:
[0015] 202-1. Determine if the transmit counter L0 is equal to 0. If it is not equal to 0, proceed to step 202-2; otherwise, disable the serial port transmit interrupt.
[0016] 202-2. Write the data pointed to by the transmit pointer P0 into the transmit data register and send it out;
[0017] 202-3. Update L0 to (L0-1) and update P0 to (P0+1);
[0018] 202-4. Proceed to step 202-1.
[0019] Preferably, let the data be in the Ath month. u Filling in blocks occurs when data needs to be written to the circular memory buffer:
[0020] A. Based on the length of the data to be filled, calculate the number of blocks that need to be filled in the buffer, and proceed to step B;
[0021] B, starting at point Au The block buffer is filled with data, and the length of the filled data is written to the length variable L corresponding to the block. U Proceed to step C;
[0022] C. Update A u For A u+1 Proceed to step D;
[0023] D. Determine if U is greater than N. If yes, proceed to step E; otherwise, proceed to step F.
[0024] E. Update A u If the result is A1, proceed to step F;
[0025] F. Check if the number of blocks already filled is equal to the number of blocks that need to be filled. If yes, end the process; otherwise, proceed to step B.
[0026] Preferably, in step 202-1, after disabling the serial port's transmit interrupt, proceed to step 202-5;
[0027] 202-5, Clear section A C The block needs to send data of length 0, update A. C For A C+1 Determine if C is greater than N. If not, proceed to step 201-1; if yes, update A. C If the value is A1, proceed to step 201-1.
[0028] Preferably, N is between 10 and 30.
[0029] Preferably, N is 20.
[0030] Preferably, Q is 50-200 bytes.
[0031] Preferably, if the memory size of each buffer is Q, then Q is 100 bytes.
[0032] The beneficial effects of this invention are:
[0033] This invention automatically utilizes time fragments to send serial port data, avoiding unnecessary waiting time. Essentially, this invention uses the time previously spent waiting for serial port data to perform other tasks, significantly improving program execution efficiency and eliminating waiting time. This invention employs non-infinite loop block processing, allowing serial port data to be sent while simultaneously filling in data to be sent, and enabling the execution of other program tasks concurrently, greatly improving program efficiency. Attached Figure Description
[0034] Figure 1 This is a flowchart of the conventional serial port data transmission method of the present invention;
[0035] Figure 2 This is a block diagram of the circular buffer of the present invention;
[0036] Figure 3 This is a flowchart of the buffer data filling process of the present invention;
[0037] Figure 4 This is a flowchart of the process of sending the first byte of data in this invention;
[0038] Figure 5 This is a flowchart of the serial port interrupt data transmission process of the present invention. Detailed Implementation
[0039] The technical solution of the present invention will be further described in detail below with reference to the accompanying drawings and specific embodiments, so that those skilled in the art can better understand the present invention and implement it. However, the embodiments are not intended to limit the present invention.
[0040] A method for serial port data transmission using time fragments includes the following steps:
[0041] S01. Create a circular memory buffer and divide it into blocks:
[0042] Create a circular memory buffer A, and divide the circular memory buffer into N equal blocks, each block being numbered sequentially as A. x Each buffer creates a length variable L. x Calculate the length of data to be sent in this block, and create a pointer P for each buffer. x Point to the starting address of this block, X = 1, 2, ..., N, and the memory size of each buffer is Q. Let the current transmission be the Ath block. C Data in the block buffer, Ath C There are still L0 data items to be sent in the block. The address currently being sent is the address pointed to by the send pointer P0. The next data will be sent at address A. u Fill the block.
[0043] Generally, the value of N should not be too small to prevent data overwriting during reading and writing. Preferably, N is between 10 and 30, and preferably, Q is between 50 and 200 bytes. Figure 2 As shown, a circular memory buffer A is created, with a size of 2000 bytes. The circular memory buffer is divided into 20 equal blocks (when N is 20, there is generally no data overwriting between read and write operations, which is sufficient for the system to process reads and writes in parallel). The size Q of each block is 100 bytes. If C=3 and U=5, it means that the data being sent is currently in block A3. Block A4 is already filled with data, and the next data will be filled in A5. The pointer variable P0 points to the starting address P3 of block A3.
[0044] Preferably, when data needs to be sent, the data transmission is divided into two parts, the first part being as follows: Figure 4 As shown in S201, when sending the Ath... C When the first data in the block is:
[0045] 201-1, Determine the case of A. C Check if the length of the data to be sent in the block is greater than 0. If yes, proceed to step 201-2; otherwise, end.
[0046] 201-2, Send pointer P0 points to A C The starting address P of the block c Send counter L0 = L c ;
[0047] 201-3. Write the data pointed to by the transmit pointer P0 into the transmit data register and send it out;
[0048] 201-4. Update L0 to (L0-1), that is, decrement the value of the transmit counter by 1, update P0 to (P0+1), that is, make the transmit pointer point to the address of the next byte, enable the serial port transmit interrupt, and then proceed to step S202 to send the remaining data using the serial port transmit interrupt.
[0049] The second part is as follows Figure 5 As shown in S202, when sending the Ath... C When there is remaining data in the block:
[0050] 202-1. Determine if the transmit counter L0 is equal to 0. If it is not equal to 0, proceed to step 202-2; otherwise, disable the serial port transmit interrupt.
[0051] 202-2. Write the data pointed to by the transmit pointer P0 into the transmit data register and send it out;
[0052] 202-3. Update L0 to (L0-1) and update P0 to (P0+1);
[0053] 202-4. Proceed to step 202-1 until the data transmission is complete.
[0054] Preferred, such as Figure 5 As shown, in step 202-1, after disabling the serial port's transmit interrupt, proceed to step 202-5:
[0055] 202-5, Clear section A C The block needs to send data of length 0, update A. C For A C+1 Determine if C is greater than N. If not, proceed to step 201-1; if yes, update A. CIf the value is A1, proceed to step 201-1.
[0056] The data transmission of this invention is divided into two parts. The first byte of data in each buffer is sent directly. After the data is sent, the transmission interrupt is enabled. The remaining data in the same block is sent out through the transmission interrupt. The whole process is a no-wait transmission, and multiple tasks can be executed simultaneously. After all the data is sent, the transmission interrupt is disabled, and the next block of transmission is started.
[0057] When data needs to be written to the circular memory buffer, such as Figure 3 As shown, it includes the following steps:
[0058] A. Based on the length of the data to be filled, calculate the number of blocks that need to be filled in the buffer, and proceed to step B;
[0059] B, starting at point A u The block buffer is filled with data, and the length of the filled data is written to the length variable L corresponding to the block. U Proceed to step C;
[0060] C. Update A u For A u+1 Proceed to step D;
[0061] D. Determine if U is greater than N. If yes, proceed to step E; otherwise, proceed to step F.
[0062] E. Update A u If the result is A1, proceed to step F;
[0063] F. Check if the number of blocks already filled is equal to the number of blocks that need to be filled. If yes, end the process; otherwise, proceed to step B.
[0064] Assuming U = 5, the data length to be filled this time is 220 bytes, and the memory size of each block is set to Q = 100 bytes. This means the data needs to be written to buffer blocks A5, A6, and A7. A5 writes the first 100 bytes, A6 writes the next 100 bytes, and A7 writes the last 20 bytes. Therefore, the length variable L5 for block A5 is 100, the length variable L6 for block A6 is 100, and the length variable L7 for block A7 is 20. The next filling position is block A8, which corresponds to U = U + 3, or U = 8. When U is greater than N, the process returns to the beginning address of the ring.
[0065] This invention allows reading and writing data to be performed independently or simultaneously. As long as N is not set too low, the two processes can proceed concurrently without interference. The specific value can be set according to the usage environment. This invention automatically utilizes time fragments for serial port data transmission, avoiding unnecessary waiting time. Essentially, this invention uses the original serial port waiting time for other tasks, significantly improving program execution efficiency and avoiding waiting time. This invention employs non-infinite loop block processing, allowing serial port data to be sent while simultaneously filling in data to be sent, and enabling the execution of other program tasks concurrently, greatly improving program efficiency.
[0066] The above are merely preferred embodiments of the present invention and do not limit the patent scope of the present invention. Any equivalent structural or procedural transformations made based on the content of the present invention's specification and drawings, or direct or indirect applications in other related technical fields, are similarly included within the patent protection scope of the present invention.
Claims
1. A method for serial data transmission using time fragments, characterized in that, Comprising the following steps: S1, creating a ring memory buffer and blocking: Create a circular memory buffer Divide the circular memory buffer into equal parts The blocks, each buffer, are numbered sequentially as follows: Each buffer creates a length variable. Calculate the length of data to be sent in this block, and create a pointer for each buffer. Points to the starting address of the block. Let the current message be the first one sent. Data in the block buffer, the first There are also blocks Data needs to be sent, and the current address for sending data is the send pointer. The address it points to; S2, when data transmission is needed, comprising the following steps: S201、when sending the first data in the block the first data in the block 201-1, judge whether the length of data to be sent in the block is greater than 0, if yes, go to step 201-2, if not, end; 201-1, judge whether the length of data to be sent in the block is greater than 0, if yes, go to step 201-2, if not, end 201-2, send pointer point to first address of block , send counter = ; 201-3, the transmit pointer points to is written into the transmit data register and sent out; 201-4, update is (0), and -1), update is (0), and +1), open the sending interrupt of the serial port, and then enter step S202 to send the remaining data by using the serial port sending interrupt; S202、when sending the first when the remaining data in the block is sent: 202-1, judging the sending counter whether it is equal to 0, if not, then entering step 202-2, otherwise, closing the sending interrupt of the serial port; 202-2, the transmit pointer data pointed to by the transmit pointer is written into the transmit data register and sent out; 202-3, update is (0 -1), update is (0 +1); 202-4, enter step 202-1; Set the data next time will be in the block filling, when it is necessary to write data to the ring memory buffer: A, according to the data length needed to fill this time, calculate the number of blocks needed to fill the buffer, enter step B; B, start at the first data filling in the block buffer and write the filled data length into the length variable corresponding to the block In this case, go to step C; C. update for go to step D; D, determine whether greater than if yes, go to step E, if no, go to step F; E. update to , go to step F; F, whether the number of blocks filled this time is equal to the number of blocks needed to fill, if yes, end, if not, enter step B.
2. The method for serial data transmission using time fragmentation according to claim 1, wherein, In step 202-1, after closing the sending interrupt of the serial port, enter step 202-5; 202-5, empty the first The data length that the block needs to send is 0, update For , determine Whether it is greater than , if not, go to step 201-1; if yes, update For , go to step 201-1.
3. The method for serial data transmission using time fragmentation of claim 1, wherein, takes values between 10 and 30.
4. The method for serial data transmission using time fragmentation according to claim 3, wherein, The value is 20.
5. The method for serial data transmission using time fragmentation of claim 1, wherein, Value is 50-200 bytes.
6. The method for serial data transmission using time fragmentation according to claim 5, wherein, Let the memory size of each buffer be Then The value is 100 bytes.
Citation Information
Patent Citations
Method and device for circularly reading and writing in buffer zone
CN102169420A
Method for carrying out real-time communication by using serial port of ARM processor
CN112131153A