Serial.readString() issue

Hello everybody. So Ive browsed a lot on here already with problems regarding strings with the arduino, and I've viewed the intro to serial comms that people like to refer to here. My main question is, when I use the if statement to evaluate if two strings are the same (if data==username), it doesn't evaluate true, even though I have the serial monitor print out both strings right before and they are the same exact string!

const int GLED=11;
const int RLED=9;
String data; //holds incoming string
String username;
int i = 0;

void setup()
{
  Serial.begin(9600); //Serial Port at 9600 baud
  pinMode(GLED, OUTPUT);
  pinMode(RLED, OUTPUT);
  username = String("tmahaffey");
}

void loop()
{

  
  if (i <1)
  {
    Serial.println("Please enter your username or ID");
    i++;
  }
 

 
  if (Serial.available() > 0)
  {
    data = Serial.readString(); //read string
    Serial.println("Seraial Read String is: " + data);
    Serial.println("Username String is: "+ username);

    if (data == username)
    {
      Serial.println("Hello " + username);
      digitalWrite(GLED, HIGH); 
      delay(5000);
      digitalWrite(GLED, LOW);
      i = 0;
    }
    else if (data != username)
    {
      Serial.println("Invalid Username");
      digitalWrite(RLED, HIGH);
      delay(5000);
      digitalWrite(RLED, LOW);
      i =0;
    }
  

    

  }

}

I have modified your sketch 'a bit' to replace the String by cstring; because, the Arduino Forum does not like the use of String construct.

Codes (slightly modifies)

const int GLED = 11;
const int RLED = 9;
char myData[50];//String data; //holds incoming string
char username[] = "tmahaffey";//String username;
int i = 0;

void setup()
{
  Serial.begin(9600); //Serial Port at 9600 baud
  pinMode(GLED, OUTPUT);
  pinMode(RLED, OUTPUT);
  //  username = "tmahaffey";//String("tmahaffey");
}

void loop()
{
  if (i < 1)
  {
    Serial.println("Please enter your username or ID");
    i++;
  }

  if (Serial.available() > 0)
  {
    byte x = Serial.readBytesUntil('\n', myData, 50);//data = Serial.readString(); //read string
    //Serial.println("Seraial Read String is: " + data);
    myData[x] = '\0'; //adding null caharcater
    Serial.print("Username string is: ");//+ username);
    Serial.println(myData);//
    if (strcmp(username, myData) == 0)//(data == username)
    {
      Serial.print("Hello ");// + username);
      Serial.println(username);
      digitalWrite(GLED, HIGH);
      delay(5000);
      digitalWrite(GLED, LOW);
      i = 0;
    }
    else //if (data != username)
    {
      Serial.println("Invalid Username");
      digitalWrite(RLED, HIGH);
      delay(5000);
      digitalWrite(RLED, LOW);
      i = 0;
    }
  }
}

Screen shot:
smUser.png

smUser.png

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

...R

char myData[50];
...
byte x = Serial.readBytesUntil('\n', myData, 50);//data = Serial.readString(); //read string
...
myData[x] = '\0';

Oops.

GolamMostafa:
I have modified your sketch 'a bit' to replace the String by cstring; because, the Arduino Forum does not like the use of String construct.

Codes (slightly modifies)

const int GLED = 11;

const int RLED = 9;
char myData[50];//String data; //holds incoming string
char username[] = "tmahaffey";//String username;
int i = 0;

void setup()
{
  Serial.begin(9600); //Serial Port at 9600 baud
  pinMode(GLED, OUTPUT);
  pinMode(RLED, OUTPUT);
  //  username = "tmahaffey";//String("tmahaffey");
}

void loop()
{
  if (i < 1)
  {
    Serial.println("Please enter your username or ID");
    i++;
  }

if (Serial.available() > 0)
  {
    byte x = Serial.readBytesUntil('\n', myData, 50);//data = Serial.readString(); //read string
    //Serial.println("Seraial Read String is: " + data);
    myData[x] = '\0'; //adding null caharcater
    Serial.print("Username string is: ");//+ username);
    Serial.println(myData);//
    if (strcmp(username, myData) == 0)//(data == username)
    {
      Serial.print("Hello ");// + username);
      Serial.println(username);
      digitalWrite(GLED, HIGH);
      delay(5000);
      digitalWrite(GLED, LOW);
      i = 0;
    }
    else //if (data != username)
    {
      Serial.println("Invalid Username");
      digitalWrite(RLED, HIGH);
      delay(5000);
      digitalWrite(RLED, LOW);
      i = 0;
    }
  }
}





**Screen shot:**
![smUser.png|607x353](upload://k27jYSj6KoF6GK9P1TCsGUg2z0x.png)

Thank you Mostafa... You say the forum doesn't like the string construct, but it appears that the arduino doesn't like it if you ask me :slight_smile:

Seemed strange to me. Both in my example both strings are displayed identical in the monitor but they don't evaluate to true. I'm sure it's not easy to explain, and I probably won't understand the explanation, but when two strings are identical, how can that if statement not evaluate to true?

I see the cstring is a good workaround, but that appears to be what it is. Such a more difficult way to get what should be an easy evaluation done. Thanks for listening to my rant.

trentmahaffey:
Thank you Mostafa... You say the forum doesn't like the string construct, but it appears that the arduino doesn't like it if you ask me :slight_smile:

No, the forum doesn't like the String class.
We don't mind strings at all.

trentmahaffey:
Thank you Mostafa... You say the forum doesn't like the string construct, but it appears that the arduino doesn't like it if you ask me :slight_smile:

GolamMostafa:
I have modified your sketch 'a bit' to replace the String by cstring; because, the Arduino Forum does not like the use of String construct.

@GolamMostafa has said that the Arduino Forum does not like the use of String (with capital S) construct (the Programming Language Element) and not the string (with small s).

trentmahaffey:
Seemed strange to me. Both in my example both strings are displayed identical in the monitor but they don't evaluate to true. I'm sure it's not easy to explain, and I probably won't understand the explanation, but when two strings are identical, how can that if statement not evaluate to true?

What you see is not always what you actually got. When you send data using serial monitor, it might add a CR ('\r') and/or LF character ('\n'). Check the line-ending in serial monitor.

Yours is set to newline (LF) so "tmahaffey" is not the same as "tmahaffey\n"