cancel
Showing results for 
Search instead for 
Did you mean: 

How to get UART data properly?

dlee.0
Associate II

Hi, I'm using stm32f401RE.

I set UART functions by using CubeMX and I'm trying to get data from UART and distinguish them.

Here is the code.

uint8_t RxBuffer[8]; 

HAL_UART_Receive_IT(&huart2, (uint8_t*)RxBuffer,sizeof(RxBuffer));​ // Writed in while loop

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
			if (huart->Instance == USART2)
      {
          if(RxBuffer[0]=='h') 
					{
							check=1;
					}
     }
}

 And this is the problem. When I set 'uint8_t Rxbuffer[1]' and send 'h' then the above conditional statement react. But when I set 'uint8_t Rxbuffer[8]' then I have to send 'hhhhhhhh' to get reaction. It means I have to send 'h' 8 times. It doesn't work when I send '1234567h' or 'h1234567'.

I can't find why this happens. I'm looking for some helps. Thanks

2 REPLIES 2
Clive1 (HNL)
Senior II

When you tell it to call you back when it has 8 characters do be surprised when it does that.

If there are 8 characters in the buffer, scan all of them, no guarantee what the order/synchronization will be. Obviously in wire order, but might have to wait if the transmission is intermittent.

The interrupt will occur for each character, but you hand that to the HAL rather than manage it yourself.

S.Ma
Principal

Have a look at the Cube library example for the corresponding Nucleo board.

There will be peripheral use case examples there (not directly application useable, so expect to contribute some dev efforts to get what most needs)