Chip
Chip

Reputation: 129

c open() Undefined error: 0 on os x

On a mac running 10.8 i am trying to open a serial port.

ls /dev/cu* returns:

/dev/cu.Bluetooth-Modem     /dev/cu.Bluetooth-PDA-Sync  /dev/cu.usbserial-A1009TT7

i can see the port is there but when i try to open it i get Undefined error: 0(0). This is my code i use to open the port.

char *path = "/dev/cu.usbserial-A1009TT7";

open(path , O_RDWR | O_NOCTTY | O_NONBLOCK);     // open the port

if (file == -1) {
    printf("Error opening port : %s(%d).\n", strerror(errno), errno);
    close(file);
    return -1;
}

anyone have any idea why the port wont open?

thanks in advance.

Upvotes: 3

Views: 1956

Answers (1)

Dietrich Epp
Dietrich Epp

Reputation: 213308

Whoops! You meant to type this:

file = open(path , O_RDWR | O_NOCTTY | O_NONBLOCK);
^^^^^^^

Also, there is no need to close a file descriptor that isn't open.

if (file == -1) {
    printf(...);
    // close(file); Completely unnecessary.  It's not valid!
    return -1;
}

Upvotes: 5

Related Questions