[ error ] ofSerial: writeByte(): serial not inited

Hi, everyone!

I am currently trying to build simple connection between TouchOsc, Openframeworks and Arduino. And I met with the problem sending data from OF to Arduino. So I tried to write the simple code example in book: Programming Interactivity. it appears the problem of [ error ] ofSerial: writeByte(): serial not inited, and there is no reaction or feedback from arduino side.

Can anyone help me out of this? Thank you!

in OF, ofApp.h:

#pragma once
#ifndef _OF_SEND_SERIAL
#define _OF_SEND_SERIAL
#include “ofMain.h”

class ofApp : public ofBaseApp{

public:
	void setup();
	void update();
	void keyPressed(int key);
	void keyReleased(int key);

    ofSerial serial;    // here's our ofSerial object that we'll use;

};
#endif

in OF, ofApp.h:

#include “ofApp.h”
//--------------------------------------------------------------
void ofApp::setup(){
serial.setup("/dev/tty.usbmodem411", 115200);
}

void ofApp::keyPressed(int key){
serial.writeByte(key);
}

in Arduino:
int message = 0; // for incoming serial data
int ledPin = 13;

void setup(){
pinMode(ledPin, OUTPUT);
Serial.begin(115200); // opens serial port, sets data rate to 9600 bps
}
void loop() {
// do something only when we receive data:
if (Serial.available() > 0) {// read the incoming byte:
message = Serial.read();
if(message == ‘!’) {
digitalWrite(ledPin, HIGH);
}
if(message == ‘?’) {
digitalWrite(ledPin, LOW);
}
}
}

Hi.

it might help to turn on verbose logging:
ofSetLogLevel(OF_LOG_VERBOSE);

you can print out a list of your serial devices like this:
serial.listDevices();

last but not least, you can check what serial.setup()returns:
if (!serial.setup("/dev/tty.usbmodem411", 115200)) {OF_EXIT_APP(0);}

setup() could look like this:

void ofApp::setup(){
     ofSetLogLevel(OF_LOG_VERBOSE);
     if(!serial.setup("/dev/tty.usbmodem411", 115200)) {
          // log and error and leave
          ofLogError() << "could not open serial port - listing serial devices";
          serial.listDevices();
          OF_EXIT_APP(0);
     }
}

does the serial example (examples/communication/serialExample) work?

Hi, thank you for your reply.

I tried your this code and it will exit soon after I run the code.

Below is the error listed:

[notice ] ofSerial: opening /dev/tty.usbmodem411 @ 115200 bps
[ error ] ofSerial: unable to open /dev/tty.usbmodem411
[notice ] ofSerial: opening /dev/tty.usbmodem411 @ 115200 bps
[ error ] ofSerial: unable to open /dev/tty.usbmodem411
[ error ] could not open serial port - listing serial devices
[notice ] ofSerial: [0] = tty.usbmodem1411
[notice ] ofSerial: [1] = cu.usbmodem1411
[notice ] ofSerial: [2] = tty.Bluetooth-Incoming-Port
[notice ] ofSerial: [3] = cu.Bluetooth-Incoming-Port

jep. it exits because the serial device /dev/tty.usbmodem411 could not be opened.

looking at the list the correct device seems to be:
/dev/tty.usbmodem1411

yeah, I know /dev/tty.usbmodem1411 is the port I am connecting with arduino. And I tried with the same example in processing, then it works. It’s a little bit tricky it doesn’t work in OF.

/dev/tty.usbmodem1411 does not open in OF?

note that in the code you posted the port you are trying to open is 411 not 1411. the error is also saying just that, port 411 not available and then in the list it shows 1411 as available.

ahh… So stupid mistake…

Thank you for the help!