fd = open("/dev/ttyUSB0",O_RDWR | O_NOCTTY);
/* O_NOCTTY - No terminal will control the process */
为什么要指定或不指定这个选项?它做什么和不做什么?
最佳答案
这在 GNU C library 的文档中得到了回答。 :
If the named file is a terminal device, don’t make it the controlling terminal for the process. See Job Control, for information about what it means to be the controlling terminal.
On GNU/Hurd systems and 4.4 BSD, opening a file never makes it the controlling terminal and
O_NOCTTY
is zero. However, GNU/Linux systems and some other systems use a nonzero value forO_NOCTTY
and set the controlling terminal when you open a file that is a terminal device; so to be portable, useO_NOCTTY
when it is important to avoid this.
也就是说,由于 USB 串行设备被视为终端,使用标志是您不希望它像终端并允许设备启动/停止恰好与给定用户连接的作业(例如,进程)。
进一步阅读:
关于c - 为什么串口要设置 'no terminal should control the process'选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54360113/