Skip to main content
Associate
October 27, 2025
Solved

UART loopback test receives only first character

  • October 27, 2025
  • 1 reply
  • 296 views

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!!

Best answer by TDK

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.

1 reply

TDK
TDKBest answer
Super User
October 27, 2025

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
October 27, 2025

Yes perfect thanks!

TDK
Super User
October 27, 2025

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""."