2016-02-28 160 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 '>'(我的字符串的開始和結束)。我正在使用串行監視器打印出值。 –