STM32 HAL rx interrupt can't get bytes correctly

不羁的心 提交于 2020-02-05 06:15:05

问题


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.

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 :)


回答1:


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++;
    }
    


来源:https://stackoverflow.com/questions/37829430/stm32-hal-rx-interrupt-cant-get-bytes-correctly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!