Quantcast
Channel: sures kumar
Viewing all articles
Browse latest Browse all 10

Sending strings from Arduino to Openframeworks

$
0
0

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..

filename.h

ofSerial mySerial;

 

filename.cpp
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;

    }

    str+=ss.str();

    if (ch==until) {

        string tmp=str;

        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++) {

            printf(“%c”,str[i]);

        }

         printf(“\n”);

         

    } while (str!=”");

}



Viewing all articles
Browse latest Browse all 10

Latest Images

Trending Articles



Latest Images