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 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

......................................................

在我的主程序中,我做了一个 Ret=LS.Open(DEVICE_PORT,4800);
Ret 值为 -2,这意味着打开设备时出现错误,如上所述 可能是权限问题吗?我该如何解决这个问题并打开端口,谢谢!

最佳答案

could it be a permission problem

很有可能。根据您使用的 Linux,有多种更改权限的选项。

这可能就像将当前用户添加到 daiout 组一样简单。或者您可能需要更改 udev 规则来设置串口的权限。

关于c++ - linux下无法打开串口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9223238/

相关文章:

c++ - 是否可以在 vector 的构造函数中使用 lambda 函数?

c++ - 如何在C/C++中输出unicode字符

linux - 当我进行搜索时,如何禁用 Vim 滚动时键入?

c - recvfrom() 会修改 len 吗?

c++ - 如何命名 C++ 捕获变量?

c++ - 一旦对 std::weak_ptr 的锁定返回了 nullptr,它还能再次为非空吗?

c# - 从 C : fputc and fwrite in C#? 转换

linux - 使用 iptables 记录所有到关闭端口的连接

linux - 将具有序列的文件重命名为新序列

c - 在 gcc 中编译时出现警告有何影响?可能会产生什么后果?