我无法在 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 的新手,在网上找到了这段代码,但它对我不起作用。
最佳答案
您需要以 super 用户/root 身份运行才能访问 linux 中的串口。尝试以 sudo
运行您的二进制文件。如果您可以验证这是问题所在,但您不希望您的进程由 root 用户运行,您可以在代码中使用一些选项来获得 root 权限。这个答案可能对阅读有用 How to programmatically gain root privileges?
关于c - 串口在linux ubuntu中打不开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22014711/