简体   繁体   中英

Why should 'no terminal should control the process' option be set in serial port?

fd = open("/dev/ttyUSB0",O_RDWR | O_NOCTTY);    
                               /* O_NOCTTY - No terminal will control the process   */

Why should we specify or not specify this option? What does it do and doesn't do?

That's answered in the documentation for 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 for O_NOCTTY and set the controlling terminal when you open a file that is a terminal device; so to be portable, use O_NOCTTY when it is important to avoid this.

That is, since the USB serial device is treated like a terminal, use the flag is you don't intend for it to be like a terminal and allow the device to start/stop jobs (eg, processes) which happen to be connected to it with a given user.

Further reading:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM