How to split a string

Hi,

I've searched everywhere and I don't seem to find a simple explanation. I want to get two arduinos to communicate trough serial.

One arduino sends a string like this: 34,3E,FE The other one receives the string and then splits it in 3 variables like this:

variable1 = 34 variable2 = 3E variable3 = FE

I have found some code but with strtok_r() like this:

char sz[] = "Here; is some; sample;100;data;1.414;1020";
void setup()
{
 char *p = sz;
 char *str;
 Serial.begin(9600);
 while ((str = strtok_r(p, ";", &p)) != NULL) // delimiter is the semicolon
   Serial.println(str);
}

but I don't really understand, whats with the * I just want to split the string, thought it would be simple :-/

Can anyone help explain the strok_r function or give an other simple way

Thanks

The * thing signifies that variable "p" is a pointer to a char (byte). So, the assignment "char *p = sz;" assigns to p the address of the letter 'H' in the string "sz". If you want to access the thing that "p" points to, you can write either char x = *p;" (this is called dereferencing the pointer), or you can write "char x = p[0];" (or even, deviously "char x = 0[p];"). If you want to point to the character after the 'H', you write "p++;", so now "p" points to 'e'.

"strtok_r" is a function returning a pointer to a "char", so "str" has to be of this type too.

String handling in 'C' isn't great - it takes a while to get used to.

Hi,

Thanks for the info, I have tried a couple of things but my programing skills stink. I've done something like this:

void setup()
  {
    Serial.begin(9600);
  }
void loop() 
  {
    if(Serial.available() > 0) 
    {
      char message [8] = {Serial.read()};    //serial message comes in like this 10,A3,3F
      char *p = message;
    }

     int number1 = p[1,2];                          // here I get an error :'p' was not declared in this scope
     int number2 = p[4,5];
     int number3 = p[7,8];
   }

I know that this code is rubbish, other stuf i tried was working with an array, but that doest work either

Could someone post some sample code that I can understand, cause I don't seem to get it, The only thing I want to do is to take a string received over serial and seperate it in three variables so the arduino can work with those.

Thanks

Won't this:

void setup(){ Serial.begin(9600); }

void loop(){ if (Serial.available() >= 8){ char number1[2] = { Serial.read(), Serial.read() }; Serial.read();//flush the delim char number2[2] = { Serial.read(), Serial.read() }; Serial.read();//flush the delim char number3[2] = { Serial.read(), Serial.read() };

Serial.println("Debug: "); Serial.print("number1: "); Serial.println(number1); Serial.print("number2: "); Serial.println(number2); Serial.print("number3: "); Serial.println(number3); } }

Be an easy solution?

Hi,

Thanks for the code, I understand it :) but it doesn't do exactly what I want. If I send for example AA,BB,CC it gives back:

Debug: number1: AABBCCh number2: BBCCh number3: CCh

it's almost what I need ;) (and what's with the h on the end)

Ah, my fault.

Maybe like this:

void setup(){ Serial.begin(9600); }

void loop(){ if(Serial.available() >= 8){ char number1[3] = { Serial.read(), Serial.read() , '\0'}; Serial.read();//flush the delim char number2[3] = { Serial.read(), Serial.read() , '\0' }; Serial.read();//flush the delim char number3[3] = { Serial.read(), Serial.read() , '\0' };

Serial.println("Debug: "); Serial.print("number1: "); Serial.println(number1); Serial.print("number2: "); Serial.println(number2); Serial.print("number3: "); Serial.println(number3); } }

Yes, it works :) :) :)

Thank you, one little thing, how do i get a hex number from the string (cause i need a hex not a char eventually), I've tried:

value1 = int(number1); value2 = int(number2); value3 = int(number3);

but this gives the value of 'number1' literally

value1 = int(*number1); value2 = int(*number2); value3 = int(*number3);

gives something, but I don't know what

thanks

Google for something called ASCII TO Int = atoi

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1176289764#5