gpt4 book ai didi

c - 打开函数不返回串口

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:32:46 24 4
gpt4 key购买 nike

我有一个 USB 串行端口模块,我想在我的 C 应用程序中使用它。问题是 open 系统调用没有返回!

int serialDevice = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY); // doesn't return

我将我的用户添加到拨出组,我可以在终端中使用 minicom 访问端口而无需 root 访问权限。所以这不是权限问题。即使是,它也应该立即返回并给出权限错误。但是 open 函数根本不返回。

我该怎么办?你有什么想法吗?

谢谢

最佳答案

我相信如果设备正忙等情况下可能会发生这种情况。

您需要添加标志O_NONBLOCK:

O_NONBLOCK When opening a FIFO with O_RDONLY or O_WRONLY set: * If O_NONBLOCK is set, an open() for reading-only shall return without delay. An open() for writing-only shall return an error if no process currently has the file open for reading. * If O_NONBLOCK is clear, an open() for reading-only shall block the calling thread until a thread opens the file for writing. An open() for writing-only shall block the calling thread until a thread opens the file for reading.

When opening a block special or character special file that supports non-blocking opens:

*
If O_NONBLOCK is set, the open() function shall return without blocking for the device to be ready or available. Subsequent behavior

of the device is device-specific. * If O_NONBLOCK is clear, the open() function shall block the calling thread until the device is ready or available before returning.

Otherwise, the behavior of O_NONBLOCK is unspecified.

关于c - 打开函数不返回串口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55157876/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com