我發送這樣的字符串正確接收的字符串: $ 13 -14,283,4,-4,17,6,-240,-180#如何從UART
但並不是因爲顯示出來緩衝區是'超載',我怎樣才能接收整個字符串,或者如何在每次讀取字節後清除它?
// get a character string
char *getsU2(char *s, int len) {
char *p = s; // copy the buffer pointer
do {
*s = getU2(); // get a new character
if ((*s=='\r') || (*s=='\n')) // end of line...
break; // end the loop s++;
// increment the buffer pointer
len--;
} while (len>1); // until buffer is full
*s = '\0'; // null terminate the string
return p; // return buffer pointer
}
// get a character string
char *getsU2(char *s, int len) {
char *p = s; // copy the buffer pointer
do {
*s = getU2(); // get a new character
if ((*s=='\r') || (*s=='\n')) // end of line...
break; // end the loop
s++;
// increment the buffer pointer
len--;
} while (len>1); // until buffer is full
*s = '\0'; // null terminate the string
return p; // return buffer pointer
}
char getU2(void) {
if(U2STAbits.OERR == 1)
{ U2STAbits.OERR = 0; }
while (!U2STAbits.URXDA); // wait for new character to arrive return U2RXREG;
// read character from the receive buffer }
getsU2(buffer,sizeof(buffer));
請熟悉縮進(爲您進行編輯),選擇自己良好的編碼風格。 – 2012-04-20 12:28:38
這是用於微控制器嗎?你什麼時候得到關於「緩衝區過載」的錯誤? – 2012-04-20 13:21:40
它看起來像getU2代碼片段可能不完整。我點沒有看到一個返回聲明。什麼是「緩衝區」變量的聲明? – user957902 2012-04-20 15:28:45