Advertisement
sanelss

Untitled

Jun 11th, 2015
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.34 KB | None | 0 0
  1. String parseBuffer=""; //the entire serial input buffer to a /r or /n charecter. There is no size limit so if there is too much data you will run out of RAM and the program will crash
  2. String sVal;  //the current variable in string form
  3. uint16_t variableCount=0;  //variable to keep track how many variables have been parsed
  4.  
  5.  
  6. //variables to be read. can be of any parsable type(atoi,atol,atof) these also don't need to be seperate variables here, they can be existing variabels in other places/objects just use those in appropriate section below
  7. int variable1;
  8. long variable2;
  9. float variable3;
  10. double variable4;
  11.  
  12. void setup() {
  13.   Serial.begin(115200);
  14. }
  15.  
  16. void loop() {
  17.  
  18.  
  19.   while(Serial.available()){  //run the fallowing code aslong as there is serial data
  20.     char c=Serial.read();    //read the current charecter
  21.    
  22.     if(c==10||c==13){ //check to see if the current charecter is /r(carriage return) or /n(newline) which indicates end of data
  23.       while(parseBuffer.length()>0){  //keep parsing the data until the buffer is empty
  24.         int delimIdx=parseBuffer.indexOf(',');  //locate the deliminator charecter
  25.         if(delimIdx!=-1){ //a delim charecter was found
  26.           sVal=parseBuffer.substring(0,delimIdx);   //set the current variable from 0 to the delim charecter
  27.           parseBuffer=parseBuffer.substring(delimIdx+1); //remove the parsing variable from the buffer
  28.         }
  29.         else{  //no delim charecter was found, so we only have the last variable left
  30.           sVal=parseBuffer;
  31.           parseBuffer=""; //clear the buffer since all data has been parsed
  32.         }
  33.        
  34.         //asign the parsed variable into a desired variable
  35.         //available functions for parsing are:
  36.         //atoi: convert string to int
  37.         //atol: convert string to long int
  38.         //atof: convert string to double(which you can then assign to a float for lower precision)
  39.         switch(variableCount){
  40.           case 0:  variable1=atoi(sVal.c_str());  break;          //first veriable of the serial stream
  41.           case 1:  variable2=atol(sVal.c_str());  break;          //second variable of the serial stream
  42.           case 2:  variable3=atof(sVal.c_str());  break;          //third veriable of the serial stream
  43.           case 3:  variable4=atof(sVal.c_str());  break;          //fourth variable of the serial stream
  44.           //case 4:  anotherVariable=atoi(sVal.c_str());  break;          //want more variables?
  45.           //case 5:  yetAnotherVariable=atoi(sVal.c_str());  break;       //how about even more? just keep defining more cases
  46.           default:  Serial.println("Too many variables!"); break;   //there is more variables in the data stream than being parsed
  47.         }
  48.        
  49.         variableCount++;  //increment the parsed variable counter
  50.       }  
  51.       variableCount=0;  //done proccessing the buffer so reset the variable counter
  52.     }  
  53.     else{
  54.       parseBuffer+=c;
  55.     }
  56.   }
  57.  
  58.  
  59.  
  60.   //Print the data to the screen
  61.   Serial.println();
  62.   Serial.print("Variable 1: ");
  63.   Serial.println(variable1);
  64.   Serial.print("Variable 2: ");
  65.   Serial.println(variable2);
  66.   Serial.print("Variable 3: ");
  67.   Serial.println(variable3);
  68.   Serial.print("Variable 4: ");
  69.   Serial.println(variable4);
  70.  
  71.   //delay a second to keep from spamming screen too fast, delete this in the actual project
  72.   delay(1000);
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement