简体   繁体   English

串口在linux ubuntu中无法打开

[英]Serial port won't open in linux ubuntu

I can't open serial port to start communication in linux ubuntu. 我无法打开串行端口以在linux ubuntu中开始通信。 I've tried this: 我已经试过了:

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);
}

I'm new in linux and found this code online, but it doesn't work for me. 我是Linux的新手,可以在线找到此代码,但对我而言不起作用。

You need to run as superuser/root to access serial port in linux. 您需要以超级用户/ root身份运行才能访问linux中的串行端口。 Try running your binary as sudo . 尝试将二进制文件作为sudo运行。 If you can verify this is the problem but you do not want your process to be run by root user there are options you can use in your code to obtain root privileges. 如果可以验证这是问题所在,但又不想由root用户运行进程,则可以在代码中使用一些选项来获取root特权。 This answer might be useful reading How to programmatically gain root privileges? 阅读如何如何以编程方式获取root特权,此答案可能很有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM