3

10.8 を実行している Mac で、シリアル ポートを開こうとしています。

ls /dev/cu* は以下を返します:

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

ポートがそこにあることがわかりますが、それを開こうとすると、未定義のエラーが発生します: 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;
}

ポートが開かない理由を知っている人はいますか?

前もって感謝します。

4

1 に答える 1

5

おっと!これを入力するつもりでした:

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

また、開いていないファイル記述子を閉じる必要はありません。

if (file == -1) {
    printf(...);
    // close(file); Completely unnecessary.  It's not valid!
    return -1;
}
于 2012-12-04T13:28:16.530 に答える