Ich möchte an einen Arduino über USB Befehle schicken in Form von:
command:value
Dazu hab ich mir eine Anweisung zum splitten aus dem Netz gesucht - nur leider ist die Rückgabe von value leer
Code
String splitString(String s, char parser, int index) {
String rs='\0';
int parserIndex = index;
int parserCnt=0;
int rFromIndex=0, rToIndex=-1;
while (index >= parserCnt) {
rFromIndex = rToIndex+1;
rToIndex = s.indexOf(parser,rFromIndex);
if (index == parserCnt) {
if (rToIndex == 0 || rToIndex == -1) {
return '\0';
}
return s.substring(rFromIndex,rToIndex);
} else {
parserCnt++;
}
}
return rs;
}
String sstring;
void serialEvent() {
if (Serial.available()) {
sstring = Serial.readStringUntil('\n');
Serial.print("got: ");
Serial.println(sstring);
String command = splitString(sstring, ':', 0);
String value = splitString(sstring, ':', 1);
Serial.print("command: ");
Serial.println(command);
Serial.print("value: ");
Serial.println(value);
}
}
Alles anzeigen
Die Rückgabe sieht bei mir derzeit so aus:
Code
got: links:20
command: links
value:
got: f:123
command: f
value:
got: fahr:255
command: fahr
value:
Alles anzeigen
was mach ich falsch :s