After many hours of frustration I can create a rather strange bit of behavior.
Using the example code below on a Mega 2560 R2 and an UNO R3 with the Serial Monitor set to NewLine 115200 baud the application seems to function as expected - that is, you send some text and it is returned to you.
const String MY_STRING_1 = "abcd";
const String MY_STRING_2 = "efgh";
const String MY_STRING_3 = "ijkl";
String temp1;
String temp2;
String temp3;
char inByte;
String command;
void setup(){
Serial.begin(115200);
}
void loop(){
temp1 = MY_STRING_2;
temp2 = MY_STRING_2;
temp3 = MY_STRING_2;
if (Serial.available() > 0) {
inByte = Serial.read();
if (inByte == 10 || inByte == 13){
Serial.println("command Received: " + command);
command = "";
}
else {
command.concat(inByte);
}
}
}
Now, make one small change:
temp1 = MY_STRING_2;
temp3 = MY_STRING_2;
temp2 = MY_STRING_2;
And the application no longer returns any text. If you reset the device, select "Send" with the Serial Monitor the application will return the expected result but as soon as a single (or more than one) character is sent then it stops responding.
Also, the length of the 3 constants at 4 characters is important because if they are reduced to 3 characters then no problem.
Change the variable assignment order back to the original order and all is well.
Can anyone please explain why this is happening? Surely the order of variable assignment should not matter.
Thanks.