【发布时间】:2021-07-27 08:54:42
【问题描述】:
我想将 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是什么意思?
【问题讨论】:
-
man cu和man tty -
好的,但是为什么我不能使用 tty 呢?
-
to a linux computer.你确定是linux而不是macos?why can't I use tty then?什么意思?为什么你不能?请注意,您问过What is the meaning of cu and tty?和 not“为什么我不能使用 tty”或类似的问题。源代码是否与您的问题相关?请考虑提出一个单独的问题。