serialEvent() string reconstruction and interpreting issue.

Hello,

For my application I am communicating to a motor driver via the serial monitor by sending strings. To do this I am using a serialEvent() function to build the string before sending it off to Serial3. It looks something like this.

void serialEvent() {
  while (Serial.available()) {
    char inChar = (char)Serial.read();
    Command += inChar;
    if (inChar == '\n') 
      commandComplete = true;
  }
}

void loop() {
  if (commandComplete) {
      Serial3.print(Command); 
    }
    Command = "";
    commandComplete = false;
  }
}

I am having an issue whereby I want to interpret the string and if the string is required to something (someAction), it does, else it sends the command to Serial3. Something like:

  if (commandComplete) {
    if (Command == ("someAction") {
      //do someAction
    } else {
      Serial3.print(Command); 
    }
    Command = "";
    commandComplete = false;
  }

The problem is that the code (possibly the string reconstruction) cannot interpret the "someAction" string and immediately sends it off to Serial3. I am sure this problem has to do with data structure but I cannot figure it out. I also tried adding a line feed, but that is not working.

  if (commandComplete) {
    if (Command == ("someAction" + '\n') {
      //do someAction
    } else {
      Serial3.print(Command); 
    }
    Command = "";
    commandComplete = false;
  }

It looks something like this.

Useless code ignored.

Something like:

More useless code ignored.

Post your real code.

    if (Command == ("someAction") {

(Why) (is) ("someAction") (in) (parentheses) (?)

Also, we don't know if you're using the String class (often unnecessary) or a character array for strings, but this statement:

if (Command == ("someAction") {

is not the way to compare strings. Check out strcmp(). Post all of you code so we can see the data types and other structures you are using.

while (Serial.available()) {
    char inChar = (char)Serial.read();
    Command += inChar;
    if (inChar == '\n') 
      commandComplete = true;
  }

You are adding the '\n' into Command and it is not included in "someAction"

And you are not adding a terminating 0 to mark the end of the received command.

Perhaps something like this

while (Serial.available()) {
    char inChar = (char)Serial.read();
    if (inChar == '\n') 
      Command += 0;
      commandComplete = true;
   }
   else {
      Command += inChar;
    }
}

...R

If the message is to be terminated with the newline character, why not use something like

#define MESSAGESIZE  20   // Whatever the size needs to be
char message[MESSAGESIZE  + 1];   // Room for terminating null

// setup() and loop() code...

// Code in loop()
int charsRead;

while (Serial.available() > 0) {
    charsRead = Serial.readBytesUntil('\n', message, MESSAGESIZE);
    message[charsRead] = '\0';   // Make it a string...
    commandComplete = true;
}
// rest of code
while (Serial.available() > 0) {
    charsRead = Serial.readBytesUntil('\n', message, MESSAGESIZE);
    message[charsRead] = '\0';   // Make it a string...
    commandComplete = true;
}

while?