2014-02-09 29 views
0

我有以下代碼:開始在處理工作和字符串不比較

String myString = port.readStringUntil(linefeed); 

    if (myString != null) { 
    print(myString); 
    if (myString.equals("SndEprom")) { 
     sending = true; 
     print("sending set true"); 
    } 

當代碼運行,這是什麼日誌顯示:

SndEprom 
0,255 
1,255 
2,255 
3,255 
4,255 
5,255 
6,255 
.... 

我會認爲line

print(「sending set true」);

已經運行。我做錯了什麼?

感謝,

羅蘭

代碼Arduino的發送EEPROM數據:

在主循環:

if (strcmp(inData, "read") == 0){ 
    Serial.println("SndEprom"); 
    delay(50); 
    sendProm(); 
} 

void sendProm(){ 
    for (int i=0; i <= 100; i++){ 
    // read a byte from the current address of the EEPROM 
    value = EEPROM.read(address); 

    Serial.print(address); 
    Serial.print(","); 
    Serial.print(value, DEC); 
    Serial.println(); 

    // advance to the next address of the EEPROM 
    address = address + 1; 

    // there are only 512 bytes of EEPROM, from 0 to 511, so if we're 
    // on address 512, wrap around to address 0 


    delay(15); 
    } 
    address = 0; 
} 
+2

你確定'myString'等於' 「SndEprom」'?檢查它是否有尾隨空格。 – Christian

+0

try myString.trim()。equals(「SndEprom」) –

+0

當我嘗試了你的建議時,檢查'myString'是否與'print(「[」+ myString +「]」)' – wolfrevo

回答

1

正如你在評論中說 - 字符串你實際上得到的是「SndEprom」 - 注意最後的空間。

要解決此問題,使用:

myString.trim().equals("SndEprom") 
+0

引發錯誤.... –

+0

好吧我真的不知道,它怎麼會產生這樣的錯誤.. –

+0

它引發的錯誤是什麼? –