为什么 fcntl(fd, F_SETFL, 0) 用在串口编程中 [英] Why fcntl(fd, F_SETFL, 0) use in serial port programming

查看:21
本文介绍了为什么 fcntl(fd, F_SETFL, 0) 用在串口编程中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始在 Linux 中进行串口编程.在网上看了几个例子后,我不明白 fcntl(fd, F_SETFL, 0) 的确切效果?它正在清除位,但它会影响哪些标志?它设置和或清除了什么?

I am starting serial port programming in Linux. After reading several examples on the web, I don't understand exact effect of fcntl(fd, F_SETFL, 0)? It is clearing bits, but what flags does it affect? What does it set and or clear?

推荐答案

一一道来

1) 使用的函数调用

fcntl() - 它对传入参数的文件描述符执行操作.

fcntl() - It perform the operation on file descriptor passed in argument.

2) 调用中的第二个参数

2) 2nd argument in call

F_SETFL (int)

将文件状态标志设置为 arg 指定的值.文件访问模式(O_RDONLY、O_WRONLY、O_RDWR)和文件创建标志(即,arg 中的 O_CREAT、O_EXCL、O_NOCTTY、O_TRUNC) 被忽略.在 Linux 上这个命令只能更改 O_APPEND、O_ASYNC、O_DIRECT、O_NOATIME、和 O_NONBLOCK 标志.

Set the file status flags to the value specified by arg. File access mode (O_RDONLY, O_WRONLY, O_RDWR) and file creation flags (i.e., O_CREAT, O_EXCL, O_NOCTTY, O_TRUNC) in arg are ignored. On Linux this command can change only the O_APPEND, O_ASYNC, O_DIRECT, O_NOATIME, and O_NONBLOCK flags.

3) 调用中的第三个参数

3) 3rd argument in call

0 表示,设置 文件状态标志归零.正如 Jean-Baptiste Yunès 在评论中所说的那样.

It is 0 means, It set file status flag to zero. As Jean-Baptiste Yunès said in comment.

文件访问模式和文件创建标志被忽略.这个命令重置所有其他标志:没有附加,没有异步,没有直接,没有时间,和没有非阻塞

file access mode and file creation flags are ignored. This command reset every other flags: no append, no async, no direct, no atime, and no nonblocking

<小时>

终于

fcntl(fd, F_SETFL, 0)

此调用会将打开的文件描述符的 file status 标志设置为值 0.

This call will set opened file desciptor's file status flag to value 0.

但理想情况下,我们不应该更改文件状态标志.最好的方法是首先使用 F_GETFL 获取当前文件状态标志,然后在其中更改所需的位.见例子:

But idealy this way we should not change file status flag. Best way is to first get the current file status flag using F_GETFL and then just change required bit in that. See example:

如果要修改文件状态标志,应使用 F_GETFL 获取当前标志并修改值.不要假设这里列出的标志是唯一实现的标志;您的程序可能会在几年后运行,然后可能会存在更多标志.例如,这是一个设置或清除标志 O_NONBLOCK 而不更改任何其他标志的函数:

If you want to modify the file status flags, you should get the current flags with F_GETFL and modify the value. Don’t assume that the flags listed here are the only ones that are implemented; your program may be run years from now and more flags may exist then. For example, here is a function to set or clear the flag O_NONBLOCK without altering any other flags:

/* Set the O_NONBLOCK flag of desc if value is nonzero,
   or clear the flag if value is 0.
   Return 0 on success, or -1 on error with errno set. */

int
set_nonblock_flag (int desc, int value)
{
  int oldflags = fcntl (desc, F_GETFL, 0);
  /* If reading the flags failed, return error indication now. */
  if (oldflags == -1)
    return -1;
  /* Set just the flag we want to set. */
  if (value != 0)
    oldflags |= O_NONBLOCK;
  else
    oldflags &= ~O_NONBLOCK;
  /* Store modified flag word in the descriptor. */
  return fcntl (desc, F_SETFL, oldflags);
}

这篇关于为什么 fcntl(fd, F_SETFL, 0) 用在串口编程中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆