Causes of Data Loss in UART Communication

If you don’t want to miss my updates, remember to check the public account in the upper right corner and mark it as a star. Send me a star!

Causes of Data Loss in UART Communication
Causes of Data Loss in UART Communication
UART: Universal Asynchronous Receiver / Transmitter, commonly referred to as serial port.
The serial port is one of the most commonly used serial peripherals by engineers, but various issues often arise in practical applications. For example: losing a byte of data.
Today, we will discuss UART-related content in conjunction with STM32, as well as the issue of easily losing a byte of data.

Several Status Flags of UART

Here, we will focus on a few status flags in the UART status register:TXE, TC, RXNE, ORE.
Causes of Data Loss in UART Communication
These flags are frequently used in programming, and data loss may occur due to improper operations on them.
TXE: Transmit Data Register Empty
  • 0: Data not transmitted to the shift register
  • 1: Data transmitted to the shift register
TC: Transmission Complete
  • 0: Transmission not complete
  • 1: Transmission complete
RXNE: Read Data Register Not Empty
  • 0: No data received
  • 1: Data ready to be read
ORE: Overrun Error
  • 0: No overrun error
  • 1: Overrun error detected

UART Communication Interfaces

Common UART communication interfaces include: TTL, RS232, RS485. When programming, it is necessary to consider the communication interface method. During long-distance communication, line delays must be considered, as improper handling can also lead to data loss.
1. TTL
TTL is quite simple; it directly connects the Tx and Rx pins of the UART without needing external conversion. As shown:
Causes of Data Loss in UART Communication
Note: The Tx and Rx pins need to be cross-connected.
2. RS232
RS-232 is one of the commonly used standards for serial communication interfaces, specifying that the logic level “1” is -5V to -15V and the logic level “0” is +5V to +15V. The purpose of this electrical standard is to improve anti-interference capability and increase communication distance.
Causes of Data Loss in UART Communication
3. RS485
RS485 typically uses a two-wire connection method, which is a bus topology allowing multiple nodes to be connected on the same bus.
In low-speed, short-distance, interference-free situations, ordinary twisted pairs can be used; conversely, for high-speed, long-distance transmission, RS485 specialized cables with impedance matching (generally 120Ω) must be used; in environments with severe interference, armored twisted shielded cables should also be used.
Causes of Data Loss in UART Communication

Data Loss in UART Reception

Data loss in UART reception may be related to both software and hardware. Below are a few common causes of data loss and their solutions.
1. Overflow Data Loss
This refers to data being lost due to overflow errors caused by failing to retrieve data in time, usually occurring when large amounts of data are received in a polling manner. Data loss can occur during the MCU startup process, when too much data is received and not handled in time, or when complex systems do not respond in time.
Solution:
  • Clear overflow error flags promptly
  • Use communication protocols to filter problems caused by data loss
2. Interrupt Data Loss
Using UART interrupt to receive data is more common than polling. The interrupt method is more responsive than polling, but improper handling can still lead to data loss.
When the data volume is large, UART reception interrupt functions may take too long, or have low priority, leading to data loss.
Solution:
  • Reduce unnecessary time consumption in the interrupt function
  • Allocate interrupt priorities reasonably
  • Clear flags before enabling interrupts
3. Clock Error Leading to Data Loss
In cases of high baud rates, if the clock error increases, it is likely to lead to data loss.
Solution:
  • Use higher precision oscillators
  • Reduce communication baud rate

Data Loss in UART Transmission

Engineers often encounter data loss in UART transmission, usually due to incomplete transmission.
The HAL library has been around for several years, but many engineers still use the standard peripheral library. If the interface is improperly encapsulated, there may be issues with losing the last byte of data during transmission.
1. Incomplete Transmission Leading to Data Loss
The following code only considers non-empty, but the actual transmission is not complete.
void UART_SendByte(uint8_t Data){  while(RESET == USART_GetFlagStatus(USART1, USART_FLAG_TXE));  USART_SendData(USART1, Data);}
However, non-empty does not mean transmission is complete. While this may be more efficient in some cases, it can lead to data loss in others.
For example: After using this function to send, entering sleep mode or shutting down the receiving device’s power can lead to loss.
Solution:
Wait for transmission to complete:
void UART_SendByte(uint8_t Data){  while(RESET == USART_GetFlagStatus(USART1, USART_FLAG_TXE));  USART_SendData(USART1, Data);  while(RESET == USART_GetFlagStatus(USART1, USART_FLAG_TC));}
If using the standard peripheral library, encapsulate functions based on actual conditions, such as sending timeouts.
Or use the HAL encapsulated interface, which includes checks for transmission completion:
HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
2. Line Delay Leading to Data Loss
UART typically uses RS232 or RS485 to increase transmission distance and enhance anti-interference. However, if the data line is too long, transmission delays may occur, especially in long-distance RS485 transmission controlled by an MCU.
Solution:
  • Add delay handling in software
  • Use communication protocols to add acknowledgment mechanisms
3. Other Reasons
UART applications are diverse; some applications in complex factories with high interference can lead to data loss; others in environments with large temperature differences may experience clock drift leading to data loss.
Solutions need to be targeted based on actual situations, such as using better communication lines and implementing fault tolerance in software.
END
Causes of Data Loss in UART Communication
Recommended Reading:

Project Sharing | Electronics Competition Series | Artificial Intelligence | Postgraduate Entrance Examination

Essential Knowledge Points | Graduation Design | Switch Power Supply | Job Seeking

We are Nimo, the founder of Darwin, who only talks about technology without flirting. Darwin is an online education platform aimed at serving professionals in the electronics industry, providing skill training videos covering popular topics in various subfields such as embedded systems, FPGA, artificial intelligence, etc. We offer tailored learning content for different groups, including common knowledge points, disassembly assessments, electronics competitions, intelligent vehicles, and postgraduate entrance examinations. Welcome to follow us.

Official website: www.darwinlearns.com
Bilibili: Darwin
Causes of Data Loss in UART Communication

Leave a Comment