Go Down

Topic: Data parsing question (Read 1 time) previous topic - next topic


I have a program in Visual C++ that will send a string of serial data to my Arduino.  There will be two numbers, separated by a symbol.  I can change the symbol in the code if it's necessary.  The numbers are each 0-100.  For example, a few strings could look like
You get the idea.
I need to read this data into two separate variables in the Arduino so I can display each individually, as well as perform calculations on them.  

Here is some example code I wrote, but I realized quickly I'm in over my head on this one.

Code: [Select]

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup(){

 lcd.begin(2, 24);



void loop()
int i=0;
int j=0;
char var1 [3];
char var2 [3];
 if (Serial.available()) {      
       i = 0;
       j = 0;  

       while (Serial.available()){  
         if (Serial.read()!="X"){
             var1[i] = Serial.read();
             var2[j] = Serial.read();



I'm sure this isn't anywhere close to correct, it obviously won't compile due to me not having a clue what I'm doing with character arrays, but am I at least on the right track?  

Any help is greatly appreciated, just a push in the right direction would be wonderful.



Aug 16, 2010, 01:19 am Last Edit: Aug 16, 2010, 01:20 am by RuggedCircuits Reason: 1
Push in the right direction:

1. Accumulate characters in a buffer until you get a newline:
Code: [Select]
if (Serial.available()) {
 buf[index] = Serial.read();
 if (buf[index] == '\n') {
   buf[index] = 0;
 } else {
   index++;  // Check for overflow!

2. Split up the buffer using 'X' as a separator:
Code: [Select]
void interpret(char *buf)
  char *p;
  int var1, var2;

  p = strtok(buf, 'X');
  var1 = atoi(p);
  p = strtok(0, 'X');
  var2 = atoi(p);


Might not work exactly as written, but hopefully it's the right direction :)

Check out our new shield: http://www.ruggedcircuits.com/html/gadget_shield.html


Below is some simple test code I made for receiving and parcing serial data that might be of interest.

Code: [Select]

//zoomkat 8-12-10 roborealm serial servo parce test

#include <WString.h> //provides easy string handling
String readString = String(100);
String parce1 = String(10);
String parce2 = String(10);
int pos = 0;
int ind1 = 0;
int ind2 = 0;

#include <Servo.h>
Servo myservo;  // create servo object to control a servo

void setup() {

void loop() {

       //expect a string like wer,qwe rty,123 456,hyre kjhg,
       while (Serial.available()) {
         if (Serial.available() >0) {
       char c = Serial.read();  //gets one byte from serial buffer
       if (c == ',') { goto parce;}
       readString.append(c); } //makes the string readString
     if (readString.length() >0) {
     pos = readString.length(); //capture string length
     ind1 = readString.indexOf(' '); //position of the space
     parce1 = readString.substring(0, ind1); //first part of string
     parce2 = readString.substring(ind1+1, pos); //second part of string
     //int n;
     //n = atoi(readString); //convert string to number

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)


Code: [Select]
goto parce;
Bad boy, zoomkat. The break command will get out of the while loop just as well, without the need for spaghetti code.


Bad boy, zoomkat. The break command will get out of the while loop just as well, without the need for spaghetti code.

Indeed, however in this case it's functionally identical, in fact GCC will generate exactly the same output in this type of construct (jump to the next instruction outside a loop), and it may be argued that "break" is more opaque than "goto" in this case. No spaghetti code in sight, IMHO.

Go Up