c open()未定义错误:os x上为0

Chi*_*hip 3 c macos serial-port

在运行10.8的mac上我试图打开一个串口.

ls/dev/cu*返回:

/dev/cu.Bluetooth-Modem     /dev/cu.Bluetooth-PDA-Sync  /dev/cu.usbserial-A1009TT7
Run Code Online (Sandbox Code Playgroud)

我可以看到端口在那里,但当我尝试打开它时,我得到未定义的错误:0(0).这是我用来打开端口的代码.

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;
}
Run Code Online (Sandbox Code Playgroud)

任何人都知道为什么港口不会打开?

提前致谢.

Die*_*Epp 5

哎呦!你打算输入这个:

file = open(path , O_RDWR | O_NOCTTY | O_NONBLOCK);
^^^^^^^
Run Code Online (Sandbox Code Playgroud)

此外,无需关闭未打开的文件描述符.

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