cancel
Showing results for 
Search instead for 
Did you mean: 

UART loopback test receives only first character

AADW
Visitor

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

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Super User

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

View solution in original post

3 REPLIES 3
TDK
Super User

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

Yes perfect thanks!

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