Arduino上的串行通信

时间:2015-12-07 18:47:54

标签: arduino communication

我有一个学校的作业,我需要打开带有#ON%的串行消息的led,然后用#OFF%关闭指示灯。 #和%是正确字符串的标识符。所以我制作了这段代码:

(bericht表示荷兰语的留言)

String readString = "";
int recievedCharacter;
String bericht = "";

int ledPin = 6;


void setup() {
  Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
 pinMode(ledPin, OUTPUT);
}

void loop() 
{

  while (Serial.available() > 0)
  { 
    delay(4);    
    char readChar = (char) Serial.read(); // 'Convert' to needed type
    bericht += + readChar;         // concatenate char to message

  }

  if(bericht.startsWith("#"))
  {

    if(bericht == "#ON%")
   {
      Serial.println(bericht);
      Serial.println("goed"); 
      digitalWrite(ledPin, HIGH);
      //message = "";
    }



    if(bericht == "#OFF%")
    {
      Serial.println("goed"); 
      digitalWrite(ledPin, LOW);
      //message = "";
    }
  }
}

问题是程序永远不会进入if(bericht ==" #ON%")部分......

很抱歉,如果这是一个愚蠢的问题,但有很多谷歌搜索,我只是无法弄清楚...

1 个答案:

答案 0 :(得分:2)

问题在于:

bericht += + readChar;         // concatenate char to message // XXX '+ char' => int

这实际上会在消息中附加一个整数。删除+

bericht += readChar;         // concatenate char to message // Goed!
相关问题