HI;
I have a program like this:
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
int CmIdx0; // Input line indexes;
int CmIdx1;
int CmIdx2;
int CmIdx3;
String fstArg; // line arguments converted
String secArg;
String trdArg;
String frtArg;
String fthArg;
void setup() {
Serial.begin(9600); // initialize serial:
inputString.reserve(200); // reserve 200 bytes for the inputString:
}
void loop() {
if (stringComplete)
{
inputString.toUpperCase();
Serial.println(inputString); // print the string when a newline arrives:
CmIdx0 = inputString.indexOf(',');
CmIdx1 = inputString.indexOf(',' , CmIdx0 +1);
CmIdx2 = inputString.indexOf(',' , CmIdx1 +1);
CmIdx3 = inputString.indexOf(',' , CmIdx2 +1);
fstArg = inputString.substring(0,CmIdx0);
secArg = inputString.substring(CmIdx0+1,CmIdx1);
trdArg = inputString.substring(CmIdx1+1,CmIdx2);
frtArg = inputString.substring(CmIdx2+1,CmIdx3);
fthArg = inputString.substring(CmIdx3+1);;
// these lines are for test only - will be deleted i the future.
Serial.print(fstArg );
Serial.print(" ");
Serial.print(secArg );
Serial.print(" ");
Serial.print(trdArg );
Serial.print(" ");
Serial.print(frtArg );
Serial.print(" ");
Serial.print(fthArg );
Serial.print(" ");
Serial.println("");
inputString = ""; // clear the string:
stringComplete = false;
}
}
void serialEvent()
{
while (Serial.available())
{
char inChar = (char)Serial.read(); // get the new byte:
inputString += inChar; // add it to the inputString:
if (inChar == '\n') // if the incoming character is a newline, set a flag
// so the main loop can do something about it:
{
stringComplete = true;
}
}
}
int MinIdxOf(String Buffer)
{
for ( int i = 0; i < Buffer.length - 1; i++)
{
if ((Buffer[i] == " " ) || (Buffer[i] == "," ))
return i;
}
return -1;
}
I read a line from the serial.event (eg: A,0000,0000,0000,0000) .
A letter ( can be A-Z uppercase) followed by 2 - 4 numbers (param1 - param4) . And the separators can be SPACE OR COMMA. (Comma shown here.)
Instead of using the STANDARD indexOF(char c) i want to use my own MinIdxOf(String Buffer), because both a Comma OR space will be accepted as separator at address I.
But the MinIdxOf(String Buffer) function is not working (invalid use of Member Function (did you forgot he '0' ?)
Now I have 2 questions:
1: What am I doing wrong in MinIdxOf(String Buffer) function, and how do I make I work ?
2: Later I want to include HEX-string (EG: B 01 FFFF FEDF 0099 (space(s) used here) and is there a smart way to convert param1 - param 4 (including HEX-strings) to an Integer ?
KRIS