0

我想将 Arduino 连接到 linux 计算机。所有设备都列在/dev/*。例如,当我连接一个原始的 Arduino mega 时,我会得到这些额外的设备:

/dev/tty.usbmodem...
/dev/cu.usbmodem...

现在,当我想使用此 C 函数从串行端口读取数据时:

void read_from_serial() {
    int fd = open("/dev/cu.usbmodem14101", O_RDWR | O_NOCTTY | O_SYNC);
    if(fd < 0) {
        printf("Failed to open connection to device.\n");
        return;
    }

    struct termios tty;
    if (tcgetattr(fd, &tty) < 0) {
        printf("Error: %s\n", strerror(errno));
        return;
    }

    cfsetospeed(&tty, (speed_t)B115200);
    cfsetispeed(&tty, (speed_t)B115200);

    if (tcsetattr(fd, TCSANOW, &tty) != 0) {
        printf("Error: %s\n", strerror(errno));
        return;
    }

    tcflush(fd, TCIOFLUSH);

    char c;
    while (read(fd, &c, 1) == 1) {
        putchar(c);
    }

    close(fd);
}

我需要连接到/dev/cu...而不是连接到/dev/tty...cu 和 tty 是什么意思?

4

1 回答 1

1

这些设备连接名称是什么?

cu和tty是什么意思?

tty代表“TeleTYpewriter”,来自https://en.wikipedia.org/wiki/Tty_(unix)

cu代表“呼叫”设备,用于呼叫它们的设备,如调制解调器,来自https://pbxbook.com/other/mac-tty.html

于 2021-07-27T09:25:14.223 回答