Termios struct description
In the termios struct, the struct generally includes the following members:
Tcflag_t c_iflag;
Tcflag_t c_oflag;
Tcflag_t c_cflag;
Tcflag_t c_lflag;
Cc_t c_cc [NCCS];
Its significance is as follows:
C_iflag: indicates the input mode and controls the input mode of the terminal. The specific parameters are as follows.
C_iflag parameter table
Key value description
IGNBRK ignores BREAK key input
If IGNBRK is set for BRKINT, the BREAK key input will be ignored. If BRKINT is set, SIGINT interruption will occur.
IGNPAR ignores parity errors
An error occurred while verifying the parity of the parmrk id.
INPCK allows input parity
ISTRIP removes 8th characters
INLCR converts the input NL (line feed) to CR (Press ENTER)
IGNCR ignore input carriage return
ICRNL converts the input carriage return to line feed (if IGNCR is not set)
IUCLC converts the input uppercase characters into lowercase characters (non-POSIX)
IXON allows control of the XON/XOFF stream during Input
IXANY output with any characters that will be restarted and stopped
IXOFF allows control of the XON/XOFF stream during Input
IMAXBEL starts to ring when the input queue is full. in Linux, this parameter is used, but the parameter is always set
C_oflag: the output mode flag that controls the terminal output mode. The specific parameters are as follows.
C_oflag Parameters
Key value description
Output after OPOST Processing
OLCUC converts the input lowercase characters to uppercase (non-POSIX)
ONLCR converts the input NL (line feed) into CR (Press ENTER) and NL (line feed)
OCRNL converts the input CR (Press ENTER) to NL (line feed)
The first line of ONOCR does not output a carriage return.
ONLRET does not output carriage return
OFILL sends padding characters to delay terminal output
OFDEL uses the DEL of the ASCII code as the filling character. If this parameter is not set, the filling character will be NUL ('/0') (non-POSIX)
NLDLY line feed output delay, which can be NL0 (no delay) or NL1 (0.1 s delay)
CRDLY carriage return delay, value range: CR0, CR1, CR2
TABDLY horizontal tab output delay. Values: TAB0, TAB1, TAB2, and TAB3
BSDLY space output delay, which can be BS0 or BS1
VTDLY vertical tab output delay, which can be VT0 or VT1
FFDLY paging latency, which can be FF0 or FF1
C_cflag: Control Mode flag that specifies the hardware control information of the terminal. The specific parameters are as follows.
C_oflag Parameters
Key value description
CBAUD baud rate (4 + 1 bit) (non-POSIX)
CBAUDEX additional baud rate (1 bit) (non-POSIX)
CSIZE length, which can be CS5, CS6, CS7, or CS8
CSTOPB sets two stop bits
CREAD Receiver
PARENB uses parity
PARODD uses parity for input and parity for output
HUPCL is suspended when the device is disabled
CLOCAL ignores the modem line status
CRTSCTS uses RTS/CTS Stream Control
C_lflag: The local mode flag that controls the terminal editing function. The specific parameters are as follows.
C_lflag Parameter
Key value description
ISIG generates signals when INTR, QUIT, SUSP, or DSUSP is input.
ICANON uses standard input mode
When both ICANON and XCASE are set for XCASE, the terminal only uses uppercase letters. If only XCASE is set, the input characters are converted to lowercase characters, unless the characters use escape characters (non-POSIX, and Linux does not support this parameter)
ECHO display input characters
ECHOE if ICANON is set at the same time, ERASE will delete the entered characters, and WERASE will delete the entered words
ECHOK: If ICANON is set at the same time, KILL will delete the current row
If both ICANON and ECHONL are set, even if ECHO is not set, the line break is still displayed.
ECHOPRT if both ECHO and ICANON are set, the printed characters (non-POSIX) will be deleted)
TOSTOP sends the SIGTTOU signal to the background
Functions related to this struct
(1) tcgetattr ()
1.Prototype
Int tcgetattr (int fd, struct termois & termios_p );
2.Function
Obtain the initial value of the terminal media (fd) and assign it to temios_p. The function can be called from the background process. However, the terminal property may be changed by the foreground process.
(2) tcsetattr ()
1. Prototype
Int tcsetattr (int fd, int actions, const struct termios * termios_p );
2. Functions
Set the terminal-related parameters (unless the underlying support is required but cannot be met) and use the termios structure referenced by termios_p. Optional_actions (the second parameter of the tcsetattr function) specifies when the change will take effect:
TCSANOW: changes occur immediately
TCSADRAIN: changes take effect after all output data written to fd is transmitted. This function should be used to modify parameters that affect output. (Changes the value when the current output is complete)
TCSAFLUSH: changes take effect after the output of all objects referenced by writing fd is transmitted. All accepted but unread inputs are discarded before changes (same as TCSADRAIN, but all current values are discarded ).
(3) tcsendbreak ()
Transmits a continuous 0-bit stream for a period of time, if the terminal uses asynchronous serial data transmission. If duration is 0, it must be transmitted for at least 0.25 seconds, not more than 0.5 seconds. If duration is not zero, the length of time it sends is defined by the implementation.
If the terminal does not use asynchronous serial data transmission, tcsendbreak () does nothing.
(4) tcdrain ()
Wait until the output of all objects referenced by writing fd is transmitted.
(5) tcflush ()
Discard the referenced object to be written, but the data has not been transmitted, or the data received but not yet read depends on the value of queue_selector:
TCIFLUSH: refreshes the received data but does not read it.
TCOFLUSH: refreshes the written data but does not transmit it.
TCIOFLUSH: refresh the received data but not read, and refresh the written data but not transmit it.
(6) tcflow ()
Data transmission or receipt on the object referenced by fd is suspended, depending on the value of action:
TCOOFF: Pending output
TCOON: restart the suspended output.
TCIOFF: Send a STOP character to STOP the terminal device from transmitting data to the system.
TCION: Send a START character so that the terminal device can transmit data to the system.
When a terminal device is opened, the input and output are not suspended by default.
(7) baud rate Function
It is used to obtain and set the input and output baud rate values in the termios structure. The new value does not take effect immediately until the tcsetattr () function is successfully called.
Set the speed to B0 so that the modem is "mounted ". You can use setserial (8) to adjust the actual bit rate corresponding to B38400.
The input and output baud rates are stored in the termios structure.
Cfmakeraw sets the terminal attributes as follows:
Termios_p-> c_iflag & = ~ (IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON );
Termios_p-> c_oflag & = ~ OPOST;
Termios_p-> c_lflag & = ~ (ECHO | ECHONL | ICANON | ISIG | IEXTEN );
Termios_p-> c_cflag & = ~ (CSIZE | PARENB );
Termios_p-> c_cflag | = CS8;
1. cfgetospeed () returns the output baud rate stored in the termios structure pointed to by termios_p.
2. cfsetospeed () sets the output baud rate stored in the termios structure directed by termios_p to speed. The value must be one of the following constants:
B0 B50 B75 B110 B134 B150 B200 B300 B600 B1200 B1800 B2400 B4800 B9600 B19200 B38400 B57600 B115200 B230400
Where: The zero value B0 is used to interrupt the connection. If B0 is specified, a connection is not allowed. Normally, the connection will be disconnected. CBAUDEX is a mask that indicates the speed higher than POSIX.1 defined (57600 or above ). Therefore, B57600 & CBAUDEX is non-zero.
3. cfgetispeed () returns the input baud rate stored in the termios structure.
4. cfsetispeed () sets the input baud rate stored in the termios structure to speed. If the input baud rate is set to 0, the actual input baud rate is equal to the output baud rate.
RETURN VALUE
1. cfgetispeed () returns the input baud rate stored in the termios structure.
2. cfgetospeed () returns the output baud rate stored in the termios structure.
3. Other functions return:
(1) 0: Successful
(2)-1: failed,
Set a value for errno to indicate an error.
Note that tcsetattr () returns success if any required modifications can be implemented. Therefore, when performing multiple modifications, call tcgetattr () again after this function to check whether all modifications are successfully implemented.