Greetings;
I'm facing a problem with Serial Communication Between Application and Micro Controller.
Intended: The Application sends a Command to Arduino; Arduino Receives the Command; Performs a String Comparison and Performs the Corresponding Action (Let's say Reply Back with Serial.println("some result back")).
Problem:
The Arduino properly receives and transforms the command string; but is unable to perform a String comparison when command is sent by Application.
Under Arduino IDE Serial Monitor; after sending the EXACT same command; the Code performs the comparison without any sort of problem.
It looks like The Arduino "says" that the string received from application is not exactly equal. Yet it is!
Under my Application I tried sending the Command with and without the terminating char \0 or \n.
The same result applies.
I've been stuck for days now and still can't understand why the Comparison result doesn't match a clearly EQUAL String.
Interesting Fact:
I've tried using indexOf("myCommand") instead of a String Comparison and it actually checks the String correctly.
However this will interfere with remaining commands as each one belongs to a specific group (word) and therefore calling "indexOf" will actually call all commands containing the first 3 chars in a command group.
/* Serial Data Configuration */
String inData;
bool hasNewData = false;
char SerialReceivedChars[32];
char endMarker = '\n';
int maxNrChars = 32;
const String GetBoardInfo = "GetBoardInfo";
/* Set User Selected Mode */
void SetMode(String cmd)
{
Serial.println("Received: " + cmd);
if (cmd.equals(GetBoardInfo)) { Serial.println("Command Match"); }
}
/* Listen for Incomming Serial Data from PC */
void SerialListen()
{
static byte ndx = 0; /* Data Received in Bytes */
char receivedChar;
while (Serial.available() > 0 && !hasNewData)
{
receivedChar = Serial.read();
/* While Received Data (Payload) hasn't Received the Terminator Char */
/* Add Received Chars to rc Variable */
if (receivedChar != endMarker)
{
SerialReceivedChars[ndx] = receivedChar;
ndx++;
/* Limit Received Chars to the Maximum Number of Chars. */
/* Whenever a new Char is Received While Maximum Number of Chars is Already Occupied: Replace Last Char with New Char. */
if (ndx >= maxNrChars) { ndx = maxNrChars - 1; }
}
/* else: Terminator Char was Received. */
/* Terminate Number of Defined Characters Variable. */
/* [Note]: In C/C++ a string is an array of char terminated with a NULL byte ('\0'); */
/* Set newData Variable to True. Meaning: New Data Has Been Received. */
else
{
SerialReceivedChars[ndx] = '\0'; /* Terminate the string. Default: '\0' or '\n' */
ndx = 0;
hasNewData = true;
}
if (hasNewData)
{
inData = String((char*)SerialReceivedChars);
SetMode(inData);
hasNewData = false;
inData = "";
}
}
}
/* Component Initialization */
void setup()
{
Serial.begin(115200);
}
/* Loop */
void loop()
{
SerialListen();
}
Any suggestions or thoughts why the Arduino is not performing a String comparison correctly?
Thanks in advance.