ofDocsexamples communication serialExample src ofApp.cpp
#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup(){
	ofSetVerticalSync(true);
	
	bSendSerialMessage = false;
	ofBackground(255);	
//    ofSetLogLevel(OF_LOG_VERBOSE);
	
	font.load("DIN.otf", 64);
    
    ofLog::setAutoSpace(true);
	
	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
	//serial.setup("COM4", baud); // windows example
	//serial.setup("/dev/ttyUSB0", baud); //linux example
	
	nTimesRead = 0;
	nBytesRead = 0;
	readTime = 0;
	memset(bytesReadString, 0, 4);
    timestamp = 0;
    
    for (auto & d : deviceList) {
        ofLog() << "Device:" << d.getDevicePath() << d.getDeviceName() << d.getDeviceName().find("tty.usbmodem");
        if (d.getDeviceName().find("tty.usbmodem") != std::string::npos  || d.getDeviceName().find("tty.usbserial") != std::string::npos ) {
            
            serial.setup(d.getDevicePath(), baud); // mac osx example
        }
    }
}

//--------------------------------------------------------------
void ofApp::update(){
    
    float t = ofGetElapsedTimef();
	if (t > timestamp + 1 ){
        
        alt = !alt;
		// (1) write the letter "a" to serial:
        const char a[8] = ("VCP:204");
        const char b[8] = "VCP:200";
        serial.writeByte('2');
       if (alt) serial.writeBytes( a , 8 );
       if (!alt) serial.writeBytes( b , 8 );
        ofLog() << alt;
        serial.writeByte('3');
        timestamp = t;
//
////         (2) read
////         now we try to read 3 bytes
////         since we might not get them all the time 3 - but sometimes 0, 6, or something else,
////         we will try to read three bytes, as much as we can
////         otherwise, we may have a "lag" if we don't read fast enough
////         or just read three every time. now, we will be sure to
////         read as much as we can in groups of three...
//
//        nTimesRead = 0;
//        nBytesRead = 0;
//        int nRead  = 0;  // a temp variable to keep count per read
//
//        unsigned char bytesReturned[3];
//
//        memset(bytesReadString, 0, 4);
//        memset(bytesReturned, 0, 3);
//
//        while( (nRead = serial.readBytes( bytesReturned, 3)) > 0){
//            nTimesRead++;
//            nBytesRead = nRead;
//        };
//
//        memcpy(bytesReadString, bytesReturned, 3);
//
//        ofLog() << bytesReadString;
//        bSendSerialMessage = false;
//        readTime = ofGetElapsedTimef();
	}
}

//--------------------------------------------------------------
void ofApp::draw(){
//    if (nBytesRead > 0 && ((ofGetElapsedTimef() - readTime) < 0.5f)){
//        ofSetColor(0);
//    } else {
//        ofSetColor(220);
//    }
//    string msg;
//    msg += "click to test serial:\n";
//    msg += "nBytes read " + ofToString(nBytesRead) + "\n";
//    msg += "nTimes read " + ofToString(nTimesRead) + "\n";
//    msg += "read: " + ofToString(bytesReadString) + "\n";
//    msg += "(at time " + ofToString(readTime, 3) + ")";
//    font.drawString(msg, 50, 100);
}

//--------------------------------------------------------------
void ofApp::keyPressed  (int key){ 
	
}

//--------------------------------------------------------------
void ofApp::keyReleased(int key){ 
	
}

//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y){
	
}

//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button){
	
}

//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){
	bSendSerialMessage = true;
}

//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){
	
}

//--------------------------------------------------------------
void ofApp::mouseEntered(int x, int y){

}

//--------------------------------------------------------------
void ofApp::mouseExited(int x, int y){

}

//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){
	
}

//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){
	
}

//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){ 
	
}