2016-02-28 115 views
0

我无法从GET响应中提取需要的数据 我只想记住介于“ - ”和“ - ”之间的charcaters,因为我不需要其他任何东西。我试图用一个while循环,如下图所示,但它不会工作解析GET响应

谢谢

String readString; 

    while (client.connected() || client.available()) { 
     char c = client.read(); //gets byte from ethernet buffer 

     while(c != "-"); //this throw error 
     readString += c; //places captured byte in readString 
    } 

    client.stop(); //stop client 

    Serial.print(readString); 

    String res = readString.substring("-","-"); //throw error 
    String val = "happy"; 

    if(res == val){ 
     Serial.print(res); 
     Serial.println(" happy"); 
    }else{ 
     Serial.print(res); 
     Serial.println(" sad"); 
    } 

    Serial.println("=================="); 
    Serial.println(); 
    readString=""; //clear readString variable 

回答

0

好了,在这里,你有不同的问题。

  1. 您不能比较字符(c)和字符串("-")。您应该使用'-'与char进行比较。
  2. 虽然在你的循环将阻止程序执行。你应该使用一种状态机(例如使用一个状态变量)。
  3. 如果您正确书写阅读功能,则无需使用substring函数。在任何情况下,substring函数都需要INDICES,而不是字符。

这就是说,该代码应工作你的情况:

// state = 
// 0 -> waiting for first - 
// 1 -> reading the answer and waiting for second - 
// 2 -> finished reading 
uint8_t state = 0; 

while (client.connected() || client.available()) { 
    char c = client.read(); //gets byte from ethernet buffer 

    if ((c == '-') && (state < 2)) 
     state++; 
    if ((state == 1) && (c != '-')) 
     readString += c; 
} 

client.stop(); //stop client 

Serial.print(readString); 
String val = "happy"; 

if(res == val){ 
    Serial.print(readString); 
    Serial.println(" happy"); 
}else{ 
    Serial.print(readString); 
    Serial.println(" sad"); 
} 

不管怎样,我真的不喜欢的Arduino使用可变大小的变量,因为你有很少的内存(为什么你应该浪费?)。我的建议是总是使用固定大小的字符串(又名字符数组)。例如:

// state = 
// 0 -> waiting for first - 
// 1 -> reading the answer and waiting for second - 
// 2 -> finished reading 
uint8_t state = 0; 

char readString[MAX_SIZE+1]; 
uint8_t readStringIndex = 0; 

while (client.connected() || client.available()) { 
    char c = client.read(); //gets byte from ethernet buffer 

    if ((c == '-') && (state < 2)) 
     state++; 
    if ((state == 1) && (c != '-')) 
    { 
     readString[readStringIndex] = c; 
     readStringIndex++; 
    } 
} 
readString[readStringIndex] = '\0'; // string terminator 

client.stop(); //stop client 

Serial.print(readString); 

if(strcmp(readString, "happy")){ 
    Serial.print(readString); 
    Serial.println(" happy"); 
}else{ 
    Serial.print(readString); 
    Serial.println(" sad"); 
} 

编辑:

的OP提到,字符串不是由两个分隔了“ - ”,而是他们“<”和“>”之间封闭。

的代码,因此,应该以这种方式修改:

// state = 
// 0 -> waiting for first - 
// 1 -> reading the answer and waiting for second - 
// 2 -> finished reading 
uint8_t state = 0; 

char readString[MAX_SIZE+1]; 
uint8_t readStringIndex = 0; 

while (client.connected() || client.available()) { 
    char c = client.read(); //gets byte from ethernet buffer 

    switch (state) 
    { 
     case 0: // Waiting for first char 
      if (c == '<') 
       state = 1; 
      break; 
     case 1: // Reading the answer and waiting for second char 
      if (c == '>') 
       state = 2; 
      else 
      { 
       readString[readStringIndex] = c; 
       readStringIndex++; 
      } 
      break; 
    } 
} 
readString[readStringIndex] = '\0'; // string terminator 

client.stop(); //stop client 

Serial.print(readString); 

if(strcmp(readString, "happy")){ 
    Serial.print(readString); 
    Serial.println(" happy"); 
}else{ 
    Serial.print(readString); 
    Serial.println(" sad"); 
} 
+0

你好,谢谢你的代码工作,但有几个问题:字符串包含“<”开头和arduino崩溃(litterally),并经过一段时间重新启动它自我从setup()。我从来没有听说过这种行为,你有什么想法可以造成这种情况?谢谢 –

+0

@SkaFrenz你使用第一或第二代码?所以基于字符串或char数组的基础?至于'<',你解析的完整字符串是什么?有一个<第一个之后 - ?请注意,如果您使用的是浏览器来查看服务器响应,也许<未显示(在这种情况下请查看页面源代码 - chrome上的CTRL-U或其他浏览器上的其他快捷方式) – frarugi87

+0

嗨, m使用第二个(数组)。对不起,我没有提到这一点,但我改变' - '与'<' and '>'(我的字符串的开始和结束)。我正在使用串行监视器打印出值。 –