Go Down

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

Steve1

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
0X0
100X100
15X25
35X95
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);

 Serial.begin(115200);

}

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

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

 lcd.clear();
 lcd.write(var1[]);
 setCursor(0,1);
 lcd.write(var2[]);

 }      


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.

Thanks,
Steve

RuggedCircuits

#1
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;
   interpret(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);

  lcd.clear();
  lcd.write(var1);
  setCursor(0,1);
  lcd.write(var2);
...
}


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

zoomkat

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() {
     Serial.begin(9600);
       myservo.attach(9);
       }

void loop() {

       //expect a string like wer,qwe rty,123 456,hyre kjhg,
       while (Serial.available()) {
       delay(10);  
         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
       }
     
     
     parce:  
     if (readString.length() >0) {
     Serial.println(readString);
     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
     Serial.println(parce1);
     Serial.println(parce2);
     Serial.println();
     
     //int n;
     //n = atoi(readString); //convert string to number
     //myservo.writeMicroseconds(n);
     //myservo.write(n);
     readString="";
     }
  }

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

PaulS

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.

crimony

Quote
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