For what? I asked what happened if you tried my suggestion. So: did it give the expected output? Or what? Did you understand how strtok() works?
It changes the input "string," so the input "string" must be in an array, not a string literal or other constant "string." (A lot of people don't like to use strtok() because of that. As far as I'm concerned: Chacun à son goût!)
in fact i need ...
You use strtok() the same way: first call it with the name of the array, and use NULL as the first parameter for subsequent calls. The delimiters argument doesn't have to be the same every time.
/*
In your program, you would read stuff into a char array.
For purposes of testing strtok, I'll just set it up
a "string" in an initialized array.
*/
char arr[30] = {
"!ANG:-11.35,-8.51,14.78"
};
void setup()
{
Serial.begin(9600);
}
void loop()
{
char str[30];
float roll, pitch, yaw;
int errors = 0;
strcpy(str, arr);
// First is throwaway unless you want to do strcmp with "!ANG" or some such thing
char *chpt = strtok(str, ":");
if (chpt == NULL) {
Serial.println("First strok returns NULL");
++errors;
}
if (errors == 0) {
chpt = strtok(NULL, ",");
if (chpt == NULL) {
Serial.println("Second strok returns NULL");
++errors;
}
else {
roll = atof(chpt);
}
}
if (errors == 0) {
chpt = strtok(NULL, ",");
if (chpt == NULL) {
Serial.println("Third strok returns NULL");
++errors;
}
else {
pitch = atof(chpt);
}
}
if (errors == 0) {
chpt = strtok(NULL, ",\r\n");
if (chpt == NULL) {
Serial.println("Fourth strok returns NULL");
++errors;
// This is an input error: do something to handle it.
}
yaw = atof(chpt);
}
if (errors == 0) {
Serial.print("(");
Serial.print(roll);
Serial.print(", ");
Serial.print(pitch);
Serial.print(", ");
Serial.print(yaw);
Serial.println(")");
}
delay(10000);
}
Output:
(-11.35, -8.51, 14.78)
Regards,
Dave
Footnote:
I used strtok(). You can use strtok()_r if you want to. (Since there are no threads or other re-entrance considerations in Arduino-land, I didn't bother making it thread-safe, but thread-safety awareness is not a bad habit to get into. Maybe I should think about it some more...)