Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无法在linux中打开串行端口_C++_Linux - Fatal编程技术网

C++ 无法在linux中打开串行端口

C++ 无法在linux中打开串行端口,c++,linux,C++,Linux,使用fedora 11 在串行类中: /* \return 1 success \return -1 device not found \return -2 error while opening the device \return -3 error while getting port parameters \return -4 Speed (Bauds) not recognized \return -5 error while writing port param

使用fedora 11

在串行类中:

/*
     \return 1 success 

\return -1 device not found

\return -2 error while opening the device

\return -3 error while getting port parameters

 \return -4 Speed (Bauds) not recognized

\return -5 error while writing port parameters

\return -6 error while writing timeout parameters

*/
`char serialib::Open(const char *Device,const unsigned int Bauds){`

    struct termios options; // Structure with the device's options
    // Open device
    fd = open(Device, O_RDWR | O_NOCTTY | O_NDELAY);            // Open port
    if (fd == -1) return -2;                        // If the device is not open, return -1
    fcntl(fd, F_SETFL, FNDELAY);                    // Open the device in nonblocking mode

[termina]$ dmesg | grep tty console [tty0] enabled serial8250: ttyS0 at I/O 0x3f8 (irq = 4) is a 16550A serial8250: ttyS1 at I/O 0x2f8 (irq = 3) is a 16550A 00:07: ttyS0 at I/O 0x3f8 (irq = 4) is a 16550A 00:08: ttyS1 at I/O 0x2f8 (irq = 3) is a 16550A [termina]$dmesg | grep tty 控制台[tty0]已启用 serial8250:I/O 0x3f8(irq=4)处的ttyS0是16550A serial8250:I/O 0x2f8(irq=3)处的ttyS1是16550A 00:07:I/O 0x3f8(irq=4)处的ttyS0为16550A 00:08:I/O 0x2f8(irq=3)处的ttyS1为16550A

[termina]$ dmesg | grep tty console [tty0] enabled serial8250: ttyS0 at I/O 0x3f8 (irq = 4) is a 16550A serial8250: ttyS1 at I/O 0x2f8 (irq = 3) is a 16550A 00:07: ttyS0 at I/O 0x3f8 (irq = 4) is a 16550A 00:08: ttyS1 at I/O 0x2f8 (irq = 3) is a 16550A 在我的主程序中,我执行Ret=LS.Open(设备_端口,4800)
Ret值为-2,这意味着如上所述打开设备时出错 这可能是许可问题吗?如何解决此问题并打开端口,谢谢

可能是权限问题吗

很有可能。根据您使用的linux,有几个选项可用于更改权限


它可能很简单,只需将当前用户添加到dailout组。或者,您可能需要更改udev规则以设置串行端口的权限。

“可能是”?你的意思是你没有费心检查
errno
?这就是我想知道的,不是
open
返回
-1
时显示的错误,而是添加一行来打印实际错误。这可以通过函数来完成。如果您不理解错误消息,请编辑您的问题以将其包括在内。您是否以root用户身份运行?或者(为了测试),他可以临时允许“其他人”完全访问串行端口。