Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- String sVal; //the current variable in string form
- uint16_t variableCount=0; //variable to keep track how many variables have been parsed
- //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
- int variable1;
- long variable2;
- float variable3;
- double variable4;
- void setup() {
- Serial.begin(115200);
- }
- void loop() {
- while(Serial.available()){ //run the fallowing code aslong as there is serial data
- char c=Serial.read(); //read the current charecter
- if(c==10||c==13){ //check to see if the current charecter is /r(carriage return) or /n(newline) which indicates end of data
- while(parseBuffer.length()>0){ //keep parsing the data until the buffer is empty
- int delimIdx=parseBuffer.indexOf(','); //locate the deliminator charecter
- if(delimIdx!=-1){ //a delim charecter was found
- sVal=parseBuffer.substring(0,delimIdx); //set the current variable from 0 to the delim charecter
- parseBuffer=parseBuffer.substring(delimIdx+1); //remove the parsing variable from the buffer
- }
- else{ //no delim charecter was found, so we only have the last variable left
- sVal=parseBuffer;
- parseBuffer=""; //clear the buffer since all data has been parsed
- }
- //asign the parsed variable into a desired variable
- //available functions for parsing are:
- //atoi: convert string to int
- //atol: convert string to long int
- //atof: convert string to double(which you can then assign to a float for lower precision)
- switch(variableCount){
- case 0: variable1=atoi(sVal.c_str()); break; //first veriable of the serial stream
- case 1: variable2=atol(sVal.c_str()); break; //second variable of the serial stream
- case 2: variable3=atof(sVal.c_str()); break; //third veriable of the serial stream
- case 3: variable4=atof(sVal.c_str()); break; //fourth variable of the serial stream
- //case 4: anotherVariable=atoi(sVal.c_str()); break; //want more variables?
- //case 5: yetAnotherVariable=atoi(sVal.c_str()); break; //how about even more? just keep defining more cases
- default: Serial.println("Too many variables!"); break; //there is more variables in the data stream than being parsed
- }
- variableCount++; //increment the parsed variable counter
- }
- variableCount=0; //done proccessing the buffer so reset the variable counter
- }
- else{
- parseBuffer+=c;
- }
- }
- //Print the data to the screen
- Serial.println();
- Serial.print("Variable 1: ");
- Serial.println(variable1);
- Serial.print("Variable 2: ");
- Serial.println(variable2);
- Serial.print("Variable 3: ");
- Serial.println(variable3);
- Serial.print("Variable 4: ");
- Serial.println(variable4);
- //delay a second to keep from spamming screen too fast, delete this in the actual project
- delay(1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement