【发布时间】:2012-04-20 10:40:08
【问题描述】:
我发送这样的字符串: $13,-14,283,4,-4,17,6,-240,-180#
但是由于缓冲区“过载”而没有显示,我如何才能接收整个字符串,或者如何在读取每个字节后清除它?
// 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));
【问题讨论】:
-
请熟悉缩进(为你做了一个编辑),为自己选择一个好的编码风格。
-
这是用于微控制器的吗?什么时候出现“缓冲区过载”的错误?
-
看起来 getU2 代码片段可能不完整。我没有看到退货声明。 “buffer”变量的声明是什么?