我不能打开串口来启动linux中的通信。我试过这个:
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的新手,在网上发现了这段代码,但它对我不起作用。
发布于 2014-02-25 12:54:13
您需要作为超级用户/超级用户运行,以访问linux中的串口。尝试以sudo的形式运行二进制文件。如果您可以验证这是问题所在,但是不希望您的进程由root用户运行,那么您可以在代码中使用一些选项来获得根权限。这个答案可能对阅读How to programmatically gain root privileges?很有用。
https://stackoverflow.com/questions/22014711
复制相似问题