ofxSimpleSerial not so simple for me :)

Hi! I’m trying to setup ofxSimpleSerial with no luck.
Second device is connected through MAX3232 chip to UART pins on my Raspberry Pi B, minibian, OF0.9.2.
Second device is sending me messages on each button press on this device.
In minicom I can see the massages as expected: 0006 and 0007 depending on the button, so the connection is OK.
I’ve added few ofLog() functions to check what’s going on and it seams that the void ofApp::onNewMessage(string & message) is not running since I don’t see onNewMessage in ofApp is running! in my logs.

main.cpp:

#include "ofMain.h"
#include "ofApp.h"

//========================================================================
int main( ){

	ofSetupOpenGL(1024,768, OF_WINDOW);
	ofSetFrameRate(30);
	ofRunApp( new ofApp());
}

ofApp.h:

#pragma once

#include "ofMain.h"
#include "testApp.h"

class ofApp : public ofBaseApp{
	public:
		void setup();
		void update();
		void draw();
		
		void keyPressed(int key);
		void keyReleased(int key);
		void mouseMoved(int x, int y);
		void mouseDragged(int x, int y, int button);
		void mousePressed(int x, int y, int button);
		void mouseReleased(int x, int y, int button);
		void mouseEntered(int x, int y);
		void mouseExited(int x, int y);
		void windowResized(int w, int h);
		void dragEvent(ofDragInfo dragInfo);
		void gotMessage(ofMessage msg);
		void onNewMessage(string & message);
		
		testApp justTesting;
};

ofApp.cpp:

#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup(){
	justTesting.setup();
}

//--------------------------------------------------------------
void ofApp::update(){
	justTesting.update();
}

//--------------------------------------------------------------
void ofApp::draw(){
	justTesting.draw();
}

//--------------------------------------------------------------
void ofApp::keyPressed(int key){
	justTesting.keyPressed(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){
	
	justTesting.mousePressed(x, y, button);
}

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

}

//--------------------------------------------------------------
void ofApp::onNewMessage(string & message){
	
	ofLog(OF_LOG_NOTICE, "onNewMessage in ofApp is running!");
	justTesting.onNewMessage(message);
}

testApp.h:

#ifndef _TEST_APP
#define _TEST_APP

#include "ofMain.h"
#include "ofxSimpleSerial.h"

using namespace std;

class testApp : public ofBaseApp{
	
	public:
		ofxSimpleSerial	serial;

		string		message;
	
		bool		red;
		bool		green;
		bool		blue;
		bool		requestRead;
	
		void		setup();
		void		update();
		void		draw();
		void		mousePressed(int x, int y, int button);
		void 		keyPressed(int key);
		void		onNewMessage(string & message);
	
	protected:
	
	
		
};	

#endif

testApp.cpp:

#include "testApp.h"


//--------------------------------------------------------------
void testApp::setup()
{	 
	ofSetVerticalSync(true);
	ofBackground(0, 0, 0);
	ofLog(OF_LOG_NOTICE, "Background is set to 000.");
	
	serial.setup("/dev/ttyAMA0", 9600);
	serial.startContinuousRead();
	ofLog(OF_LOG_NOTICE, "Serial connection should be workin by now.");
	
	ofAddListener(serial.NEW_MESSAGE,this,&testApp::onNewMessage);
	ofLog(OF_LOG_NOTICE, "Listener event set.");
	
	message = "";
}

void testApp::onNewMessage(string & message)
{
	ofLog(OF_LOG_NOTICE, "onNewMessage is working!");
	cout << "onNewMessage, message: " << message << "\n";
	
	red = (message == "0006");
	green = (message == "0007");
	blue = (message == "800000000060");
}

void testApp::update()
{
	
}

//--------------------------------------------------------------
void testApp::draw()
{
	int redColor = (red)? 255 : 0;
	int greenColor = (green)? 255 : 0;
	int blueColor = (blue)? 255 : 0;
	
	ofBackground(redColor, greenColor, blueColor);
}

//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
	serial.sendRequest();
	ofLog(OF_LOG_NOTICE, "mouse pressed!");
}

void testApp::keyPressed(int key){
	serial.sendRequest();
	ofLog(OF_LOG_NOTICE, "key pressed!");
}

terminal logs:

[notice ] Background is set to 000.
[notice ] ofSerial: opening /dev/ttyAMA0 @ 9600 bps
[notice ] ofSerial: opened /dev/ttyAMA0 sucessfully @ 9600 bps
[notice ] Serial connection should be workin by now.
[notice ] Listener event set.
[notice ] key pressed!
[notice ] key pressed!
[notice ] key pressed!

This addon expects that the final character of a msg that comes from the arduino has ‘/n’, look at line 55, if not the callback never get fired

Thanks for the info.
I’m not communicating with arduino and each message ends with CR (0x0D) so that’s why it didn’t worked for me.

Yep, sorry, whatever serial device msg not only arduino :slightly_smiling:

I recommend you to use this:

Right now I’m using this code, but thanks for the tip!

	if(mySerialConnection.available())
	{
		keepReadingSerial=true;
		while(keepReadingSerial)
		{
			char myByte = 0;
			myByte = mySerialConnection.readByte();
			if(myByte==0x0D)
			{
				keepReadingSerial=false;
			}
			else
			{
				message+=myByte;
			}
		}
	}