我正在检查一个数字的文本文件(使用HTTP请求)。我想在每次更改时跟踪这个数字,所以我在字符数组中收集HTTP响应并将其与先前存储的计数进行比较(尝试使用int,现在尝试使用字符数组)但它不会似乎存储结果。 lastCount
是一个全局变量,此函数用于检查和存储新计数:
boolean checkCount()
{
char currentLine[15];
int index=0;
if (client.connected())
{
Serial.println("Connected. Checking for availabilty of client...");
if (client.available())
{
Serial.println("Client is available! Trying to read from client...");
// read incoming bytes:
if(client.find("\r\n\r\n"))
{
char inChar;
while(inChar=client.read())
{
if(inChar==-1) break;
currentLine[index] = inChar; //read in all characters of count
currentLine[index+1] = '\0';
index++;
Serial.print("\n\nCurrent count count: ");
Serial.print(currentLine);
Serial.print("\n\n");
}
if(strcmp(currentLine,lastCount)!=0) //not equal
{
Serial.println("NOT EQUAL TO LAST COUNT");
Serial.print("last count: "); //test output
Serial.print(lastCount);
Serial.print("current count: ");
Serial.println(currentLine);
strcpy(lastCount, currentLine);//, sizeof(lastCount));
Serial.println("BLINKING LED");
blink(3);
}
else
{
Serial.println("EQUAL TO LAST COUNT");
Serial.println("NOT BLINKING.");
}
delay(5000); //delay 5 seconds (don't kill server)
return true;
}
else
{
Serial.println("newlines not found - error with request");
return false;
}
}
else
{
Serial.println("count not available");
return false;
}
}
//...
}
答案 0 :(得分:0)
请参阅以下代码:
你应该把这个'\ 0'放在数组中字符串的末尾。
currentLine [index + 1] ='\ 0';应该从“while循环”中调用此行。 否则strcmp总是只从“currentLine”获得一个字符。
请在存储输入时注意字符串缓冲区的数组长度
while(inChar=client.read())
{
currentLine[index++] = inChar; //read in all characters of count
}
currentLine[index] = '\0';
if(strcmp(currentLine,lastCount)!=0) //no