Skip to content

Instantly share code, notes, and snippets.

@adielfernandez
Last active March 10, 2018 23:29
Show Gist options
  • Save adielfernandez/9989000 to your computer and use it in GitHub Desktop.
Save adielfernandez/9989000 to your computer and use it in GitHub Desktop.
Arduino serial string parsing (from OF string sending)
//This code is for sending data from OpenFrameworks to Arduino. This will package 3 values for the arduino
//to read as a string. This code supports values from -2,147,483,648 to 2,147,483,647, not just 0-255!
//(since we're storing them on the arduino side as long variables)
//------------------------------OpenFrameworks string sending------------------------------
//The code below will send the values 12345, 0 and 255.
//----------in header file----------
ofSerial serial;
void sendSerial(long one, long two, long three);
//----------in cpp file----------
void testApp::setup(){
ofSetLogLevel(OF_LOG_VERBOSE);
serial.listDevices();
vector <ofSerialDeviceInfo> deviceList = serial.getDeviceList();
// this should be set to whatever com port your serial device is connected to.
// (ie, COM4 on a pc, /dev/tty.... on linux, /dev/tty... on a mac)
// arduino users check in arduino app....
int baud = 9600;
serial.setup(0, baud); //open the first device (or replace 0 with the serial device listed in the console
}
//This function is a roundabout way of packaging the values as a character array since
//OF can't send strings via serial. Borrowed from Kyle McDonald
//https://github.com/openframeworks/openFrameworks/issues/279
void testApp::sendSerial(long one, long two, long three){
//package values as a string separated by commas and with a newline ending
string outgoing = ofToString(one) + "," + ofToString(two) + "," + ofToString(three) + "\n";
//serial can't handle strings so we turn the string into a char array
unsigned char* outgoingCharArray = new unsigned char[outgoing.size()];
memcpy(outgoingCharArray, outgoing.c_str(), outgoing.size());
//send the char array out via serial
serial.writeBytes(outgoingCharArray, outgoing.size());
//delete the char array for memory management
delete [] outgoingCharArray;
}
//then just call the sendSerial function whenever you want to send data:
sendSerial(12345,0,255);
//----------------------------------------Arduino code----------------------------------------
//incoming data stored as longs
long data[3];
// This will be the buffered string from Serial.read()
// up until you hit a \n
String str = "";
// Keep track of current position in array
int counter = 0;
// Keep track of the last comma so we know where to start the substring
int lastIndex = 0;
void setup(){
Serial.begin(9600);
}
void loop(){
//if there is data coming in from serial
if (Serial.available() > 0) {
//read the first byte and store it as a char type
char ch = Serial.read();
//if we find a newline character, it means we got to the end of the string so lets do something
if (ch == '\n') {
//lets loop through each character in the string
for (int i = 0; i < str.length(); i++) {
// check if it's a comma
if (str.substring(i, i+1) == ",") {
// Grab the piece from the last index up to the current position and store it temporarily
String tempString = str.substring(lastIndex, i);
// Then convert that temporary string to an int and store that
data[counter] = tempString.toInt();
// Update the last position and add 1, so it starts from the next character
lastIndex = i + 1;
// Increase the position in the array that we store into
counter++;
}
//If we're at the end of the string (no more commas to stop us)
if (i == str.length() - 1) {
//grab the last part of the string from the lastIndex to the end
String tempString = str.substring(lastIndex);
//And convert to int
data[counter] = tempString.toInt();
}
}
//clear out string and counters to get ready for the next incoming string
str = "";
counter = 0;
lastIndex = 0;
}
else {
//if we havent reached a newline character yet, add the current character to the string
str += ch;
}
}
//data now available in the data[] array
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment