strcmp isn't working

Hey!

So I'm sitting here scratching my head a bit because I can't figure out what I've done wrong when using strcmp.
I've read a few threads regarding the same problem, but none of the solutions have been applicable here.

This is the code:

symbolLength = rxBufString.length();
rxBufString.substring(9, symbolLength).toCharArray(symbolCompare, symbolLength);
Serial.println(symbolCompare);
if (strcmp(symbolCompare, "P_Manifold") == 0) {
  debug = 1;
  P_Manifold = rxBufString.substring(0, 5);
}

rxBufString is sent from the ECU in my car via CAN bus.
I'm getting the length of the String to be able to determine if the String is exactly P_Manifold, and not something that contains P_Manifold.
I'm removing the first 8 characters of rxBufString because they can't be used here.
The toCharArray seems to be working fine since I can see that symbolCompare = "P_Manifold" when printed to the serial monitor.
debug is never set to 1 and P_Manifold is never set to rxBufString.substring(0, 5).

Like I said, I print symbolCompare to the serial monitor and can see that it is in fact exactly "P_Manifold", so why isn't it working?

Any help is highly appreciated!

Regards,
Christian

Yeah. According to what you say, there can be no problem.

I print symbolCompare to the serial monitor and can see that it is in fact exactly "P_Manifold"

Try

Serial.print(">");
Serial.print(symbolCompare);
Serial.println("<");

What exactly does the output look like ?

rxBufString.substring(9, symbolLength).toCharArray(symbolCompare, symbolLength);

The second argument to the toCharArray() is NOT the length of the String. It is the size of the buffer to be written to. Do you have a large enough array?

UKHeliBob:
Try

Serial.print(">");

Serial.print(symbolCompare);
Serial.println("<");




What exactly does the output look like ?

Great tip! The output is

>P_Manifold
<

When I see it in the serial monitor I don't see a line break, but when I paste it here there is a line break after ">P_Manifold" before "<". Could that be a clue as to why I can't get strcmp to work? I'll check the rest of the code to see if I can find anything.

Could that be a clue as to why I can't get strcmp to work?

Yes.

You need to learn about the trim() method (and the proper values for the arguments to toCharArray() and the need to post ALL of your code).

PaulS:

rxBufString.substring(9, symbolLength).toCharArray(symbolCompare, symbolLength);

The second argument to the toCharArray() is NOT the length of the String. It is the size of the buffer to be written to. Do you have a large enough array?

Thanks, I know. symbolLength will always be 8 characters longer than symbolCompare because I'm removing the first 8 characters from rxBufString when converting it to a char array.

Edit: Solved it!
For some reason when reading from the ECU via my Sparkfun CAN bus shield an extra line break is set when the ECU sends "D" and then "A", which marks the end of a parameter that has been sent. I don't know if this a universal thing because the serial monitor seems to understand "DA" and makes a line break automatically.
To solve this I changed

rxBufString.substring(9, (symbolLength)).toCharArray(symbolCompare, symbolLength);

to

rxBufString.substring(9, (symbolLength - 1)).toCharArray(symbolCompare, symbolLength);

which removes the last charcter in rxBufString which is the line break.

Thank you very much for your help!

Regards,
Christian