There is the chance that before and after the string some other garbage information is sent from the zero.
What I want to do is to load the entire String beginning from [ to ] into an String variable. All garbage that comes before and after should be ignored.
All my aproaches did not work stable or did just work for short messages, but not for long ones.
I think that this is an relatively common problem - can anyone point me to a function or piece of code that can handle this?
This was my last try:
It did work for short messages, but not for long ones....
void loop() {
/*** Daten vom Zero empfangen ***/
String value = "";
while (Serial.available()) {
char inChar = (char)Serial.read();
inputString = inputString + String(inChar);
if (inChar == ']') {
message_exists = true;
break;
}
}
if (message_exists == true) {
message_exists = false;
value = inputString;
inputString = "";
}
if (value == "") { value = "XXX"; }
/*** Daten senden ***/
sendPacket(send_data(value));
/*** Daten empfangen ***/
for (int i=0; i <= 10; i++){
delay(50);
String data = readPacket();
if (data != "") { Serial.print(data); }
}
}
"All my aproaches did not work stable or did just work for short messages, but not for long ones."
That gives us nothing to work with. What do you mean that it "didn't work"?
You are most likely experiencing Memory Problems due to: inputString = inputString + String(inChar); being inside a Loop which executes at least 569 times.
The first time through, the string consumes 1 Byte. The second time it consumes 2. The third time it consumes 3.
It consumes the Memory that the variable currently Needs PLUS the Memory the new value will Need.
So, the last time through, it Needs 568 + 569 Bytes.
To make it even worse, all the previous values haven't had time to be cleaned up meaning they are also still occupying Memory.
Your program is probably running out of Memory and crashing.
Because your values are so Long, compared to the amount of Memory you may have (which Arduino are you using? They have different amounts of Memory!), this will be no easy trick to accomplish but I don't think it is possible the way you are doing it.