终端规范模式,非规范模式,tcsetattr, tcgetattr

规范模式:以行为单位来上报数据。

非规范模式:终端收到数据后立刻上报,不用等回车

终端 规范模式/非规范模式 可以通过tcsetattr来设置。下面例子VMIN = 1,VTIME = 0 表示read会阻塞,直到收到vmin个字节的数据。

struct termios state;
tcgetattr(fd,&state);
tty.c_lflag &= ~(ECHO|ICANON);
tty.c_cc[VMIN]=1;              
tty.c_cc[VTIME]=0;
tcsetattr(fd,TCSAFLUSH,&state);

 

下面是linux man page 中关于终端规范模式及VTIME,VMIN的介绍。

Canonical and noncanonical mode

       The setting of the ICANON canon flag in c_lflag determines whether the
       terminal is operating in canonical mode (ICANON set) or noncanonical mode
       (ICANON unset).  By default, ICANON set.

       In canonical mode:

       * Input is made available line by line.  An input line is available when
         one of the line delimiters is typed (NL, EOL, EOL2; or EOF at the start
         of line).  Except in the case of EOF, the line delimiter is included in
         the buffer returned by read(2).

       * Line editing is enabled (ERASE, KILL; and if the IEXTEN flag is set:
         WERASE, REPRINT, LNEXT).  A read(2) returns at most one line of input; if
         the read(2) requested fewer bytes than are available in the current line
         of input, then only as many bytes as requested are read, and the
         remaining characters will be available for a future read(2).

       In noncanonical mode input is available immediately (without the user
       having to type a line-delimiter character), and line editing is disabled.
       The settings of MIN (c_cc[VMIN]) and TIME (c_cc[VTIME]) determine the
       circumstances in which a read(2) completes; there are four distinct cases:

       * MIN == 0; TIME == 0: If data is available, read(2) returns immediately,
         with the lesser of the number of bytes available, or the number of bytes
         requested.  If no data is available, read(2) returns 0.

       * MIN > 0; TIME == 0: read(2) blocks until the lesser of MIN bytes or the
         number of bytes requested are available, and returns the lesser of these
         two values.

       * MIN == 0; TIME > 0: TIME specifies the limit for a timer in tenths of a
         second.  The timer is started when read(2) is called.  read(2) returns
         either when at least one byte of data is available, or when the timer
         expires.  If the timer expires without any input becoming available,
         read(2) returns 0.

       * MIN > 0; TIME > 0: TIME specifies the limit for a timer in tenths of a
         second.  Once an initial byte of input becomes available, the timer is
         restarted after each further byte is received.  read(2) returns either
         when the lesser of the number of bytes requested or MIN byte have been
         read, or when the inter-byte timeout expires.  Because the timer is only
         started after the initial byte becomes available, at least one byte will
         be read.
posted @ 2013-02-25 13:00  datetree  阅读(1243)  评论(0编辑  收藏  举报