AnsweredAssumed Answered

How are you supposed to use HAL_UART_Receive in STM32Cube ?

Question asked by hedlund.johan on Nov 25, 2014
Latest reply on Nov 28, 2014 by perez.diego
I am implementing a command parser that receives data on a serial port. My need is to read one byte at a time and when I recive a full command I will handle it.

I look at the example in STM32Cube_FW_L0_V1.1.0\Projects\STM32L053R8-Nucleo\Examples\UART\UART_TwoBoards_ComPolling\Src\main.c

In line 152:

  if(HAL_UART_Receive(&UartHandle, (uint8_t *)aRxBuffer, RXBUFFERSIZE, 5000) != HAL_OK)
  {
    Error_Handler();  
  }

I have modified this to lower the timeout value and set RXBUFFERSIZE to 1 and call the function in a loop.

However I think the original code is not a good example since if the function HAL_UART_Recive gets timeout the function will return HAL_TIMEOUT and the code will end up in the Error_Handler with a while(1) loop.

If just ignoring this by not calling Error_Handler when getting HAL_TIMEOUT and call the function HAL_UART_Recive once more it will return HAL_BUSY. And it seems like no way to get out of this. So I think this example is broken if it would serve as an example on how to use HAL_UART_Receive with a timeout.

So if possible if someone could share with me a code snippet where you can read bytes once at a time in a loop from UART with correct error handling I would be happy.









Outcomes