Serial read char string stops reading?

Hello, I am trying to read entire strings from the serial monitor, but it doesn’t work as I want it to work.

When I type in lock, unlock, or test, which are in the if statements in the loop, it works, but when I make one typo, or type another word, it completely stops reading the Serial Monitor input. As if the code terminates itself if I do not type the same words as stated in the if statements.

I want the code to work even if I make one typo, or write another word that is not in the if statements. So when I make one typo, I still want to be able to use lock, unlock, or test to send to the Arduino.

Is the code below even the correct way to read Strings from the Serial Monitor input?

Code (code from StackOverflow)

#define BAUD (9600)

// serial read in data
char inData[20]; // Allocate some space for the string
char inChar=-1; // Where to store the character read
byte index = 0; // Index into array; where to store the character


void setup() 
{
  // baud
  Serial.begin(BAUD);
}

// read serial into string
char Comp(char* This) {
    while (Serial.available() > 0) // Don't read unless
                                   // you know there is data
    {
        if(index < 19) // One less than the size of the array
        {
            inChar = Serial.read(); // Read a character
            inData[index] = inChar; // Store it
            index++; // Increment where to write next
            inData[index] = '\0'; // Null terminate the string
        }
    }
    
    if (strcmp(inData,This)  == 0) {
        for (int i=0;i<19;i++) {
            inData[i]=0;
        }
        index=0;
        return(0);
    }
    else {
        return(1);
    }
}

void loop()
{
    if (Comp("unlock")==0) {
        Serial.write("Unlocking\n");
        unlock();
    }
    if (Comp("lock")==0) {
        Serial.write("Locking\n");
        lock();
    }
    if (Comp("test")==0) {
        Serial.write("Locking\n");
        test.test();
    }
}

but when I make one typo, or type another word, it completely stops reading the Serial Monitor input.

No, it doesn’t. But, you’ve provided no way to wipe the mistaken data from inData. The only way that inData ever gets reset is when a match is found.

Is the code below even the correct way to read Strings from the Serial Monitor input?

Not really. It assumes that you will never make a mistake and that you will never send anything that is not part of the payload (no carriage returns, no line feeds, etc.).

Take a look at https://forum.arduino.cc/index.php?topic=288234.0 for how to properly send/receive serial data.

It seems to me that the first call to Comp will consume whatever comes into the serial, and the two Comp calls which follow could easily miss "their own strings".

I think you need to read in the serial data into a buffer once, then do compares on the buffer.

I've just had a look at the Stackoverflow link you mentioned. And one of the replies says the same thing as I do:

"This shouldn't work. The second "if" statement inside the loop will never trigger because you've already read the serial data from the first "if" comparison."

PaulS:
No, it doesn't. But, you've provided no way to wipe the mistaken data from inData. The only way that inData ever gets reset is when a match is found.
Not really. It assumes that you will never make a mistake and that you will never send anything that is not part of the payload (no carriage returns, no line feeds, etc.).

Take a look at Serial Input Basics - Programming Questions - Arduino Forum for how to properly send/receive serial data.

Awesome link thank you for that. Will read and adapt.

giovanniguerra:
I've just had a look at the Stackoverflow link you mentioned. And one of the replies says the same thing as I do:

"This shouldn't work. The second "if" statement inside the loop will never trigger because you've already read the serial data from the first "if" comparison."

Yes I noticed that it sometimes even kills the process even if I did not make any typo. Not a reliable code. Will read PaulS link on how to correctly implement it.


Thanks for the replies

Very simple example of capture and compare.

// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

int ledPin = 13;
String readString;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT); 
  Serial.println("serial on/off test 0021"); // so I can keep track
}

void loop() {

  while (Serial.available()) {
    delay(3);  
    char c = Serial.read();
    readString += c; 
  }

  if (readString.length() >0) {
    Serial.println(readString);

    if (readString == "on")     
    {
      digitalWrite(ledPin, HIGH);
      Serial.println("LED ON");
    }
    if (readString == "off")
    {
      digitalWrite(ledPin, LOW);
      Serial.println("LED OFF");
    }
    readString="";
  } 
}

Hello
your post is very helpful and it solved my problem. Thanks. I am beginner in Arduino.
I have Nextion LCD. When I press button on LCD it will send 0x65 0x00 0x06 0x01 0xff 0xff 0xff to the seral port & when i will press again it will send 0x65 0x00 0x06 0x00 0xff 0xff 0xff to the serial port. I want to switch ON and OFF an LED but how to do? Can you write code for it? Please help me. My email is hiimran81@gmail.com
Please send through email as well

hiimran81:
Hello
your post is very helpful and it solved my problem. Thanks. I am beginner in Arduino.
I have Nextion LCD. When I press button on LCD it will send 0x65 0x00 0x06 0x01 0xff 0xff 0xff to the seral port & when i will press again it will send 0x65 0x00 0x06 0x00 0xff 0xff 0xff to the serial port. I want to switch ON and OFF an LED but how to do? Can you write code for it? Please help me. My email is hiimran81@gmail.com
Please send through email as well

We don't send answers by email. Just come back here and read the answers.

In any case, as you have not posted your program I have no idea what the problem might be and consequently I have no answer.

...R