String Processing

hi
i have an arduino and a 9dof AHRS IMU.
IMU send serial data by this format

0.47,0.32,-17.37
0.46,0.35,-17.36
0.47,0.37,-17.36
0.48,0.34,-17.35

i need a function that Split This string to an array of strings

Like this in VB6

inp="AAAA,BBBB,CCCC"
inp2 = Split(inp, ",")(1)

that inp2 equal to BBBB

i try this codes

// test = "This,is,an,test,of,the,string,tokenizer,function.";

a = strtok_r(test, "," ,&brkb);
Serial.println(a);

a = strtok_r(NULL, "," ,&brkb);            
Serial.println(a);

But out put is

This
Y
This
Y
This
Y

i need some help :-/
please help me :cry: :cry:

What happens if you try the following:

    char test[] = "This,is,a,test,of,the,string,tokenizer,function";

    char *a;
    char *brkb;

    a = strtok_r(test, "," ,&brkb);
    Serial.print("1: ");
    Serial.println(a);

    a = strtok_r(NULL, "," ,&brkb);
    Serial.print("2: ");
    Serial.println(a);

Regards,

Dave

tanx
in fact i need a code or method that help me to read ROLL ,PITCH and YAW from 9dof AHRS razor IMU
this imu send data as this Format

!ANG:-11.35,-8.51,14.78
!ANG:-13.21,-9.75,14.29
!ANG:-14.73,-11.15,13.33
!ANG:-15.83,-12.74,11.84
!ANG:-16.71,-14.07,10.29
!ANG:-17.37,-14.93,8.96
!ANG:-18.29,-15.52,8.11
!ANG:-19.28,-16.12,7.46
!ANG:-19.40,-15.98,6.56
!ANG:-18.80,-15.27,5.64

and i try to split it into three integers.
like roll=-18.80
pitch=-15.27
yaw=5.64
please help me :-/

tanx

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...)

You can use strtok()_r

Works better if you use strtok_r().

Thanks, Paul. I had to lay off my proofreader. Tough times...

Regards,

Dave

Tanx for your answer ;D
your code is working perfect and solve my problem :sunglasses:
best regards :wink: