问题描述
我正在尝试使用stm32 HAL库进行串行rx中断,但出现了我不知道的错误.
Hi guys I am trying to make serial rx interrupt using stm32 HAL library and I got error what I dont know.
这真的很简单. PC将字节提供给stm32板,并且stm32将使用rx中断例程获取这些字节.
It is really simple program. PC gives bytes to stm32 board and stm32 will take those bytes using rx interrupt routine.
问题是当我发送4个字节(例如"12345")时,stm32板只有4个字节,最后一个byte(5)消失了.这是更好理解的图片.
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.
这是我在HAL_UART_RxCpltCallback例程中的代码:
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 :)
推荐答案
检查功能:HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
;
- 使大小足够长,例如16字节;
-
调用
HAL_UART_RxCpltCallback
例程进入函数HAL_UART_Receive_IT(...)
.我建议您像这样添加结束字符(\n
)检测:
- make a size enough longer like 16bytes for example;
call your
HAL_UART_RxCpltCallback
routine into the functionHAL_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++;
}
这篇关于STM32 HAL rx中断无法正确获取字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!