Search code examples
cavrstm32nodemcu

STM32 HAL rx interrupt can't get bytes correctly


Hi guys I am trying to make serial rx interrupt using stm32 HAL library and I got error what I dont know.

It is really simple program. PC gives bytes to stm32 board and stm32 will take those bytes using rx interrupt routine.

Problem is when i send over 4 bytes ,such as "12345", stm32 board only got 4 bytes and last one byte(5) is gone somewhere. Here is picture for better understanding.

enter image description here

Here is my code in HAL_UART_RxCpltCallback routine:

HAL_UART_Transmit(&huart4, &receive1, 1, 1000);
HAL_UART_Receive_IT(&huart4, &receive1, 1);

If you have any idea, please comment :)


Solution

  • check the function: HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);

    1. make a size enough longer like 16bytes for example;
    2. call your HAL_UART_RxCpltCallback routine into the function HAL_UART_Receive_IT(...). I suggest to you to add an end character (\n) detection like this:

      if ( (*huart->pRxBuffPtr) == '\n') {
          HAL_UART_RxCpltCallback(huart);  //-------------------------------------------/////////
      } else {
          huart->pRxBuffPtr++;
      }