struct Termios structural body "turn"

Source: Internet
Author: User
Tags control characters eol flushes posix sigint signal vmin

This article was reproduced from: http://blog.csdn.net/vevenlcf/article/details/51096122

First, data members

The Termios function Family provides a general terminal interface for controlling asynchronous communication ports. This structure contains at least the following members:
tcflag_t C_iflag; /* Input mode */
tcflag_t C_oflag; /* Output Mode */
tcflag_t C_cflag; /* Control mode */
tcflag_t C_lflag; /* Local mode */
cc_t C_cc[nccs]; /* Control characters */

struct Termios
{unsigned short c_iflag;/* Input mode flag */
unsigned short c_oflag; /* Output Mode flag */
unsigned short c_cflag; /* Control Mode flag */
unsigned short c_lflag; /* Zone mode flag or local mode flag or local mode */
unsigned char c_line; /* Row Control line Discipline */
unsigned char C_CC[NCC]; /* Control Character Properties */
};
Second, the role
This variable is used to provide a robust set of line settings if the port is used before being initialized by the user. Driver initialization This variable uses a standard set of values that is copied from the Tty_std_termios variable. The Tty_std_termos is defined in the TTY core as:

struct Termios Tty_std_termios = {
. C_iflag = ICRNL | Ixon,
. C_oflag = Opost | ONLCR,
. C_cflag = B38400 | CS8 | Cread | HUPCL,
. C_lflag = Isig | Icanon | ECHO | Echoe | Echok |
Echoctl | Echoke | Iexten,
. C_CC = INIT_C_CC
This struct Termios structure is used to hold all current line settings to a specific port of the TTY device. These line settings control the current baud rate, data size, data flow control settings, and many other values. Third, the value of the member (a) C_iflag flag constant: Input mode input modes can control how the input value is processed before it is passed to the program.   The input value may be a character that is received by the terminal driver for the serial port or keyboard. We can use the c_iflag of the Termios structure to control it, and the way it is defined is combined with or.
IGNBRK: Ignores the break state in the input. (Ignore interrupts in the command line)
Brkint: (can produce a plug when the command line is interrupted) if IGNBRK is set, break is ignored. If there is no setting, but Brkint is set, then break will cause the input and output queues to be flushed, and if the terminal is a control terminal of a foreground process group, all processes in the process group will receive the SIGINT signal. If neither set IGNBRK nor set Brkint,break will be considered with the NUL word Fu Tongyi, unless PARMRK is set, in which case it is treated as sequence 377??.
Ignpar: Ignores frame errors and parity faults.
PARMRK: If Ignpar is not set, insert 377 in the word match either with parity or frame error. If neither set Ignpar nor set PARMRK, will there be a parity wrong or frame error character as?.
INPCK: Enable input parity detection.
Istrip: Remove the eighth bit.
INLCR: Translates NL in input to CR. (Converts the received newline symbol to return)
IGNCR: Ignores the carriage return in the input.
ICRNL: Translates the carriage return in the input to a new line (unless IGNCR is set) (otherwise the input is not terminated when the input signal has a CR).
IUCLC:(not POSIX) maps uppercase letters in the input to lowercase letters.
Ixon: Enables Xon/xoff flow control of the output.
Ixany:(does not belong to Posix.1;xsi) allows any character to start the output again. (?)
Ixoff: Enables the input Xon/xoff flow control.
Imaxbel: (not POSIX) when the input queue is full, it rings zero. Linux does not implement this bit and always treats it as set.
(ii) C_oflag flag constant: Output mode

Output mode is primarily responsible for controlling the processing of input characters. How the output character is handled by the program before it is routed to the serial port or display.

The output pattern is controlled by the c_oflag of the Termios structure, which is defined in the form of or to be combined.


Opost: Enables the specific implementation of self-defined output processing.
OLCUC: (not POSIX) maps lowercase letters in the output to uppercase.
ONLCR: (XSI) maps the new line character in the output to carriage return-line feed.
OCRNL: Maps a carriage return in the output to a new line character
ONOCR: Do not output carriage return in column No. 0.
Onlret: Do not output carriage return.
Ofill: Sends the fill character as a delay instead of using timing to delay.
Ofdel: (not POSIX) The padding character is ASCII DEL (0177). If not set, the padding character is ASCII NUL.
nldly: New line delay mask. The values are NL0 and NL1.
crdly: Enter the delay mask. Values are CR0, CR1, CR2, or CR3.
tabdly: Horizontal Jump delay mask. The values are TAB0, TAB1, TAB2, TAB3 (or Xtabs). The value is TAB3, which is xtabs, which expands the jump to a space (8 spaces per tick). (?)
bsdly: Fallback delay mask. The value is BS0 or BS1. (has never been implemented)
vtdly: Vertical jump-off delay mask. The value is VT0 or VT1.
ffdly: The delay mask for the input table. The value is FF0 or FF1.
(iii) C_CFLAG flag constant: Control mode

Control mode is primarily used for controlling the hardware settings of terminal devices. The c_cflag of the Termios structure is used to control the signal. The control mode is used to connect the serial line to the data device, or it can be used to talk to the end device.

In general, it is easier to change the configuration of a terminal device than to use the Termios control mode to change the behavior of the line (lines).


Cbaud: (not POSIX) baud rate mask (4+1 bit).
Cbaudex: (Non-POSIX) extended baud rate mask (1-bit), included in Cbaud.
(POSIX specifies that the baud rate is stored in the Termios structure and does not specify its location precisely, but instead provides the function cfgetispeed () and Cfsetispeed () to access it. Some systems use Cbaud selected bits in C_cflag, and other systems use separate variables, such as Sg_ispeed and Sg_ospeed. )
CSIZE: The character length mask (the number of bits used to transmit or receive characters). The value is CS5 (5bits for transmitting or receiving characters), CS6, CS7, or CS8.
CSTOPB: Set two stop bits instead of one.
Cread: Open the recipient.
Parenb: Allows output to generate parity information and input parity (enable co-generation and detection).
Parodd: The input and output are odd checks (using odd-bit rather than even parity).
HUPCL: After the last process shuts down the device, lower the modem control line (hang up). (?)
Clocal: Ignore modem control line.
Loblk:(not POSIX) blocks output from the non-current shell layer (for SHL). (?)
Cibaud:(a mask that does not belong to POSIX) input speed. Cibaud the values are the same as the Cbaud, and the Ibshift bit is shifted to the left.
Crtscts:(does not belong to POSIX) enables rts/cts (hardware) flow control.

(iv) C_lflag flag constant: Local mode
Local mode is mainly used to control the different features of terminal equipment. Use the C_lflag in the Termios structure to set the local pattern.
There are two more important signs in the mega-concentration:
1.ECHO: It allows you to block the response of typed characters.
2.ICANON (normal mode) flag, which can be switched back and forth between two different terminal device modes for the received characters.
Isig: When receiving a character INTR, QUIT, SUSP, or DSUSP, the corresponding signal is generated.
Icanon: Enable standard mode (canonical modes). Allows the use of special characters EOF, EOL, EOL2, ERASE, KILL, Lnext, reprint, STATUS, and Werase, as well as buffering by line.
Xcase: (Does not belong to POSIX; Linux is not supported) if Icanon is set at the same time, the terminal is only capitalized. The input is converted to lowercase except for prefixed characters. When output, uppercase characters are prefixed (certain system specific characters are specified), and lowercase characters are converted to uppercase.
Echo: echoes the input characters.
Echoe: If Icanon is set at the same time, the character ERASE erases the previous input character, werase erases the previous word.
Echok: If Icanon is set at the same time, the character KILL deletes the current line.
ECHONL: If the Icanon is set at the same time, the echo character NL is not set even if you do not.
Echoctl: (not POSIX) if ECHO is set at the same time, except TAB, NL, START, and STOP ASCII control signals are echoed back to ^x, where X is the ASCII code larger 0x40 than the control signal. For example, the character 0x08 (BS) is echoed as ^h.
ECHOPRT: (not POSIX) if both Icanon and Iecho are set, the characters are printed at the same time as the deletion.
Echoke: (not POSIX) if you set Icanon at the same time, each character in a row is deleted when you echo Kill, as you specified Echoe and echoprt.
Defecho:(not POSIX) is echoed only when a process is read.
Flusho:(does not belong to POSIX; Not supported under Linux) output is refreshed. This flag can be switched by typing the character DISCARD.
Noflsh: Disables flush in the queue when generating SIGINT, sigquit, and SIGSUSP signals when the input and output queues are flushed.
Tostop: Sends a sigttou signal to the background process group that is trying to write the control terminal (transmits the information to be written to the background processing).
Pendin:(does not belong to POSIX; Not supported under Linux) when the next character is read, all characters in the input queue are re-exported. (bash uses it to handle typeahead)
Iexten: Enable input processing for custom implementation. This flag must be used in conjunction with Icanon in order to interpret the special characters Eol2,lnext,reprint and WERASE,IUCLC flags to be valid.
(v) C_CC array: Special control characters

can provide the user to set some special functions, such as CTRL + C character combination. The special control character is mainly used to set the C_CC array members in the Termios structure.
C_CC arrays are mainly used in both formal and informal environments, but it is important to note that formal and informal can not be confused.
It defines a special control character. Symbol subscript (initial value) and meaning:

VINTR: (003, ETX, Ctrl-c, or also 0177, DEL, rubout) break character. Emits a SIGINT signal. It can be recognized when ISIG is set and is no longer passed as input.
Vquit: (034, FS, ctrl) exit character. Emits a sigquit signal. It can be recognized when ISIG is set and is no longer passed as input.
Verase: (0177, DEL, rubout, or 010, BS, Ctrl-h, or also #) delete the characters. Deletes a character that has not been deleted, but does not delete the previous EOF or the beginning of the line. It can be recognized when Icanon is set and is no longer passed as input.
Vkill: (025, NAK, Ctrl-u, or ctrl-x, or also @) terminating character. Deletes the input since the last EOF or beginning of the line. It can be recognized when Icanon is set and is no longer passed as input.
Veof: (004, EOT, ctrl-d) file end character. More precisely, this character allows the content in the TTY buffer to be sent to the user program waiting to be entered without having to wait for EOL. If it is the first character of a line, then read () of the user program returns 0, indicating that EOF is read. It can be recognized when Icanon is set and is no longer passed as input.
VMIN: The minimum number of characters to read in non-canonical mode (the min is mostly the minimum number of bytes that can satisfy read).
Veol:(0, NUL) appends the end-of-line character. can be recognized when Icanon is set.
Vtime: Delay in non-canonical mode reading, in one-tenth seconds.
VEOL2: (not in POSIX; 0, NUL) Another end-of-line character. can be recognized when Icanon is set.
Vswtch: (not in POSIX; not supported under Linux; 0, NUL) switch characters. (Used only by SHL.) )
Vstart: (021, DC1, Ctrl-q) start character. Restarts the output that was aborted by the stop character. It can be recognized when Ixon is set and is no longer passed as input.
Vstop: (023, DC3, ctrl-s) stop character. Stop the output until you type the Start character. It can be recognized when Ixon is set and is no longer passed as input.
VSUSP: (032, SUB, ctrl-z) hangs the character. Sends a SIGTSTP signal. It can be recognized when ISIG is set and is no longer passed as input.
VDSUSP: (not in POSIX; not supported under Linux; 031, EM, CTRL-Y) delay suspend signal. When the user program reads this character, it sends a SIGTSTP signal. When Iexten and Isig are set up, and the system supports job management, it can be identified and no longer passed as input.
Vlnext: (not in POSIX; 026, SYN, ctrl-v) literally the next. Refers to the next input character and cancels any special meaning of it. It can be recognized when Iexten is set and is no longer passed as input.
Vwerase: (not in POSIX; 027, ETB, ctrl-w) Delete words. When Icanon and Iexten are set, they are recognized and are no longer passed as input.
Vreprint: (not in POSIX; 022, DC2, Ctrl-r) re-outputs unread characters. When Icanon and Iexten are set, they are recognized and are no longer passed as input.
Vdiscard: (not in POSIX; not supported under Linux; 017, SI, ctrl-o) switch: Start/end discard unfinished output. It can be recognized when Iexten is set and is no longer passed as input.
Vstatus: (not in POSIX; not supported under Linux; status request:024, DC4, ctrl-t).
The subscript values for these symbols are different, except that the vtime,vmin values may be the same as veol,veof. (in non-canonical mode, the meaning of the special character changes to the delay meaning.) Min Indicates the minimum number of characters that should be read in. Time is a timer in one-tenth seconds. If they are set at the same time, read waits until at least one character is read, and returns as soon as the MIN character is read or when the last read-in character begins. If only Min,read is set, it will not be returned until MIN characters are read. If only Time,read is set, it will be returned immediately at least one character is read, or when the timer expires. If none is set, read will return immediately, giving only the currently prepared characters. )
The min and time combinations have the following four types:

1, MIN = 0, time =0
              have read immediate callback
              returns 0 without reading any characters
2, MIN = 0, Time >0
              read returns the characters Or, in a very second, return time
       if it is too late to read any characters, return 0
3, MIN > 0, Time =0
               read waits until min character is readable
4, min > 0, time > 0
              The timer is started between each of the characters
               read at min character, return value or Time's character timing (1/10 seconds) Pass values back to
Four, functions related to this struct
(i) tcgetattr ()
1. prototype
int tcgetattr (int fd,struct Termois & termios_p);
2. function  
obtains the initial value of the terminal media (FD) and assigns its value to temios_p; The function can be called from a background process, but the terminal properties may be changed by the subsequent foreground process.


(ii) tcsetattr ()
1. Prototypes
int tcsetattr (int fd,int actions,const struct termios *termios_p);
2. function
Set the terminal-related parameters (unless the underlying support is not sufficient), using the Termios structure referenced by termios_p. Optional_actions (the second parameter of the TCSETATTR function) specifies when the change will work:
Tcsanow: Change occurs immediately
Tcsadrain: Changes take effect after all outputs written to FD are transmitted. This function should be used when modifying parameters that affect the output. (Change the value when the current output is complete)
Tcsaflush: Changing the output of all objects written to the FD reference takes effect and all accepted but unread inputs are discarded before the change occurs (same as Tcsadrain, but all current values are discarded).
(c) Tcsendbreak ()
Transmits a continuous 0-value bitstream for a period of time if the terminal uses asynchronous serial data transfer. If the duration is 0, it transmits at least 0.25 seconds and will not exceed 0.5 seconds. If duration is nonzero, the length of time it is sent is defined by the implementation.
If the terminal is not using asynchronous serial data transfer, Tcsendbreak () does nothing.
(d) Tcdrain ()
Waits until all the output of the object that is written to the FD reference is transmitted.
(v) Tcflush ()
Discards data to be written to the referenced object, but not yet transmitted, or received but not yet read, depending on the value of the Queue_selector:

Tciflush: Refreshes the received data but does not read
Tcoflush: Flushes the written data but does not transmit
Tcioflush: Flushes the received data at the same time but does not read, and flushes the written data but does not transmit
(vi) Tcflow ()
Suspends data transfer or reception on the object referenced by FD, depending on the value of the action:

Tcooff: Suspend output   
Tcoon: Restart the pending output   
Tcioff: Sends a stop character to stop the terminal device from transmitting data to the system   
Tcion: Send a START character, so that the terminal device transmits data to the system   
The default setting when opening an end device is that neither the input nor the output hangs.


(vii) Baud rate function
is used to get and set the value of the input and output baud rate 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 "hang the machine." The actual bit rate corresponding to the B38400 can be adjusted with setserial (8).
The input and output baud rates are stored in the Termios structure.
Cfmakeraw set the terminal properties 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 the termios_p
2.cfsetospeed () sets the output baud rate stored in the Termios structure that termios_p points 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: 0 value B0 is used to interrupt the connection. If you specify B0, you should not assume that there is a connection. Typically, this will disconnect the connection. Cbaudex is a mask that indicates the speed above the POSIX.1 definition (57600 and above). Therefore, B57600 & Cbaudex are 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 will be equal to the output baud rate.
V 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: Success
(2)-1: Failure,
and indicates an error for the errno value.
Note that tcsetattr () returns successfully if any of the required modifications can be implemented. Therefore, when making multiple modifications, you should call Tcgetattr () again after this function to detect if all modifications have been successfully implemented.
Vi. Notes Notes
Unix V7 and many later systems have a list of baud rates, in 14 values B0, ..., B9600 you can see two constants exta, EXTB ("External A" and "External B"). Many systems extend this list to a higher baud rate.
Tcsendbreak non-zero duration have different effects. SunOS Specifies the interrupt duration*n seconds, where N is at least 0.25, not higher than 0.5. Linux, AIX, DU, Tru64 send duration microseconds of break. FreeBSD, NetBSD, HP-UX, and MacOS ignore duration values. In Solaris and UnixWare, Tcsendbreak with a nonzero duration effect is similar to Tcdrain.
See ALSO

Stty (1), setserial (8)

struct Termios structural body "turn"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.