01:47:02.608 -> ID t1:1345180, t2:3397651, t3:1350181
i wants to select the values of t1, t2 ,t3 for some basic calculations. How could i extract it from string?
01:47:02.608 -> ID t1:1345180, t2:3397651, t3:1350181
i wants to select the values of t1, t2 ,t3 for some basic calculations. How could i extract it from string?
What kind of data container is it in? i.e. show your actual code.
You could use strstr to locate the t tags, and work from that.
unsigned long tim = millis();
// Serial.print(x.toInt() + tim);
Serial.print("Current time: ");
Serial.println(tim);
Serial.println(rString + ", t3:" + tim); //here's all the string is printed, i want to extract the t1,t2,t3 values.
Have you looked at documentation for the string manipulation functions that the String class provides?
The code you posted only shows where 'rString' is used, not defined or populated.
@aarg
i saw many,but didnt get the solution
Serial.println("ID t1:1345180, t2:3397651, t3:1350181");
ID t1:1345180, t2:3397651, t3:1350181
just assume its a string and i want to subtract t3-t2 after extracting it.
Use the C-string functions for that. There are individual tutorials for each of the functions.
Use of String objects is not a good idea, as they cause Arduinos to crash.
Yes, you really need to compare C-strings and the String class and go with one or the other. Namely, C-strings.
The String class is initially more beginner friendly, that's why so many tutorials use it. Then, the mysterious crashes come.
You could do something like this
char input[] = {"t1:1345180, t2:3397651, t3:1350181"};
void setup()
{
Serial.begin(115200);
while (!Serial);
char * ptr;
unsigned long t[3];
byte index = 0;
ptr = strtok(input, ":");
ptr = strtok(NULL, ",");
t[index] = atol(ptr);
for (index = 1; index < 3; index++)
{
ptr = strtok(NULL, ":");
ptr = strtok(NULL, ",");
t[index] = atol(ptr);
}
for (int x = 0; x < 3; x++)
{
Serial.println(t[x]);
}
Serial.println(t[0] + t[1] + t[2]);
}
void loop()
{
}
f you capture the string of characters as a String, then the below is an example of parsing out data using the String functions. All out in the open and not hidden in cryptic C-string functions.
//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
}
}
}