Data transmission method based on improved USART protocol
By adding data response mechanism, transmission error detection and correction mechanism, and data integrity verification mechanism to the USART protocol, the problem of low data transmission reliability in industrial environments is solved, and the reliability of data transmission is improved.
Patent Information
- Application Number
- CN202410963988.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-18
- Publication Date
- 2024-11-22
AI Technical Summary
The USART protocol is easily affected by noise in industrial environments or environments with strong electromagnetic interference, resulting in an increase in data transmission error rate, reduced reliability, lack of data response mechanisms, insufficient transmission error detection and correction mechanisms, and limited verification function, and inability to be effective. Ensure data integrity.
Based on the USART protocol, data response mechanism, transmission error detection and correction mechanism, and data integrity verification mechanism are added, so as to ensure the reliability of data transmission through data serial number and CRC verification code, and achieve correct reception and integrity verification of data segments.
Through the data response mechanism, ensure that the sender determines whether the receiver successfully receives data, transmit error detection and correction mechanisms to timely identify and retransmit lost data, and the data integrity verification mechanism detects data tampering or corruption, reduces data transmission error rate, and improves data transmission reliability.
Smart Images

Figure CN119011701A_ABST
Abstract
Description
Technical Field
[0001] The invention relates to the field of data transmission, and in particular to a data transmission method based on an improved USART protocol. Background Art
[0002] USART (Universal Synchronous / Asynchronous Receiver / Transmitter) is a universal serial communication interface protocol, commonly used for data transmission between microcontrollers and peripheral devices. It supports synchronous and asynchronous transmission modes and can transmit data at a higher speed.
[0003] However, the USART protocol is easily affected by noise in industrial environments or environments with strong electromagnetic interference, resulting in increased data transmission error rates and reduced reliability. In addition, the USART protocol itself also has some defects:
[0004] (1) Lack of data response mechanism: The sender cannot confirm whether the receiver has successfully received the data, which may result in data loss.
[0005] (2) Inadequate transmission error detection and correction mechanism: Data lost during data transmission cannot be identified and retransmitted in a timely manner, reducing transmission reliability.
[0006] (3) The check bit function is limited: it can only detect single-bit errors and cannot effectively ensure data integrity or prevent data from being tampered with or damaged.
[0007] These defects seriously affect application scenarios that require high data transmission reliability. Summary of the invention
[0008] In view of the deficiencies in the prior art, the present invention provides a data transmission method based on an improved USART protocol to solve the problems that the USART protocol is easily affected by noise in an industrial environment or an environment with strong electromagnetic interference, resulting in an increased data transmission error rate and reduced reliability.
[0009] The technical solution of the present invention is:
[0010] A data transmission method based on an improved USART protocol, based on the USART protocol, adds a data response mechanism to enable the sending end to determine whether the receiving end has successfully received the data, adds a transmission error detection and correction mechanism to identify and process the frame loss phenomenon that occurs during data transmission, and adds a data integrity verification mechanism to detect whether the data is tampered with or damaged during transmission;
[0011] The data response mechanism specifically includes the following steps:
[0012] A1: The sender divides the data to be transmitted into equal-length data segments whose length does not exceed the buffer capacity of the receiver;
[0013] A2: assigning a data sequence number to each data segment according to the transmission order of the data segments, and encapsulating each data segment to obtain a data frame; the data sequence number is used to identify the transmission order of the data segments;
[0014] The data segment is encapsulated according to the RUTF frame format:
[0015] Header: used to mark the start of RUTF;
[0016] Flag bit: used to mark whether the frame is a data frame or a response frame. If the value of the flag bit is 0, it means that the frame is a data frame. If the value of the flag bit is 1, it means that the frame is a response frame.
[0017] Sequence number: used to record the data sequence number of the current data frame or the response sequence number of the current response frame; the response sequence number is the sequence number attached to the response frame after the receiving end successfully receives a data segment and verifies its integrity, which is used to inform the sending end that the data segment has been successfully received;
[0018] Data: data to be sent, i.e. the divided data segments;
[0019] CRC checksum: used to check whether the data has been tampered with or damaged during transmission;
[0020] Tail: used to mark the end of RUTF;
[0021] A3: The sending end sends the data frame to the receiving end through several data frames in the format specified by the USART protocol;
[0022] A4: The receiving end receives the data frame sent by the sending end and verifies it. If the verification is successful, the receiving end accepts the data frame and sends a response frame to the sending end. Otherwise, the data frame is discarded. The response frame is also encapsulated in the RUTF frame format.
[0023] Specifically, the receiving end starts to cache the data frame after receiving the data frame in the format specified by the USART protocol including the header, and ends the reception of the data frame after receiving the data frame in the format specified by the USART protocol including the tail, so as to obtain a complete data frame. The receiving end initializes the response sequence number, and attaches the current response sequence number each time a response frame is sent. Each time a data frame is received, it is verified whether the data sequence number in the data frame is the same as the current response sequence number. If they are the same, the data frame is accepted and the response frame is sent, and the response sequence number is incremented. If they are different, the data frame is discarded.
[0024] A5: If the sender receives a response frame, it means that the receiver has successfully received the data. If it does not receive a response frame, it is considered that the data transmission has failed. The sender will resend the data frame after a set time until it successfully receives a response frame.
[0025] The transmission error detection and correction mechanism specifically includes the following steps:
[0026] B1: The sender sends the corresponding data frame to the receiver according to the maintained data sequence number;
[0027] B2: Start a timer and set a timeout period;
[0028] B3: If the sender does not receive the response frame sent by the receiver before the timer expires, it is considered that the data frame is lost or the response frame fed back by the receiver is lost and returns to B1 until the sender receives the response frame sent by the receiver.
[0029] The process of sending the response frame by the receiving end is specifically as follows: the receiving end determines whether there is a data frame received repeatedly according to the data sequence number in the data frame, and if not, receives the data frame, and sends a response frame with the same response sequence number as the data sequence number in the data frame to inform the sending end that the data frame has been received; if yes, discards the repeatedly received data frame, and sends a response frame with the same response sequence number as the data sequence number in the data frame again to inform the sending end that the data frame has been received;
[0030] The data integrity verification mechanism specifically includes the following steps:
[0031] C1: Select the polynomial used for CRC calculation;
[0032] C2: According to the number of bits of the selected polynomial, set a CRC register with the same number of bits and assign an initial value to the CRC register;
[0033] C3: Calculate the CRC checksum for each data segment using the CRC register combined with the selected polynomial;
[0034] Specifically, the sender performs XOR operation on each data bit of the data segment and the most significant bit of the value in the CRC register. If the XOR result is 1, the value in the CRC register is shifted left by one bit and XORed with the selected polynomial. If the XOR result is 0, only the value in the CRC register is shifted left by one bit. After processing all the data bits of the data segment, the value in the CRC register is the calculated CRC check code.
[0035] C4: The calculated CRC check code is added to the CRC check code part of the RUTF format data frame and sent to the receiving end;
[0036] C5: After receiving the data segment and CRC checksum in the RUTF format data frame, the receiver recalculates the CRC checksum based on the received data segment using the selected polynomial and the initial value of the CRC register;
[0037] C6: The receiving end compares the received CRC check code with the calculated CRC check code. If the two CRC check codes are the same, it means that the data segment has not been tampered with or damaged. Otherwise, it means that the data segment has been tampered with or damaged during transmission.
[0038] Compared with the prior art, the beneficial effects of the present invention are:
[0039] (1) Data response mechanism: By establishing a data response mechanism, the sender can ensure that the receiver can determine whether the data has been successfully received.
[0040] (2) Transmission error detection and correction mechanism: A transmission error detection and correction mechanism is established to promptly identify and retransmit data lost during data transmission.
[0041] (3) Data integrity verification mechanism: Establish a data integrity verification mechanism to detect whether the data has been tampered with or damaged during transmission.
[0042] Through the above improvements, the data transmission error rate of the USART protocol in industrial environments or noisy electromagnetic environments can be reduced, and the reliability of data transmission can be improved. BRIEF DESCRIPTION OF THE DRAWINGS
[0043] Figure 1 The present invention is a data response mechanism flow chart of a data transmission method based on an improved USART protocol in an embodiment of the present invention;
[0044] Figure 2 A schematic diagram of a reliable USART transmission frame format proposed by a data transmission method based on an improved USART protocol in an embodiment of the present invention;
[0045] Figure 3 The present invention is a flow chart of a transmission error detection and correction mechanism of a data transmission method based on an improved USART protocol in an embodiment of the present invention;
[0046] Figure 4 The present invention is a flowchart of a data integrity verification mechanism of a data transmission method based on an improved USART protocol in an embodiment of the present invention;
[0047] Figure 5 The present invention is a schematic diagram of a data transmission process of a data transmission method based on an improved USART protocol in an embodiment of the present invention. DETAILED DESCRIPTION
[0048] The present invention is described in detail below with reference to the accompanying drawings and embodiments.
[0049] A data transmission method based on an improved USART protocol, based on the USART protocol, adds a data response mechanism to enable the sender to determine whether the receiver has successfully received the data, adds a transmission error detection and correction mechanism to identify and process frame loss in the data transmission process, and adds a data integrity verification mechanism to detect whether the data has been tampered with or damaged in the transmission process.
[0050] The data transmission method based on the improved USART protocol provided by the present invention does not require any changes to the USART protocol itself, but only requires the addition of a data response mechanism, a transmission error detection and correction mechanism, and a data integrity verification mechanism at the data sending end and the receiving end.
[0051] In this implementation, the USART protocol is initialized in a conventional manner according to different platforms.
[0052] The data response mechanism is as follows Figure 1 As shown, the specific steps include:
[0053] A1: The sender divides the data to be transmitted into equal-length data segments whose length does not exceed the buffer capacity of the receiver;
[0054] A2: assigning a data sequence number to each data segment according to the transmission order of the data segments, and encapsulating each data segment according to the frame format of RUTF (Reliable USART Transmission Frame) to obtain a data frame in RUTF format; the data sequence number is used to identify the transmission order of the data segments;
[0055] A2: assigning a data sequence number to each data segment according to the transmission order of the data segments, and encapsulating each data segment to obtain a data frame; the data sequence number is used to identify the transmission order of the data segments;
[0056] The data segment is encapsulated according to the frame format of RUTF (Reliable USART Transfer Frame) as follows Figure 2 As shown, specifically:
[0057] Header: used to mark the start of RUTF;
[0058] Flag bit: used to mark whether the frame is a data frame or a response frame. If the value of the flag bit is 0, it means that the frame is a data frame. If the value of the flag bit is 1, it means that the frame is a response frame.
[0059] Sequence number: used to record the data sequence number of the current data frame or the response sequence number of the current response frame; the response sequence number is the sequence number attached to the response frame after the receiving end successfully receives a data segment and verifies its integrity, which is used to inform the sending end that the data segment has been successfully received;
[0060] Data: data to be sent, i.e. the divided data segments;
[0061] CRC (Cyclic Redundancy Check): used to check whether the data has been tampered with or damaged during transmission;
[0062] Tail: used to mark the end of RUTF;
[0063] A3: The transmitting end sends the data frame to the receiving end through several data frames in the format specified by the USART protocol (i.e., the frame format used to transmit data in the USART protocol);
[0064] Since the data bits of the data frame in the format specified by the USART protocol are usually 5 to 9 bits (configurable), it is usually unable to accommodate a complete data frame in the RUTF format. Therefore, the data frame in the RUTF format is transmitted in batches using the data frame in the format specified by the USART protocol.
[0065] The sender and receiver maintain their own sequence numbers to track data transmission. Specifically, the sender initializes the data sequence number at the beginning of data transmission, and attaches the current data sequence number each time it sends a data frame in RUTF format, and increments the data sequence number after successfully sending and receiving a response frame;
[0066] A4: The receiving end receives the data frame sent by the sending end and verifies it. If the verification is successful, the receiving end accepts the data frame and sends a response frame to the sending end. Otherwise, the data frame is discarded. The response frame is also Figure 2 Specifically, the receiving end stores the response sequence number in the sequence number in the response frame and sets the value of the flag bit in the response frame to 1.
[0067] Specifically, the receiving end starts to cache the data frame after receiving the data frame in the format specified by the USART protocol including the header, and ends the reception of the data frame after receiving the data frame in the format specified by the USART protocol including the tail, thereby obtaining a complete data frame. The receiving end initializes the response sequence number, and attaches the current response sequence number each time a response frame is sent. Each time a data frame is received, it is verified whether the data sequence number in the data frame is the same as the current response sequence number. If they are the same, the data frame is accepted and the response frame is sent, and the response sequence number is incremented. If they are different, the data frame is discarded and no other processing is performed. This mechanism ensures that the data segments in the RUTF format data frame are transmitted in the correct order, and can effectively assist in handling errors and retransmissions during data transmission.
[0068] A5: If the sender receives a response frame, it means that the receiver has successfully received the data. If it does not receive a response frame, it is considered that the data transmission has failed. The sender will resend the data frame after a set time until it successfully receives a response frame.
[0069] In this implementation manner, if the transmitting end receives a response frame, it only needs to read the response sequence number in the sequence number to determine which data has been successfully transmitted.
[0070] The transmission error detection and correction mechanism is as follows Figure 3 As shown, the specific steps include:
[0071] B1: The sender sends the corresponding data frame to the receiver according to the maintained data sequence number;
[0072] B2: Start a timer and set a timeout period;
[0073] B3: If the sender does not receive the response frame sent by the receiver before the timer expires, it is considered that the data frame is lost or the response frame fed back by the receiver is lost and returns to B1 until the sender receives the response frame sent by the receiver.
[0074] The process of sending the response frame by the receiving end is specifically as follows: the receiving end determines whether there is a data frame received repeatedly according to the data sequence number in the data frame, and if not, receives the data frame, and sends a response frame with the same response sequence number as the data sequence number in the data frame to inform the sending end that the data frame has been received; if yes, discards the repeatedly received data frame, and sends a response frame with the same response sequence number as the data sequence number in the data frame again to inform the sending end that the data frame has been received;
[0075] The data integrity verification mechanism is as follows Figure 4 As shown, the specific steps include:
[0076] C1: Select the polynomial used for CRC calculation;
[0077] Commonly used polynomials include CRC-16, CRC-32, etc. The choice of polynomial depends on its error detection capability, matching degree with the transmitted data, implementation complexity, etc. Now assume that the sender and receiver select CRC-32 polynomial '0x04C11DB7';
[0078] C2: According to the number of bits of the selected polynomial, set a CRC register with the same number of bits and assign an initial value to the CRC register (usually all 0s or all 1s);
[0079] Since the CRC-32 polynomial is used, a 32-bit CRC register is set at both the sender and the receiver, and the CRC register is given an initial value of '0xFFFFFFFF' (usually all 0s or all 1s).
[0080] C3: Calculate the CRC checksum for each data segment using the CRC register combined with the selected polynomial;
[0081] Specifically, the sender performs XOR operation on each data bit of the data segment and the most significant bit (MSB) of the value in the CRC register. If the XOR result is 1, the value in the CRC register is shifted left by one bit and XORed with the selected polynomial. If the XOR result is 0, only the value in the CRC register is shifted left by one bit without any other processing. After all the data bits of the data segment are processed, the value in the CRC register is the calculated CRC check code.
[0082] C4: The calculated CRC check code is added to the CRC check code part of the RUTF format data frame and sent to the receiving end;
[0083] C5: After receiving the data segment and CRC checksum in the RUTF format data frame, the receiver recalculates the CRC checksum based on the received data segment using the selected polynomial '0x04C11DB7' and the initial value '0xFFFFFFFF' of the CRC register;
[0084] C6: The receiving end compares the received CRC checksum with the calculated CRC checksum. If the two CRC checksums are the same, it means that the data segment has not been tampered with or damaged. Otherwise, it means that the data segment has been tampered with or damaged during transmission.
[0085] like Figure 5 As shown, the working process of a data transmission method based on the improved USART protocol is shown, specifically:
[0086] Initially, the sender sends data frame DATARUTF 0. After receiving DATARUTF 0, the receiver performs a CRC check on the data in DATARUTF 0. If the check is correct, it responds with an acknowledgement frame ACK RUTF 0 to the sender.
[0087] After receiving ACK RUTF 0, the sender knows that the receiver has successfully received DATARUTF 0, so it further sends data frame No. 1 DATARUTF 1.
[0088] Now assume that DATA RUTF 1 is lost or its data is damaged during transmission, then the receiving end will not respond to DATA RUTF 1, that is, the sending end will not receive the receiving end's response to DATA RUTF 1.
[0089] Then, the sender will resend DATARUTF 1 after the timer expires.
[0090] Similarly, after the receiving end receives DATARUTF 1 and verifies that the data is correct, it responds with ACK RUTF1 to the sending end.
[0091] Now assume that ACK RUTF 1 is lost during transmission, then the sender will not receive ACK RUTF 1, and the sender will resend DATARUTF 1 after the timer expires.
[0092] After receiving DATARUTF 1, the receiving end will find that the frame has been received, so the receiving end will discard the received DATARUTF 1 and respond to the sending end with ACK RUTF 1 again.
[0093] After receiving ACK RUTF 1, the sender knows that the receiver has successfully received DATARUTF 1, so it further sends data frame No. 2 DATARUTF 2.
Claims
1. A data transmission method based on an improved USART protocol, characterized in that: Based on the USART protocol, a data response mechanism is added to enable the sender to determine whether the receiver has successfully received the data. A transmission error detection and correction mechanism is added to identify and handle frame loss during data transmission. A data integrity verification mechanism is added to detect whether the data has been tampered with or damaged during transmission.
2. A data transmission method based on an improved USART protocol according to claim 1, characterized in that, The data response mechanism specifically includes the following steps: A1: The sender divides the data to be transmitted into equal-length data segments whose length does not exceed the buffer capacity of the receiver; A2: assigning a data sequence number to each data segment according to the transmission order of the data segments, and encapsulating each data segment to obtain a data frame; the data sequence number is used to identify the transmission order of the data segments; A3: The sending end sends the data frame to the receiving end through several data frames in the format specified by the USART protocol; A4: The receiving end receives the data frame sent by the sending end and verifies it. If the verification is successful, the receiving end accepts the data frame and sends a response frame to the sending end. Otherwise, the data frame is discarded. The response frame is also encapsulated according to the frame format of RUTF; A5: If the sender receives a response frame, it means that the receiver has successfully received the data. If the response frame is not received, it is considered that the data transmission has failed. The sender will resend the data frame after a set time until the response frame is successfully received.
3. A data transmission method based on improved USART protocol according to claim 2, characterized in that, The data segment is encapsulated according to the RUTF frame format: Header: used to mark the start of RUTF; Flag bit: used to mark whether the frame is a data frame or a response frame. If the value of the flag bit is 0, it means that the frame is a data frame. If the value of the flag bit is 1, it means that the frame is a response frame. Sequence number: used to record the data sequence number of the current data frame or the response sequence number of the current response frame; the response sequence number is the sequence number attached to the response frame after the receiving end successfully receives a data segment and verifies its integrity, which is used to inform the sending end that the data segment has been successfully received; Data: data to be sent, i.e. the divided data segments; CRC checksum: used to check whether the data has been tampered with or damaged during transmission; Tail: Used to mark the end of RUTF.
4. A data transmission method based on an improved USART protocol according to claim 2, characterized in that, A4 specifically: Specifically, the receiving end starts to cache the data frame after receiving the data frame in the format specified by the USART protocol including the header, and ends the reception of the data frame after receiving the data frame in the format specified by the USART protocol including the tail, thereby obtaining a complete data frame. The receiving end initializes the response sequence number, and attaches the current response sequence number each time a response frame is sent. Each time a data frame is received, it is verified whether the data sequence number in the data frame is the same as the current response sequence number. If they are the same, the data frame is accepted and the response frame is sent, and the response sequence number is incremented. If they are different, the data frame is discarded.
5. A data transmission method based on an improved USART protocol according to claim 1, characterized in that, The transmission error detection and correction mechanism specifically includes the following steps: B1: The sender sends the corresponding data frame to the receiver according to the maintained data sequence number; B2: Start a timer and set a timeout period; B3: If the sender does not receive the response frame sent by the receiver before the timer expires, it is considered that the data frame is lost or the response frame fed back by the receiver is lost and returns to B1 until the sender receives the response frame sent by the receiver. The process of the receiving end sending a response frame is specifically as follows: the receiving end determines whether a data frame is received repeatedly based on the data sequence number in the data frame. If not, the data frame is received, and a response frame with a response sequence number identical to the data sequence number in the data frame is sent to inform the sending end that the data frame has been received; if yes, the repeatedly received data frame is discarded, and a response frame with a response sequence number identical to the data sequence number in the data frame is sent again to inform the sending end that the data frame has been received.
6. A data transmission method based on an improved USART protocol according to claim 1, characterized in that, The data integrity verification mechanism specifically includes the following steps: C1: Select the polynomial used for CRC calculation; C2: According to the number of bits of the selected polynomial, set a CRC register with the same number of bits and assign an initial value to the CRC register; C3: Calculate the CRC checksum for each data segment using the CRC register combined with the selected polynomial; C4: The calculated CRC check code is added to the CRC check code part of the RUTF format data frame and sent to the receiving end; C5: After receiving the data segment and CRC checksum in the RUTF format data frame, the receiver recalculates the CRC checksum based on the received data segment using the selected polynomial and the initial value of the CRC register; C6: The receiving end compares the received CRC check code with the calculated CRC check code. If the two CRC check codes are the same, it means that the data segment has not been tampered with or damaged. Otherwise, it means that the data segment has been tampered with or damaged during transmission.
7. A data transmission method based on an improved USART protocol according to claim 6, characterized in that, C3 is specifically: The sender performs XOR operation on each data bit of the data segment with the most significant bit of the value in the CRC register. If the XOR result is 1, the value in the CRC register is shifted left by one bit and XORed with the selected polynomial. If the XOR result is 0, only the value in the CRC register is shifted left by one bit. After processing all the data bits of the data segment, the value in the CRC register is the calculated CRC check code.
Citation Information
Cited By
United verification method and device of USART and DMA, equipment and medium
CN119962448A
