2012-11-14 150 views
-1

我想比較一個爆炸字符串與Arduino中的另一個字符串,但它不會工作。從串口讀取被分割的字符串。Arduino - 比較一個拆分字符串與另一個字符串

首先,這些都是我用爆炸字符串的函數:

int count_delimiters(char str[], const char* delimiters) { 
    int i, j, result = 0; 
    for (i = 0; i < strlen(str); ++i) { 
     for (j = 0; j < strlen(delimiters); ++j) { 
      if (str[i] == delimiters[j]) { 
       ++result; 
      } 
     } 
    } 
    return (result + 1); 
} 

char** split(char str[], const char* delimiters) { 
    int result_size = count_delimiters(str, delimiters); 
    int i = 0; 
    char* result[result_size]; 
    char* pch = strtok(str, ","); 

    while (pch != NULL) 
    { 
     result[i] = pch; 
     pch = strtok(NULL, ","); 
     ++i; 
    } 

    return result; 
} 

,我嘗試了爆炸字符串比較另一個字符串的部分看起來像這樣:

char input_array[input.length()]; 
input.toCharArray(input_array, (input.length() + 1)); 
exploded = split(input_array, ","); 

if ("$test" == exploded[0]) { 
    Serial.println("match"); // This code is never reached. 
} 

當我輸入$ test時,串口監視器中的其他我希望打印出一個匹配,但不打印任何內容。如果我做Serial.println(exploded[0]);,它應該輸出$ test。我究竟做錯了什麼?

我已經試圖尋找非打印字符,例如\r\n\0,但它似乎並沒有包含任何的這些,因爲當我檢查「$測試\ R」或旁人仍然不會返回真實。

回答

1

在這一行:

if ("$test" == exploded[0]) 

使用STRCMP,而不是由==比較。

相關問題