Skip to main content
Associate
Oct 27, 2025 1:04 PM UTC
Solved

UART loopback test receives only first character

Hi all,

I have an STM32L071VBT6 and want to test the UART functionality. I configured UART4 RX and TX and physically connected them to do a loopback test. 

I define 2 arrays to start with

/* USER CODE BEGIN PV */
uint8_t Send_buffer[] = "Gateway test";
uint8_t Rec_buffer[13];
/* USER CODE END PV */
 
I start by clearing the Rec_buffer array.
/* USER CODE BEGIN 2 */
  memset(Rec_buffer, 0, sizeof(Rec_buffer));
  /* USER CODE END 2 */
 
Then in my while loop I try to send and receive, but I always just receive the first character ('G' in this case in Rec_buffer[0])
 
 while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */

      // UART TRANSMIT/RECEIVE TEST CODE
      HAL_UART_Transmit(&huart4, Send_buffer, strlen((char*)Send_buffer), 100);
      HAL_UART_Receive(&huart4, Rec_buffer, strlen((char*)Send_buffer), 100);
      HAL_Delay(1000);
  }
  /* USER CODE END 3 */
 
AADW_0-1761570191480.png

Anyone knows why I manage to receive only a single character each time?

Thanks!!

This topic has been closed for replies.
TDK
TDKBest answer
Oct 27, 2025 1:06 PM UTC

There is no automatic buffer. You need to be ready to receive when characters come in, not after.

That means you need to use a non-blocking receive call.

HAL_UART_Receive_IT or HAL_UART_Receive_DMA with appropriate data handling. Called before characters are transmitted.

"If you feel a post has answered your question, please click ""Accept as Solution""."

3 replies

TDK
TDKBest answer
Oct 27, 2025 1:06 PM UTC

There is no automatic buffer. You need to be ready to receive when characters come in, not after.

That means you need to use a non-blocking receive call.

HAL_UART_Receive_IT or HAL_UART_Receive_DMA with appropriate data handling. Called before characters are transmitted.

"If you feel a post has answered your question, please click ""Accept as Solution""."
AADWAuthor
Associate
Oct 27, 2025 1:21 PM UTC

Yes perfect thanks!

TDK
Oct 27, 2025 1:30 PM UTC

Are interrupts enabled and set up?

There's nothing wrong with the code you presented. Issue is elsewhere.

"If you feel a post has answered your question, please click ""Accept as Solution""."