LINUX serial port-struct termios struct

Source: Internet
Author: User
Tags eol sigint signal vmin

Source: http://www.cnblogs.com/xiaotlili/archive/2011/08/06/2129510.html

I. Data Member

The termios function family provides a conventional Terminal interface for controlling non-synchronous 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 character */

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;/* region mode flag, Local Mode flag, or local mode */
Unsigned char c_line;/* line control line discipline */
Unsigned char c_cc [NCC];/* control character features */
};
Ii. Functions
This variable is used to provide a sound set of line settings if this port is used before being initialized by the user. the driver initializes this variable using a standard value set, which is copied from the tty_std_termios variable. tty_std_termos is defined:

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
}; The struct termios structure is used to hold all the current line settings and give the tty device a specific port. these line settings control the current baud rate, data size, data flow control settings, and many other values.

3. Member Value

(1) c_iflag flag constant:The input mode can control the processing method before the input value is passed to the program.

The input value may be a character received by the serial port or keyboard Terminal Driver. We can use the c_iflag flag of the termios structure to control it. The defined methods are combined by or.
Ignbrk: Ignore the break status in the input. (Ignore the interruption in the command line)
Brkint: (a break occurs when the command line is interrupted.) If ignbrk is set, break is ignored. If not set, but brkint is set, the break will refresh the input and output queues. If the terminal is a control terminal of the foreground process group, all processes in this process group receive the SIGINT signal. If neither ignbrk nor brkint is set, break is considered to be synonymous with NUL unless parmrk is set. In this case, it is considered as a sequence of 377 & #0; & #0 ;.
Ignpar: Ignore the accept error and parity error.
Parmrk: If no ignpar is set, insert 377 & #0; Before the characters with an error in parity or parity ;. If neither ignpar nor parmrk is set, the characters with parity or parity errors are considered as & #0 ;.
Inpck: enables input parity detection.
Istrip: removes the eighth digit.
Inlcr: translates the input NL into Cr. (Convert the received line feed to return)
Igncr: Ignore the carriage return in the input.
Icrnl: translate the carriage return in the input into a new line (unless igncr is set) (otherwise, the input will not be terminated when the input signal has cr ).
Iuclc: (not POSIX) maps uppercase letters in the input to lowercase letters.
Ixon: enables output Xon/xoff flow control.
Ixany: (not posix.1; xsi) allows any character to start output again. (?)
Ixoff: enables incoming Xon/xoff throttling.
Imaxbel: (not POSIX) when the input queue is full, it must be zero. Linux does not implement this one. It is always regarded as configured.

(2) c_oflag flag constant:Output Mode)

The output mode is mainly used to control how output characters are processed. How are output characters processed by a program before being transferred to a serial port or display.

The output mode is controlled by using the c_oflag flag of the termios structure. The methods defined by the output mode are combined by or.


Opost: enables custom output processing.
Olcuc: (not POSIX) ing lowercase letters in the output to uppercase letters.
Onlcr :( xsi) maps the new line character in the output to carriage return-line feed.
Ocrnl: maps the carriage return in the output to a newline character.
Onocr: The carriage return is not output in the 0th column.
Onlret: Do not output a carriage return.
Ofill: The sending padding character is used as the delay, rather than the time delay.
Ofdel: (not POSIX) the padding character is ASCII del (0177 ). If this parameter is not set, the padding character is ascii nul.
Nldly: the new line delay mask. Valid values: nl0 and nl1.
Crdly: Enter the delay mask. The value is Cr0, CR1, CR2, or C3.
Tabdly: horizontal hop delay mask. The optional values are tab0, tab1, tab2, and tab3 (or xtabs ). The value is tab3, that is, xtabs. It is expanded to a space (each grid character is filled with 8 spaces ). (?)
Bsdly: return the latency mask. The value is bs0 or BS1. (Never implemented)
Vtdly: vertical hop delay mask. The value is vt0 or vt1.
Ffdly: the latency mask of the incoming table. The value is ff0 or ff1.

(3) c_cflag flag constant: Control Mode)

Control mode is mainly used to control the hardware settings of terminal devices. The c_cflag flag of the termios structure is used for control. The control mode is used to connect a sequence line to a data device, or to communicate with a terminal device.

Generally, changing the configuration of a terminal device is easier than changing the behavior of lines using the control mode of termios.

Cbaud: (not POSIX) baud rate mask (4 + 1 bit ).
Cbaudex: (not POSIX) The extended baud rate mask (1 bit) is included in cbaud.
(POSIX specifies that the baud rate is stored in the termios structure and does not specify its location accurately. Instead, it provides the cfgetispeed () and cfsetispeed () functions to access it. Some systems use the bit selected by cbaud in c_cflag, and other systems use separate variables, such as sg_ispeed and sg_ospeed .)
Csize: Specifies the character length mask (the number of digits used to transmit or receive tokens ). The value is cs5 (5 bits is used for transmitting or receiving text elements), CS6, cs7, or cs8.
Cstopb: set two stop bits instead of one.
Cread: Open the recipient.
Parenb: allows the output to generate parity information and input parity (enable the same-bit generation and detection ).
Parodd: the input and output parameters are odd parity checks (the odd bit is used instead of the even bit ).
Hupcl: after the last process closes the device, the control line of the modem is lowered ). (?)
Clocal: ignores the modem control line.
Loblk: (not POSIX) blocks the output from the non-current shell layer (for SHL ). (?)
Cibaud: (not POSIX) the input speed mask. The values of cibaud are the same as those of cbaud, And the ibshift bit is shifted to the left.
Crtscts: (not POSIX) enables RTS/CTS (hardware) throttling.

(4) c_lflag flag constant:Local Mode)
Local Mode is mainly used to control different features of terminal devices. Use the c_lflag flag in the termios structure to set the local mode.
There are two important signs in the collection:
1. Echo: it allows you to block the response of the typed character.
2. icanon (regular mode) flag, which can switch the received characters back and forth between two different terminal device modes.
Isig: a signal is generated when intr, quit, susp, or dsusp characters are received.
Icanon: enables the standard mode ). Special characters such as EOF, EOL, eol2, erase, kill, lnext, reprint, status, and werase can be used as well as row-based buffering.
Xcase: (not POSIX; not supported in Linux) if both icanon is set, the terminal only has uppercase letters. The input is converted to lowercase letters, except for characters with a prefix. During output, uppercase characters are prefixed (specific characters specified by some systems) and lowercase characters are converted to uppercase letters.
Echo: Echo input character.
Echoe: If icanon is set at the same time, the character erase erased the previous input character, and werase erased the previous word.
Echok: if both icanon is set, kill deletes the current row.
Echonl: If icanon is set at the same time, Echo character NL is displayed, even if ECHO is not set.
Echoctl :( not POSIX) if ECHO is set at the same time, ASCII control signals except tab, NL, start, and stop are displayed as ^ X, here, X is an ascii code of 0x40 greater than the control signal. For example, the character 0x08 (BS) is displayed as ^ h.
Echoprt: (not POSIX) if both icanon and iecho are set, the characters are printed when they are deleted.
Echoke: (not POSIX) If icanon is set at the same time, each character in a row will be deleted when the ECHO is killed, as if echoe and echoprt are specified.
Defecho: (not POSIX) echo only when a process is read.
Flusho: (not POSIX; not supported in Linux) the output is refreshed. This flag can be switched by typing the discard character.
Noflsh: Do not refresh the input and output queues when generating SIGINT, sigquit, and sigsusp signals, that is, disable flush in the queue.
Tostop: Send the sigttou signal to the background process group of the write control terminal (send the information to the background for processing ).
Pendin: (not POSIX; not supported in Linux) when reading the next character, all characters in the input queue are output again. (Bash uses it to process typeahead)
Iexten: enables custom input processing. This flag must be used together with icanon to interpret the special characters eol2, lnext, reprint, werase, and iuclc.
(5) c_cc array:Special Control Character

You can set some special functions, such as Ctrl + C character combinations. Special control characters are mainly set using c_cc array members in the termios structure.
C_cc arrays are mainly used in both formal and informal environments, but note that formal and informal environments cannot be confused.
It defines special control characters. Symbol subscript (initial value) and meaning:

Vintr :( 003, etx, Ctrl-C, or also 0177, Del, rubout) interrupt characters. Sends a SIGINT signal. When isig is set, it can be recognized and no longer passed as input.
Vquit :( 034, FS, Ctrl-) Exit character. Sends a sigquit signal. When isig is set, it can be recognized and no longer passed as input.
Verase :( 0177, Del, rubout, or 010, BS, Ctrl-H, or also #) Delete characters. Delete the previous character that has not been deleted, but do not delete the previous EOF or the beginning of the line. When icanon is set, it can be recognized and no longer passed as input.
Vkill :( 025, Nak, Ctrl-u, or Ctrl-X, or also @) Termination character. Delete the input from the previous EOF or line header. When icanon is set, it can be recognized and no longer passed as input.
Veof :( 004, EOT, Ctrl-d) End character of the file. More precisely, this character causes the contents in the TTY buffer to be sent to the user program waiting for input, instead of waiting for the EOL. If it is the first character of a line, the user program's read () will return 0, indicating that the EOF is read. When icanon is set, it can be recognized and no longer passed as input.
Vmin: the minimum number of characters read in non-canonical mode (Min indicates the minimum number of characters that can satisfy the read ).
Veol :( 0, NUL) appended line tail character. It can be recognized when icanon is set.
Vtime: The read latency in non-canonical mode, in the unit of 10 seconds.
Veol2 :( not in POSIX; 0, NUL) another line tail character. It can be recognized when icanon is set.
Vswtch :( not in POSIX; not supported under Linux; 0, NUL) Switch character. (Only for SHL .)
Vstart :( 021, DC1, Ctrl-q) Start character. Restart the output that is 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. Send sigtstp signals. When isig is set, it can be recognized and no longer passed as input.
Vdsusp :( not in POSIX; not supported under Linux; 031, em, Ctrl-y) delay suspension signal. When the user program reads this character, it sends the sigtstp signal. When iexten and isig are set and the system supports job management, they can be identified and are no longer transmitted as input.
Vlnext :( not in POSIX; 026, Syn, Ctrl-v) literally next. Reference the next input character to cancel 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 a word. It can be identified when icanon and iexten are set and will not be passed as input.
Vreprint :( not in POSIX; 022, DC2, Ctrl-R) resend unread characters. It can be identified when icanon and iexten are set and will not be 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 values of these symbols are different. Except for vtime, the values of Vmin may be the same as those of veol and veof. (In non-canonical mode, the meaning of special characters is changed to the meaning of delay. Min indicates the minimum number of characters to be read. Time is a timer in the unit of 10 seconds. If they are set at the same time, read will wait until at least one character is read. Once the characters are read in min or the time elapsed since the last character is read, the system returns immediately. If only Min is set, read will not be returned before the min characters are read. If only time is set, read reads at least one character or returns immediately when the timer times out. If none of them are set, read
Will be returned immediately, only the characters currently prepared are given .)
There are four combinations of Min and time:

1. min = 0, time = 0
Instant return with read
Otherwise, 0 is returned and no characters are read.
2. min = 0, time> 0
Read: returns the character to be read, or returns time in 10 seconds.
If you cannot read any character, 0 is returned.
3. min> 0, time = 0
Read will wait until the min characters are readable
4. min> 0, time> 0
The timer starts between each character.
Read reads min characters, and returns the value when the time (1/10 seconds) of the returned value or time exceeds

4. functions related to this struct
(1) tcgetattr ()

1. Prototype
Int tcgetattr (int fd, struct termois & termios_p );
2. Functions
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 ()Set the output baud rate stored in the termios structure directed to 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 ()Set 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.

5. 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.

Vi. Notes
UNIX V7 and many later systems have a baud rate list, in 14 B0 ,..., after b9600, two constants exta and extb ("external a" and "external B") are displayed "). Many systems extend this list to a higher baud rate.
Non-zero duration in tcsendbreak has different effects. SunOS specifies the duration of interruption * n seconds, where n is at least 0.25, not higher than 0.5. Linux, Aix, Du, and Tru64 send duration microsecond break. FreeBSD, NetBSD, HP-UX, and MACOs ignore duration values. In Solaris and unixware, tcsendbreak works with a non-zero duration effect similar to tcdrain.
See also

Stty (1), setserial (8)

Remember don't at a loss and let the brain to calm down when comes questions, so can solve them faster!

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.