【发布时间】:2014-03-27 16:57:14
【问题描述】:
我无法在 linux ubuntu 中打开串口开始通信。我试过这个:
int OpenPort(void) {
int fd;
fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
if(fd == -1)
{
printf("Failed to open port.\n");
}
else
{
fcntl(fd, F_SETFL, 0);
printf("Opened!\n");
}
return(fd);
}
int main()
{
int x = OpenPort();
printf("%i\n", x);
exit(0);
}
我是 linux 新手,在网上找到了这段代码,但它对我不起作用。
【问题讨论】:
-
错误是什么?尝试将
printf("Failed to open port.\n")替换为printf("Failed to open port: %s.\n", strerror(errno))或perror("Failed to open port.")。 -
错误是权限被拒绝
标签: c linux ubuntu serial-port communication