0

I am very new to Arduino Programming. I am trying to identify the input string from serial monitor and printing the output to the console accordingly

Code is :

void setup() {
       Serial.begin(9600);
}

void loop() {

   while (Serial.available() > 0 ) {

     String str = Serial.readString();

     if (str.equals("send")) {
        Serial.println("identified");
     } else {
        Serial.println("unknown");
     }

   }

}

Whenever i put send string it is showing "unknown" as the output, which is wrong, and i should get "identified" as the output. Can anyone guide me here to achieve the result.

Note : I am getting output as i wanted by using readStringUntil function but the strings has a lot of "." in it, hence not useful.

  • what is your line end setting in Serial Monitor? – Juraj Jan 1 '18 at 17:41
  • both NL and CR. – KarmaCoding Jan 1 '18 at 17:44
  • then check for "send/r/n" – Juraj Jan 1 '18 at 17:45
  • 1
    You mean \r\n not /r/n... – Majenko Jan 1 '18 at 18:08
  • 1
    The problem with relying on things like readString is you really don't know what they are doing or how they work. Parse the incoming data manually so you know what it is you are working with. – Majenko Jan 1 '18 at 18:16
1

If you set your Serial Monitor's line ending to "Both NL & CR", then this code will find the word "send". It will NOT find the word "send" in this string, "ssend" or in this string, "sendd".

void setup(){
  Serial.begin(9600);
}

void loop(){
  while(Serial.available() > 0 ){
    String str = Serial.readString();
    if(str.substring(0) == "send\r\n"){
      Serial.println("identified");} 
    else{
      Serial.println("unknown");
    }
  }
}

If you want to find the string "send" in "1234send" or "send1234" or "1234send5678" then use indexOf();

void setup(){
  Serial.begin(9600);
}

void loop(){
  while(Serial.available() > 0 ){
    String str = Serial.readString();
    if(str.indexOf("send") > -1){
      Serial.println("identified");} 
    else{
      Serial.println("unknown");
    }
  }
}
0

There are any number of reasons the *.equals test is false. Consider case difference or extra characters introduced from the source of your communications. Even non printable characters may pose a problem and cause the test to fail.

Consider using *.substring() instead. This method will return true if the substring is found. For example:

  String stringOne = "12345 send 1234";

  // substring(index) looks for the substring from the index position to the end:
  if (stringOne.substring(0) == "send") 
  {
    Serial.println("identified");
  } 
  else
  {
    Serial.println("unknown");
  } 

...should always return "identified" because the characters 's', 'e', 'n' and 'd' are found in the correct sequence within the string stringOne.

  • String str = Serial.readString(); if (str.substring(0) == "send") Serial.println("identified"); else Serial.println("unknown"); this did not work even. – KarmaCoding Jan 1 '18 at 18:03
  • Odd. Did you look at the linked to web page to make sure you were using substring correctly? (This web page?). Also, I see from the comments that you did print it out the received string and you did see "send" in the received message, correct? You may just want to comment out the serial read line and try using "String str = "12345 send 1234";" to test if everything is working. Judging for other comments you might quickly try sending (typing in) "space"+"send"+"space" (" send ") to see if that works. – st2000 Jan 1 '18 at 22:15
  • That won't work. substring(n) just returns the portion of the string starting at position n. So substring(0) just returns a copy of the string. str.substring(0,4) will return the frist 4 characters of the string (assuming there at lest 4 chars to return - I'm not familiar enough with this implementation to know what it does for a shorter string - C# would throw an exception). – dwilliss Dec 1 at 19:26
0
void setup() {
       Serial.begin(9600);
}

void loop() {
   char inChar[5];

   if(Serial.available() > 0 )
   {
     for (int i=0; i<5; i++)
     {
      inChar[i] = Serial.read();
      delay(10);
     }

     if (inChar[0] == 's' && inChar[1] == 'e' \
                          && inChar[2] == 'n' \
                          && inChar[3] == 'd')
         {
          Serial.println("identified");
         }

     else
     {
       Serial.println("unknown");
     }
  }
}
  • Reading the String "send" using Serial.readString() command is fine. But somehow it will not work if you use the result for comparing ( e.g data == "send" ...). A possible solution is to use character array (e.g inChar[5]). The size of the array is 1 more than the maximum characters needed. – Ron Lim Sep 5 at 5:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.