Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Arduino (C language) parsing string with delimiter (input through serial interface)

Didn't find the answer here :/

I want to send to my arduino through a serial interface (Serial.read()) a simple string of three numbers delimited with comma. Those three numbers could be of range 0-255.

Eg. 255,255,255 0,0,0 1,20,100 90,200,3

What I need to do is to parse this string sent to arduino to three integers (let's say r, g and b).

So when I send 100,50,30 arduino will translate it to

int r = 100
int g = 50
int b = 30

I tried lots of codes, but none of them worked. The main problem is to translate string (bunch of chars) to integer. I figured out that there will probably be strtok_r for delimiter purpose, but that's about it.

Thanks for any suggestions :)

share|improve this question
    
atoi to turn it into a number –  Keith Nicholas Jun 17 '12 at 2:14
    
so, split it up by commas, and then atoi each bit –  Keith Nicholas Jun 17 '12 at 2:15
    
Why bother going through strings? Just send three bytes with the appropriate values. –  Kevin Jun 17 '12 at 2:25
1  
If you are controlling the orginal sent content, instead of sending strings, why not just send 3 bytes? Then no parsing or commas needed. –  jdh Jun 17 '12 at 2:30
    
Save the bytes into a unsigned char[], then you can easily access them. –  user195488 Jun 17 '12 at 2:49

3 Answers 3

To answer the question you actually asked, String objects are very powerful and they can do exactly what you ask. If you limit your parsing rules directly from the input, your code becomes less flexible, less reusable, and slightly convoluted.

Strings have a method called indexOf() which allows you to search for the index in the String's character array of a particular character. If the character is not found, the method should return -1. A second parameter can be added to the function call to indicate a starting point for the search. In your case, since your delimiters are commas, you would call:

int commaIndex = myString.indexOf(',');
//  Search for the next comma just after the first
int secondCommaIndex = myString.indexOf(',', commaIndex+1);

Then you could use that index to create a substring using the String class's substring() method. This returns a new String beginning at a particular starting index, and ending just before a second index (Or the end of a file if none is given). So you would type something akin to:

String firstValue = myString.substring(0, commaIndex);
String secondValue = myString.substring(commaIndex+1, secondCommaIndex);
String thirdValue = myString.substring(secondCommaIndex); // To the end of the string

Finally, the integer values can be retrieved using the String class's undocumented method, toInt():

int r = firstValue.toInt();
int g = secondValue.toInt();
int b = thirdValue.toInt();

More information on the String object and its various methods can be found int the Arduino documentation.

share|improve this answer

I think you want to do something like this to read in the data:

String serialDataIn;
String data[3];
int counter;


int inbyte;

void setup(){
  Serial.begin(9600);
  counter = 0;
  serialDataIn = String("");
}

void loop()
{
    if(serial.available){
        inbyte = Serial.read();
        if(inbyte >= '0' & inbyte <= '9')
            serialDataIn += inbyte;
        if (inbyte == ','){  // Handle delimiter
            data[counter] = String(serialDataIn);
            serialDataIn = String("");
            counter = counter + 1;
        }
        if(inbyte ==  '\r'){  // end of line
                handle end of line a do something with data
        }        
    }
}

Then use atoi() to convert the data to integers and use them.

share|improve this answer

@cstrutton -Excellent suggestion on using 'indexOf' . it saved me a ton of time for my project. One minor pointer though,

I noticed the thirdvalue did not get displayed (was coming back as ZERO). Upon playing with it little-bit and going through the doc at http://arduino.cc/en/Tutorial/StringIndexOf I realized, I can use lastIndexOf for the last value.

Here are two lines of modifications that provided correct third value.

int lastCommaIndex = myString.lastIndexOf(',');

String thirdValue = myString.substring(lastCommaIndex+1); // To the end of the string
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.