我想将 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 是什么意思?