Why is this condition returning false?

I am a little confused why my function is returning false, when I should be expecting a true value.

For example, I have defined the following constants, a string variable, and a function to perform a comparison between the string var and the constant:

#define VALID_CMD "AOK"
#define INVALID_CMD "ERR"
String responseBuffer;
bool commandAOK();
bool CLASS::commandAOK()
{
	readBytes(responseBuffer, 100);
	Serial.print(responseBuffer);
	return responseBuffer == VALID_CMD;
}

void CLASS::readBytes(String & buffer, int numberOfBytes)
{
	buffer = "";
	for (int i = 0; i < numberOfBytes; ++i)
		buffer += readByte();
}

However the result of calling commandAOK() is always false… but when I look at the Serial monitor which prints out the “responseBuffer” string, it says “AOK”.

Is the serial monitor hiding other characters that may be in the string, and if so, how can I view them, or is there something else going on?

Comparing strings is more complicated than that. This is C, not some wimp language.

I don’t know off the top of my head how to do it with the String class - I avoid String like the plague (for the reasons described at length in almost every thread where String is mentioned) and use c strings (ie fixed length char arrays) instead, and for those, you use strcmp() to compare strings.

DrAzzy:
Comparing strings is more complicated than that. This is C, not some wimp language.

I don't know off the top of my head how to do it with the String class - I avoid String like the plague (for the reasons described at length in almost every thread where String is mentioned) and use c strings (ie fixed length char arrays) instead, and for those, you use strcmp() to compare strings.

Well technically... this is c++, and to my understanding the (Arduino) String class has implemented the operator ==... I am aware of how string comparison works, I have add to create String classes by stratch.
https://www.arduino.cc/en/Tutorial/StringComparisonOperators. So, that is the correct way to compare string...

I would also not go so far and call other languages "wimp" languages, just because the allow for more elegant code and garbage collection... and sure cstrings are great if you know the size of the data ahead of time... but would not be ideal when the size of the data is unknown, thereby creating creating extra overhead when needing to resize the string... also cstring need to be null terminated, and increase the complexity of the code.

Yes == is implemented

may be that's why?

String aString;
#define VALID_CMD "this"
#define BAD_VALID_CMD "this\r\n"

void setup() {
  aString = String("this");

  Serial.begin(115200);

  if (aString == "this") {
    Serial.println("aString is == \"this\"");
  }

  if (aString == VALID_CMD) {
    Serial.println("aString is == VALID_CMD");
  }

  if (aString == BAD_VALID_CMD) {
    Serial.println("aString is == BAD_VALID_CMD");
  }
}

void loop() {}

would suggest you do

Serial.print("[");
Serial.print(responseBuffer);
Serial.print("]");

and also check the length of responseBuffer to see if it's 3 chars

also I don't understand what readByte() is in buffer += readByte();

are you reading 100 bytes? what if there is nothing to read? what do you add at the end of the buffer?

readbyte is one liner function:

char Bluetooth::readByte()
{
	return bluetooth.available() ? (char)bluetooth.read() : 0;
}

I did a serial print, as shown in the original post, but I am going to try checking the length like you suggested!

johnnydicamillo:
readbyte is one liner function:

char Bluetooth::readByte()

{
return bluetooth.available() ? (char)bluetooth.read() : 0;
}

and you cal lit that way:

	buffer = "";
	for (int i = 0; i < numberOfBytes; ++i) buffer += readByte();

you understand that with this code you won’t read 100 chars, right (as numberOfBytes could let suppose you intend to)? you will only try 100 times to read super quickly the bluetooth serial port which does not guarantee you get your input string in full or at all if you are reading too fast compared to data being available?

And on why we don’t like Strings too much on Arduino - read this post for example

J-M-L:
and you cal lit that way:

	buffer = "";
for (int i = 0; i < numberOfBytes; ++i) buffer += readByte();



you understand that with this code you won't read 100 chars, right (as numberOfBytes could let suppose you intend to)? you will only try 100 times to read super quickly the bluetooth serial port which does not guarantee you get your input string in full or at all if you are reading too fast compared to data being available?

And on why we don't like Strings too much on Arduino - [read this post](https://hackingmajenkoblog.wordpress.com/2016/02/04/the-evils-of-arduino-strings/) for example

Sure, it would be too fast to read, but that is assuming I haven’t called any delay functions preceding the call to my read bytes function, when in fact, I have a print and println function which blocks the program appropriately to ensure the data is there. It is not a question if the data is being stored in the buffer… I already know it is there when I did a serial print. It was more of a question of whether other characters are in the string.