ofSerial receiving strings

I wanted to make a quick and dirty ofSerial usage so I can send commands and monitor output of an external microcontroller.
I’ve noticed the serialExample is not so straightforward for this usage and people resort to other addons, but now this code is working great for me.
posting here in the case it is useful for somebody else:
Edit: Clear screen automatically to visualize most recent messages.

#include "ofApp.h"

ofBuffer buffer;
string serialData;

void ofApp::setup(){
	int baud = 9600;
	serial.setup("/dev/cu.usbmodem84673301", baud);
}

void ofApp::update(){
	if (serial.available()) {
		serial.readBytes(buffer, serial.available());
		serialData += buffer.getText();
		
// clear screen if there are more than 55 lines
		if (ofSplitString(serialData, "\r").size() > 55) {
			serialData = "";
		}
		serial.flush();
	}
}

void ofApp::draw(){
	ofBackground(0);
	ofSetColor(255);
	ofDrawBitmapString(serialData, 20, 20);
}
3 Likes