0

我现在正在使用以下代码,我想知道它哪里错了。

struct termios options;
int fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
fcntl(fd, F_SETFL, 0);
tcgetattr(fd, &options);
cfsetispeed(&options, B9600);
tcsetattr(fd, TCSANOW, &options);
4

1 回答 1

0

你可以试试下面的代码,看看它是否有帮助?

int open_port(void)
{
    int fd;

fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{
    perror("open_port: Unable to open /dev/ttyS0 - ");
}
else
    fcntl(fd, F_SETFL, 0);

struct termios options;

tcgetattr(fd, &options);

cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);

tcsetattr(fd, TCSANOW, &options);

return (fd);
}
于 2013-03-03T20:41:45.517 回答