Wirting to Serial causes problems in c++ class

I have a class that holds an ofSerial object as an instance variable, when I try to write to it, it gives me a:

[ error ] ofSerial: writeByte(): couldn't write to port: 9 Bad file descriptor

I’ve checked and the ofSerial port is properly initialized.

If I call the port in my main.cpp, it writes fine, but, as soon as I pass it to a class it fails like above. Any idea why this might be happening?

When you “pass it to a class” are you making a copy of it or passing a pointer of reference to the original instance that you confirmed to be working? If you aren’t passing it by reference or pointer, then you are copying it, which means it will be de-initialized.

I was passing it by copy. I tried initializing the serial port in the class itself instead and am still getting the same issue.

Strange, could you share some code?

Class declaration:

class LedStrand {

public:
  
void updateHardware();

ofSerial portAddress;

LedStrand();
LedStrand(int portDevicePos, int numPixels, int baud);
    
};

The definition:

LedStrand::LedStrand()
{
}

LedStrand::LedStrand(int portDevPos, int numPixels, int baud)
{

portAddress.setup(portDevPos, baud);

}

void LedStrand::updateHardware()
{
    portAddress.writeByte('#');
    ofSleepMillis(100);
}

ofApp.h has a member variable:

LedStrand ledStrand;

In ::setup() (this is in a loop):

ledStrand = LedStrand(i, totalPixels, baud);

In ::draw():

        ledStrand.updateHardware();

^ When the serial tries to write is when the error occurs. If I setup the port in ofApp.cpp, I don’t have the same issue. Let me know if you need to see any more code.

I added a little check before the updateHardware() call:

ofLog(OF_LOG_NOTICE, portAddress.isInitialized() ? "yes" : "no");

It’s coming up “yes”.

I’ve also tried creating the ofSerial object and passing it by reference to the constructor, I am still getting the above error.

An update:

I get the:

ofSerial: writeByte(): couldn't write to port: 9 Bad file descriptor

The first few times I write but then the program appears to be writing correctly.

[notice ] ofSerial: opening /dev/tty.usbmodem663631 @ 115200 bps
[notice ] ofSerial: opened /dev/tty.usbmodem663631 sucessfully @ 115200 bps  
[notice ] setting up ledStrandLeftTest
[notice ] ofSerial: opening /dev/tty.usbmodem666371 @ 115200 bps
[notice ] ofSerial: opened /dev/tty.usbmodem666371 sucessfully @ 115200 bps
[notice ] setting up ledStrandRightTest
[notice ] updating
[notice ] writing to left
[notice ] writing to right
[ error ] ofSerial: writeByte(): couldn't write to port: 9 Bad file descriptor
[ error ] ofSerial: writeBytes(): couldn't write to port: 9 Bad file descriptor
[notice ] updating
[notice ] writing to left
[notice ] writing to right
[ error ] ofSerial: writeByte(): couldn't write to port: 9 Bad file descriptor
[ error ] ofSerial: writeBytes(): couldn't write to port: 9 Bad file descriptor
[notice ] updating
[notice ] writing to left
[notice ] writing to right
[notice ] updating
[notice ] writing to left
[notice ] writing to right
[notice ] updating
[notice ] writing to left
[notice ] writing to right

Is there any reason it would start working after a couple of tries?

It takes time to establish a serial connection as Arduino’s reset when a serial connection is made. Note the time it takes when you open the serial monitor from the arduino IDE. That could be a problem though I have to admit I don’t think I have ever gotten that error before.

Also the port.isInitialized isn’t really an accurate indication if you look at the ofSerial code. I wouldn’t use it to check the connection as it really only checks that the initial code ran without error.

The bad file descriptor error means that the open was successful but that the descriptor was not valid for writing. Is it only initially that it happens or randomly/consistently? if it only happens when the communication is first established I wouldn’t be overly concerned.

I would suggest lowering the baud rate of your connection. Arduino’s can communicate at 115200 but its not perfect and unless you are pushing a lot of data halving it might help with your issue, especially if its a one way connection. i.e. you’re sending data not not receiving any.

Hi @Collin_Schupman, I have the same problem, how did you fix it? Thanks!