我正在尝试与我的 mac OS X Yosemite 的电源通信。该代码在 Linux 机器上运行良好,但当我在 Mac 上尝试时,它不起作用。我正在使用 USB 串行转换器并已下载 PL-2303 驱动程序。该驱动程序在我的/dev 文件夹中显示为 cu.usbserial 和 tty.usbserial。
我的代码失败的部分:
fd = initserial("/dev/cu.usbserial");
int initserial(char port[])
{
struct termios shimtermios;
int fd;
if((fd=open(port,O_RDWR)) < 0) {
perror("Opening the RS-232 port failed for initserial\n");
exit(-1);
}
if (tcgetattr(fd, &shimtermios) < 0) {
perror("couldn't get terminal attributes\n");
exit(-2);
}
shimtermios.c_iflag=012005;
shimtermios.c_oflag=014004;
shimtermios.c_cflag=03206276;
shimtermios.c_lflag=05060;
cfsetospeed(&shimtermios,B19200);
if (tcsetattr(fd, TCSAFLUSH, &shimtermios) < 0) {
perror("couldn't set terminal attributes\n");
exit(-3);
}
return (fd);
}
我收到以下错误消息
couldn't set terminal attributes
: Invalid argument
如果您对这个 linux/unix 问题有任何经验,请告诉我。非常感谢!
最佳答案
请参阅termios
man page可用于 c_iflag
、c_oflag
、c_cflag
和 c_lflag
的有效掩码)。您应该将掩码常量组合在一起,而不是对值进行硬编码。
根据经验,FTDI USB 串行转换器在 OS X 上的运行效果往往比 pl2303 更好。 OS X 有一个内置的驱动程序 (AppleUSBFTDI)。
关于linux - 使用 USB 串行转换器通过 mac os x 与电源通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32343365/