parsing string data with strtok..

hello everyone,
i need to split data from this serial input data..
213,342,234p
23,343,343p

ending character for this data is letter p...

so for i tried to split using string and sub string option...its working fine with this example 213,342,234p,
but for this 23,343,343p...its not acting properly..i tried to do delimeter type..but i dnt have much info abt it... i have upload my code...

char recievedChar;
String temperature;
String gas;
String breaks;
String readString;
int value;
void setup()
{
Serial.begin(9600);
}

void loop()
{
test();
}

void test() {
while (Serial.available() > 0)
{
char IncomingData = Serial.read();

readString += IncomingData ;
temperature = readString.substring(0, 3);
gas = readString.substring(4, 7);
breaks = readString.substring(8, 11);

//Serial.print(IncomingData);

// Process message when new line character is DatePrimite
if (IncomingData == 'p')
{
Serial.print("temp=");
Serial.println(temperature);
temperature = ""; // Clear buffer
Serial.print("gas=");
Serial.println(gas);
gas = ""; // Clear buffer
Serial.print("breaks=");
Serial.println(breaks);
breaks = ""; // Clear buffer
readString = "";
}
}
}

data_string.ino (1.13 KB)

The strtok() function is for use with cstrings and not the String class.

The parse example in Serial Input Basics illustrates its use.

Note that it is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).

...R

Another thing: the characters on the Serial interface trickle in slowly, one by one. Every couple dozen or couple hundred iterations of loop() you may have another character that has arrived. Validate, parse and store the data as it comes in.

Don't try to read substrings before you have a complete set of data, and have verified the data is valid (i.e. the commas where you expect them, digits and only digits where you expect them, and the terminaitng p character).

http://forum.arduino.cc/index.php?topic=396450.0

your tutorials helped me to realise mistakes i have done on strtok function...thanks its helped me to take my project next step...

Using "String" class on an Arduino?

That's daring! :roll_eyes: