Linux ~ Termios Serial Programming

Source: Internet
Author: User

The ERMIOS structure is a standard interface defined in the POSIX specification, similar to the Termio interface in System V, by setting values in the data structure of the Termios type and using a small

Group function calls, you can control the terminal interface.

The values that can be adjusted to affect the terminal are divided into the following groups according to different patterns:

1. Input mode

2. Output mode

3. Control mode

4. Local mode

5. Special control mode

The typical definition of the smallest Termios structure is as follows:

struct termios{    tcflag_t c_iflag;    tcflag_t C_oflag;    tcflag_t C_cflag;    tcflag_t C_lflag;    cc_t     C_cc[nccs];};

The name of the struct member corresponds to the 5 types of parameter listed above.

You can call the function tcgetattr to initialize the TERMIOS structure of a terminal, which is prototyped as follows:

#include <termios.h>  int tcgetattr (intstruct

This function call writes the value of the current terminal interface variable to the structure that the termios_p parameter points to. If these values are later modified, you can reconfigure the terminal interface by calling the function tcsetattr.

#include <termios.h>  int tcsetattr (int fd, int actions, const struct Termios *termios_h);  

There are three ways to modify the parameters of the actions control, as shown below.

1.TCSANOW: Change the value immediately

2.TCSADRAIN: After the current output is complete, modify the value.

3.TCSAFLUSH: After the current output is complete, modify the value, but discard any currently available input that is not returned from the read call.

Next we will introduce the five modes separately.

One input mode

The input mode controls how the input data is processed before it is passed to the program. You control them by setting the flags for the C_iflag members in the Termios structure. All the flags are defined as

macros, and can be combined in a bitwise OR way.

The macros that are available for C_iflag members are as follows:

Brkint: An interrupt is generated when a terminating state is detected in the input line.

TGNBRK: Ignores the terminating state in the input row.

TCRNL: Converts the accepted carriage return character to a new line character.

TGNCR: Ignores new line characters that are accepted.

INLCR: Converts the new line character that is accepted to a carriage return character.

Ignpar: Ignores the character of the parity-check error.

INPCK: Performs a parity check on the received characters.

PARMRK: Marks the parity test error.

Istrip: Cuts all received characters to 7 bits.

Ixoff: Enable software flow control on the input.

Ixon: Enable software flow control on the output.

If both the Brkint and TGNBRK flags are not set, the terminating state in the input line is read as a null (0x00) character.

Three. Output mode

The output mode controls how the output characters are handled, that is, the characters emitted by the program are processed before being passed to the serial port or screen. The output mode is controlled by setting the identity of the C_oflag member.

Opsot: Turn on the output processing function

ONLCR: Converts a newline character in the output to a carriage return

OCRNL: Convert carriage return to line feed

ONOCR: line No. 0 does not output carriage return character

Onlret: does not output a carriage return character

nldly: Select when line boundaries is changed

crdly: Carriage Break delay

tabdly: Tab delay

...

Output mode is not used much.

Four. Control mode

The control mode controls the hardware characteristics of the terminal through the C_cflag member identification configuration.

Clocal: Ignores status lines for all modems

Cread: Enable character receiver

CS5/6/7/8: Use 5/6/7/8 bit when sending or receiving characters

CSTOPB: Use two stop bits per character

HUPCL: Hangs up modem when off

Parenb: Enable generation and detection of parity codes

Parodd: Use only odd tests without even checking

In general, this is not the way to modify the hardware features by modifying the terminal configuration file directly.

Five. Local mode

Certain features of the terminal are controlled by C_lflag members

ECHO: Enable local echo of input characters

Echonl: Echo Line break

Icanon: Enable standard input processing

Isig: Enable signal

...

The most common is the echo and Icanon flags, which suppress the echo of typed characters (suppress??), the latter as described

Six. Special control characters

The subscript for the C_CC array has different values in both standard and non-standard modes:

Standard mode:

veof:eof characters

Veol:eol characters

Verase:erase characters

Vintr:intr characters

Vkill:kill characters

Vquit:quit characters

Vstart:start characters

Vstop:stop characters

Non-standard mode:

Vintr:intr characters

Vmin:min value

Vquit:quit characters

Vsusp:susp characters

Vtime:time value

Vstart:start characters

Vstop:stop characters

1. Characters

INTR: This character causes the terminal driver to send a SIGINT signal to a process connected to the terminal

QUIT: This character causes the terminal driver to send a sigquit signal to the process connected to the terminal

This character causes the terminal driver to pass all the characters in the input line to the application that is reading the input. If the input behavior is empty, the read call will return 0, as if you were calling read at the end of the file

...

2.TIME and Min values

These values are used only in non-standard modes, together controlling the reading of the input and controlling what happens when a program tries to associate a file descriptor with a terminal

MIN = 0, time = 0 O'Clock: Read returns immediately if the characters to be processed are returned, and if not, the read call returns 0 and does not read any characters

MIN = 0, time > 0 o'clock: Characters processed or returned after a time of 0.1 seconds

Min > 0, time = 0 o'clock: Read waits until there is a min character to read, and the return value is the number of characters. Returns 0 when the end of the file is reached

MIN > 0, Time > 0 o'clock: When read is called, it waits for a character to be received. When you receive the first character and its subsequent characters, a character interval timer is enabled. Read returns when there is a min character readable or a time interval between two characters that is over 0.1 seconds

By setting the Min and time values, we can process the input individually by character

3. Access Terminal mode through the shell

Stty-a: This command is used to view the settings of the current terminal

Stty sane: If you accidentally set the wrong terminal mode, you can use this command to recover, another way to recover is to save the current Stty settings before setting, and then read the

Stty-g > Save_stty: Save current settings to file Save_atty

Stty $ (cat save_stty): read out the Save_atty file and restore the original terminal settings

The third way to recover is to re-lay down a terminal emulator. View the dead terminal process and kill it.

4. Setting terminal mode in command line mode

For example, to have a shell script read a single character, you need to turn off standard mode and set min to 1,time to 0:

Stty-icanon min1 Time 0

Another example is the ability to turn off the echo when entering a password:

Atty-echo

After you use this command, perform Atty echo, restoring the echo function again

5. Terminal speed

There is no member or identifier for the terminal speed in the Termios structure, but we can do it through a set of functions. Note that the input and output are separate and should use different functions

#include <termios.h>speed_t cfgetispeed (conststruct termios *); speed_t Cfgetospeed (conststruct termios *); int cfsetispeed (struct termios *, speed_t speed); int cfseospeed (struct Termios *, speed_t speed);

These functions only work on the TERMIOS structure, so you need to call tcgetattr () to get the Termios structure, then call one of the above functions to set the terminal speed, and finally call Tcsetattr () to make the setting take effect

The above speed parameter can be set to a value that is more important than the following:

B0: Suspend terminal

b1200:1200 Porter

b2400:2400 Porter

b9600:9600 Porter

b19200:19200 Porter

b38400:38400 Porter

6. Other functions

These functions work directly on file descriptors and do not need to read and write Termios structures:

#include <termios.h>

int tcdrain (int fd); Let the caller wait until all queued output is sent

int tcflow (int, int flowtype); Pause or restart output

int Tcflush (int fd, int in_out_selector), empty input, output, or both

Linux ~ Termios Serial Programming

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.