2014-02-09 26 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

它引发的错误是什么? –