1

I am executing a function in Arduino by giving String input to the serial monitor.

The issue here is when i enter "SHOW$" (without quotes) the function showData() works but when i enter the same "SHOW$" (without quotes) it doesn't gets called. I want to call the function showData() every time i enter the "SHOW$" text into the serial console.

My question here is to what mistake i am doing here, and what is the solution for this.

String strReq = "";
char inChar = 0;

void setup() {

  Serial.begin(9600);

}

void loop() {

  while (Serial.available())
  {
    inChar = Serial.read();

    if (inChar == '$') {

     Serial.println(" Received String :");
     Serial.print(strReq);

     if (strReq.equalsIgnoreCase("SHOW"))
       showData();

     Serial.flush();
     strReq = "";

    } else {

      strReq = strReq + inChar;

    }

  }

}

void showData() {

  Serial.print("Showing Information");

}
  • how is line ending set in Serial Monitor? it is possible that the second time you add line end characters from previous input to strReq – Juraj Jan 8 at 16:18
  • both NL & CR is set – java bee Jan 8 at 16:19
  • then the second input is "\r\nSHOW" and this is not equal to SHOW – Juraj Jan 8 at 16:20
  • right because next time it is printing with new line.. how to solve this.? – java bee Jan 8 at 16:21
  • one option is to ignore control caracters when reading. else if (inChar >= 32) { – Juraj Jan 8 at 16:23
1

The root of your problem seems to be not knowing what Serial.flush() does.

It pre-Arduino 1.0.0 times Serial.flush() used to clear any data in the serial's RX buffer. It would throw it away. In your code that would throw away the line ending \r\n that is sent along with the command.

However in Arduino 1.0.0 it was changed to instead block execution until all bytes had been sent out of the Serial TX buffer. This was deemed more useful, and a replacement to the original functionality is very easy to implement if needed.

This change was not widely documented, but it is mentioned in the manual:

Waits for the transmission of outgoing serial data to complete. (Prior to Arduino 1.0, this instead removed any buffered incoming serial data.)

So with Serial.flush() acting like it does in current versions, you would get a different string the second time around. You would see:

SHOW$
\r\nSHOW$
\r\nSHOW$

and so on.

You have come up with a "fix", which uses String::trim() to discard any leading and trailing non-printable characters, which removes the line-endings from your subsequent strings. However it would be far better to ignore them in the first place and not need to do the trimming.

The simplest way would be to only add characters to your string if they are printable:

if (inChar == '$') {
 Serial.println(" Received String :");
 Serial.print(strReq);
 if (strReq.equalsIgnoreCase("SHOW"))
   showData();
 Serial.flush();
 strReq = "";
} else if (inChar >= ' ') { // << Only add if a space or above
  strReq = strReq + inChar;
}

A simple change, but one which then means you're not having to add the extra processing of trimming unwanted characters from the string.

0

The solution was to use String.trim() function. Thanks guys for giving a quick reply on the question.

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.