ofxScene and serial communication

Hello, I am doing an app with different scenes and I am using ofxSceneManager addon. I would like to be able to use the same serial port between the different scenes, but I can’t because when I change of Scene, the another scene is using this port.
Where should I setup and connect uart port? Maybe in ofApp.cpp?

yes create an ofSerial in ofApp and pass a reference to it to the scenes. usually the easiest is to use a shared_ptr to share the ownership of the object among different objects, like:


shared_ptr<ofSerial> serial(new ofSerial);


//create scenes:
for(auto & scene: scenes){

in every scene:

shared_ptr<ofSerial> serial;

void MyScene::setup(shared_ptr<ofSerial> serial){
    this->serial = serial;
1 Like

Thank you! It doesn’t work… I think I am not doing it correctly. I have this problem:

Exception thrown: read access violation.

this was nullptr.

If there is a handler for this exception, the program may be safely continued.

be sure that you are setting the serial object to every scene before you start using it, otherwise it’ll be null and the application will crash

also be sure that in ofApp.h when you declare the serial object you also initialize it like:

shared_ptr<ofSerial> serial(new ofSerial);

if you are not sure when the object is going to be initialized you can add in your secenes whenever you are going to use the serial pointer:

    ofLogError() << "trying to use null serial from scene blah";
1 Like

I had problems with the word “new”:

So, I read in a book, I should write “shared_ptr serial(new ofSerial);” in ofApp.cpp setup. Whatsmore, that’s the example I read.

void ofApp::setup(){
shared_ptr a(new int);
*a = 5;
vector<shared_ptr > v;

And well… If I put “shared_ptr serial(new ofSerial);” in ofApp.cpp, it doesn’t work… but I don’t have problems with the word “new”