(deleted)
it is possible to convert the serial data string beds, and create n substrings according to the comma separator?
Yes, IF you had a string. You don't.
You need to read the data and store it in an array, keeping the array NULL terminated. Then, when the end of packet marker, whatever it is, arrives, you could use strtok() to get the comma-separated tokens.
(deleted)
But can I create an array of characters without knowing a priori the length?
You can't. But there is probably some reasonable maximum number of characters that you can expect from the device.
For NULL I usually use string.Trim(). Is it correct?
No. string is not a class, so it has no Trim() method.
I don't know what do you mean with strtok()
That's a shame. I'm willing to bet $5 that google does.
(deleted)
it is possible to convert the serial data string beds, and create n substrings according to the comma separator?Knowing that the readings are different lengths depending on the surrounding conditions?
Fairly easy if the total data packet has an end of data delimiter (* in the below test code).
//zoomkat 11-12-13 String capture and parsing
//from serial port input (via serial monitor)
//and print result out serial port
//copy test strings and use ctrl/v to paste in
//serial monitor if desired
// * is used as the data string delimiter
// , is used to delimit individual data
String readString; //main captured String
String angle; //data String
String fuel;
String speed1;
String altidude;
int ind1; // , locations
int ind2;
int ind3;
int ind4;
void setup() {
Serial.begin(9600);
Serial.println("serial delimit test 11-12-13"); // so I can keep track of what is loaded
}
void loop() {
//expect a string like 90,low,15.6,125*
//or 130,hi,7.2,389*
if (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
if (c == '*') {
//do stuff
Serial.println();
Serial.print("captured String is : ");
Serial.println(readString); //prints string to serial port out
ind1 = readString.indexOf(','); //finds location of first ,
angle = readString.substring(0, ind1); //captures first data String
ind2 = readString.indexOf(',', ind1+1 ); //finds location of second ,
fuel = readString.substring(ind1+1, ind2+1); //captures second data String
ind3 = readString.indexOf(',', ind2+1 );
speed1 = readString.substring(ind2+1, ind3+1);
ind4 = readString.indexOf(',', ind3+1 );
altidude = readString.substring(ind3+1); //captures remain part of data after last ,
Serial.print("angle = ");
Serial.println(angle);
Serial.print("fuel = ");
Serial.println(fuel);
Serial.print("speed = ");
Serial.println(speed1);
Serial.print("altidude = ");
Serial.println(altidude);
Serial.println();
Serial.println();
readString=""; //clears variable for new input
angle="";
fuel="";
speed1="";
altidude="";
}
else {
readString += c; //makes the string readString
}
}
}