1
我試圖通過作爲字符串打包的uart接收一個數字。我發送數字1000,所以我得到4個字節+空字符。但是當我使用atoi()將數組轉換爲數字並將整數與1000進行比較時,我並不總是得到正確的數字。這是我接收號碼的中斷處理函數。有人有任何想法,有什麼可能是錯的?作爲字符串(uart)的接收號碼
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE))
{
char t = USART1->RDR;
if((t != '\n' && t!='\0') && (cnt < 4))
{
received_string[cnt] = t;
cnt++;
}
else
{
cnt = 0;
}
t = 0;
received_string[4] = 0;
}
if(cnt==4)
{
data = atoi(received_string);
}
}
您需要調試您的代碼。檢查收到的字符以及組成該字符串的內容。 *「我並不總是得到一個正確的數字。」* - 這是一個不完整的觀察,並且表明您在調試時沒有多少努力。 – sawdust
是的,你真的需要在RX緩衝區中發送原始字節(received_string數組)。您可能會發送'\ r \ n'或其他東西,而不僅僅是\ n,這與串行終端程序很相似。 – rost0031