Sending strings from Arduino to Openframeworks

Recently worked on a project where I had to receive strings from Arduino to Openframeworks. Its not really that straight forward like processing so thought of sharing the code..


ofSerial mySerial;


setup() {

mySerial.setup(0, 9600); // update port no and baud rate according to your system.



// trim trailing spaces

string ofxTrimStringRight(string str) {

size_t endpos = str.find_last_not_of(” \t\r\n”);

return (string::npos != endpos) ? str.substr( 0, endpos+1) : str;



// trim trailing spaces

string ofxTrimStringLeft(string str) {

size_t startpos = str.find_first_not_of(” \t\r\n”);

return (string::npos != startpos) ? str.substr(startpos) : str;



string ofxTrimString(string str) {

return ofxTrimStringLeft(ofxTrimStringRight(str));;


string ofxGetSerialString(ofSerial &serial, char until) {

static string str;

stringstream ss;

char ch;

int ttl=1000;

while ((ch=serial.readByte())>0 && ttl–>0 && ch!=until) {

ss << ch;



if (ch==until) {

string tmp=str;


return ofxTrimString(tmp);

} else {

return “”;




update() {

// Receive String from Arduino

string str;

do {

str = ofxGetSerialString(mySerial,’\n’); //read until end of line

if (str==””) continue;


for(int i = 0; i < str.length(); i++) {





} while (str!=””);



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s