Go Down

Topic: Serial.readString() issue (Read 100 times) previous topic - next topic

trentmahaffey

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!

Code: [Select]


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;
    }
 

   

  }

}

GolamMostafa

#1
Sep 22, 2019, 06:57 am Last Edit: Sep 22, 2019, 06:58 am by 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)
Code: [Select]
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:

Robin2

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
Two or three hours spent thinking and reading documentation solves most programming problems.

TheMemberFormerlyKnownAsAWOL

#3
Sep 22, 2019, 10:38 am Last Edit: Sep 22, 2019, 10:39 am by TheMemberFormerlyKnownAsAWOL
Code: [Select]
char myData[50];
...
byte x = Serial.readBytesUntil('\n', myData, 50);//data = Serial.readString(); //read string
...
myData[x] = '\0';
 
Oops.

trentmahaffey

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)
Code: [Select]
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:

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 :)

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.

TheMemberFormerlyKnownAsAWOL

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 :)


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

GolamMostafa

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 :)
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).


sterretje

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"
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Go Up