Skip to content

Instantly share code, notes, and snippets.

@glynhudson
Created August 10, 2011 22:30
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save glynhudson/1138458 to your computer and use it in GitHub Desktop.
Save glynhudson/1138458 to your computer and use it in GitHub Desktop.
Arduino Serial CSV decoder - sketch to convert serial data received in CSV format into variables
/****************************** Functions **************************************/
/*************************************************************************************************/
// Gets the serial command from the serial port, decodes it and performs action
/*********************************************************************************************/
// Decodes a serial input string in the form a255,65535,65535,65535,65535 CR
// So an alpha command a (arg0), followed by up to 5 numerical arguments (arg1 to arg5)
// If any of the arguments are missing they will default to zero
// so b1 = b,1,0,0,0,0 and b,1 = b,1,0,0,0,0
// Always produces 6 comma separated terms, of which the first is alpha
// (or punctuation character except comma)
/*************************************************************************************************/
// a = command
// 255 = 8-bit sub address
// , is delimiter
// 65535 is integer arg1
// , is delimiter
// 65535 is integer arg2
// , is delimiter
// 65535 is integer arg3
// , is delimiter
// 65535 is integer arg4
// , is delimiter
// 65535 is integer arg5
/*********************************************************************************************/
/*************************************************************************************************/
int serialReader(){
initStrings(); // Clear the string buffers before we start
dataReady = 0; // Clear the serial flag
if (Serial.available() > 0) { // if there are bytes waiting on the serial port
inByte = Serial.read(); // read a byte
delay(10); // *** added to make it work ***
if (inByte == 13){ // follow the false code until you get a CR - this packs inString with the complete string
dataReady = true;
//************************************************************************************************************************
// Work your way through inString and parse out the various comma separated fields
// Put them into six seperate strings arg0_String to arg5_String so that they can be ennumerated or acted upon
//************************************************************************************************************************
k = 0; //reset the string pointer
q = 0 ;
for (int p = q+1; p <34; p++) { // Now start at the location after the 2nd comma and strip out the arg2_String
nextChar = inString[p]; // get the next Char
q++;
if (nextChar == 44 || nextChar == 13 ) { // if it's a comma (ASCII 44) then bail out
break;
}
arg0_String[k] = nextChar; // start packing the arg0_String
k++; // increment the pointer to arg1_String
}
/*******************************************************************************************/
// Now we process the arg1_String checking whether we are b1 or b,1 format
/*******************************************************************************************/
k = 0; //reset the string pointer
for (int p = q+1; p <34; p++) { // Now start at the location after the 2nd comma and strip out the arg2_String
nextChar = inString[p]; // get the next Char
q++;
if (nextChar == 44 || nextChar == 13 ) { // if it's a comma (ASCII 44) then bail out
break;
}
arg1_String[k] = nextChar; // start packing the arg1_String
k++; // increment the pointer to arg2_String
}
/*******************************************************************************************/
// Now we process the arg2_String in the same way
/*******************************************************************************************/
k = 0; //reset the string pointer
for (int p = q+1; p <34; p++) { // Now start at the location after the 2nd comma and strip out the arg2_String
nextChar = inString[p]; // get the next Char
q++;
if (nextChar == 44 || nextChar == 13 ) { // if it's a comma (ASCII 44) then bail out
break;
}
arg2_String[k] = nextChar; // start packing the arg1_String
k++; // increment the pointer to arg2_String
}
/*******************************************************************************************/
// Now we process the arg3_String in the same way
/*******************************************************************************************/
k = 0; //reset the string pointer
for (int p = q+1; p <34; p++) { // Now start at the location after the 2nd comma and strip out the arg3_String
nextChar = inString[p]; // get the next Char
q++;
if (nextChar == 44 || nextChar == 13 ) { // if it's a comma (ASCII 44) then bail out
break;
}
arg3_String[k] = nextChar; // start packing the arg1_String
k++; // increment the pointer to arg3_String
}
/*******************************************************************************************/
// Now we process the arg4_String in the same way
/*******************************************************************************************/
k = 0; //reset the string pointer
for (int p = q+1; p <34; p++) { // Now start at the location after the 2nd comma and strip out the arg4_String
nextChar = inString[p]; // get the next Char
q++;
if (nextChar == 44 ) { // if it's a comma (ASCII 44) then bail out
break;
}
arg4_String[k] = nextChar; // start packing the arg1_String
k++; // increment the pointer to arg4_String
}
/*******************************************************************************************/
// Now we process the arg5_String in the same way
/*******************************************************************************************/
k = 0; //reset the string pointer
for (int p = q+1; p <34; p++) { // Now start at the location after the 2nd comma and strip out the arg5_String
nextChar = inString[p]; // get the next Char
q++;
if (nextChar == 44 || nextChar == 13 ) { // if it's a comma (ASCII 44) then bail out
break;
}
arg5_String[k] = nextChar; // start packing the arg1_String
k++; // increment the pointer to arg5_String
}
arg0 = atoi(arg0_String); // get enumerated arg0
arg1 = atoi(arg1_String); // get enumerated arg1
arg2 = atoi(arg2_String); // get enumerated arg2
arg3 = atoi(arg3_String); // get enumerated arg3
arg4 = atoi(arg4_String); // get enumerated arg4
arg5 = atoi(arg5_String); // get enumerated arg5
arg6++; // increment the message number
// Print out the ennumerated arguments noting that arg0 is a character
sprintf( statusstr, "%u,%u,%u,%u,%u,%u,%u", arg0, arg1, arg2, arg3, arg4 , arg5, arg6 );
Serial.println(statusstr); // print out the string for debug purposes
// clear the inStrings
for (int j = 0; j < 34; j++) { // clear the inString for next time
inString[j] = 0;
}
i = 0;
return incoming;
}
else dataReady = false; // No CR seen yet so put digits into array
// Serial.print(inByte);
inString[i] = inByte;
i++;
}
}
/***************************************************************************************************/
// Initialise the strings to zero
/***************************************************************************************************/
int initStrings() {
for (int j = 0; j < 4; j++) { // clear the arg0_String
arg0_String[j] = 0;
}
for (int j = 0; j < 6; j++) { // clear the arg1_String
arg1_String[j] = 0;
}
for (int j = 0; j < 6; j++) { // clear the arg2_String
arg2_String[j] = 0;
}
for (int j = 0; j < 6; j++) { // clear the arg1_String
arg3_String[j] = 0;
}
for (int j = 0; j < 6; j++) { // clear the arg2_String
arg4_String[j] = 0;
}
for (int j = 0; j < 6; j++) { // clear the arg2_String
arg5_String[j] = 0;
}
}
/***********************************************************************************/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment