c++实现串口功能之termios.h头文件研读<一>

含义

The termios functions describe a general terminal interface that is provided to 
control asynchronous communications ports.

该头文件下定义的这些接口是用来控制端口异步通信

termios结构体

struct termios {
  tcflag_t c_iflag;    //input modes 输入模式标志
  tcflag_t c_oflag;    //output modes 输出模式标志
  tcflag_t c_cflag;    //control modes 控制模式标志
  tcflag_t c_lflag;    //local modes 本地模式标志
  cc_t c_line;         
  cc_t c_cc[NCCS];     //special characters 特殊字符
};

一、 c_iflag flag constants(输入模式标志常量):

IGNBRK

Ignore BREAK condition on input
在输入的时候,忽略BREAK

BRKINT

If IGNBRK is set, a BREAK is ignored.
如果设置了IGNBRK,则BREAK 会被忽略,不起作用。

If it is not set but BRKINT is set, then a BREAK causes the input and output queues to be flushed, and if the terminal is the controlling terminal of a foreground process group, it  will cause a SIGINT to be sent to this foreground process group. 
如果没有设置IGNBRK,但是设置了BREAK,BREAK会导致输入和输出队列刷新,如果此时终端正在控制着前台的进程组,则会发送一个SIGNT给当前前台的进程组。

When neither IGNBRK nor BRKINT are set, a BREAK reads as a null byte ('\0'), except when PARMRK is set, in which case it reads as the sequence \377 \0 \0.
如果既没有设置IGNBRK,也没有设置BREAK,BREAK将被读成空字符串,但是如果设置了PARMRK,它将会被读成\377 \0 \0。

IGNPAR

Ignore framing errors and parity errors.
忽略帧错误和奇偶校验错误

PARMRK

If this bit is set, input bytes with parity or framing errors are marked when passed to the program. 
如果设置了该位,在传给程序的时候,带有奇偶校验错误或者帧错误的输入值会被标记出来。

This bit is meaningful only when INPCK is set and IGNPAR is not set.
只有当INPCK设置了,但是IGNPAR没设置的时候,该标识位才会有作用。

The way erroneous bytes are marked is with two preceding bytes, \377 and \0.  Thus, the program actually reads three bytes for one erroneous byte received from the terminal.  
被标记为错误的字节会增加\377和\0两个前缀,因此程序从终端读取错误字节的时候要读取三个字节。

If a valid byte has the value \377, and ISTRIP (see below) is not set, the program might confuse it with the prefix that marks a parity error.  Therefore, a valid byte \377 is passed to the program as two bytes, \377\377, in this case.
如果一个合法的字节的值包含\377,同时没有设置ISTRIP,这个时候程序就会和有前缀的检验位错误表示混淆,因此,为了防止这样的错误,一个合法的带有\377的字节在传给程序的时候会传两个\377字节过去。

If neither IGNPAR nor PARMRK is set, read a character with a parity error or framing error as \0.
如果既没设置IGNPAR ,也没设置PARMRK ,当读到带有奇偶校验位错误或者帧错误的字节的时候,就会当做\0。

INPCK

Enable input parity checking.

开启输入奇偶校验位检查

ISTRIP

Strip off eighth bit

去除字符的第8个比特

INLCR

Translate NL to CR on input

输入的时候将输入的NL(换行)转换成CR(回车)

IGNCR

Ignore carriage return on input.

输入时忽略回车

ICRNL

Translate carriage return to newline on input (unless IGNCR is set).

在输入时将回车转换为换行符(除非IGNCR 已设置)

IUCLC

 (not in POSIX) Map uppercase characters to lowercase on input.

(不在 POSIX 中)在输入时将大写字符映射为小写

IXON

Enable XON/XOFF flow control on output.

在输出时可以对XON/XOFF流进行控制

IXOFF

Enable XON/XOFF flow control on input.

在输入时可以对XON/XOFF流进行控制

IXANY

(XSI) Typing any character will restart stopped output.
 (The default is to allow just the START character to restart output.)
 键入任何字符将重新启动停止的输出。(默认是只允许 START 字符重启输出)

IMAXBEL

(not in POSIX) Ring bell when input queue is full.  Linux does not implement
 this bit, and acts as if it is always set.
 (不在 POSIX 中)输入队列已满时响铃。 Linux 没有实现这个位,并且就好像它总是被设置一样。

IUTF8

(since Linux 2.6.4)(not in POSIX) Input is UTF8; this allows character-erase 
to be correctly performed in cooked mode.
(自 Linux 2.6.4 起)(不在 POSIX 中)输入为 UTF8; 这允许在cooked mode下正确执行字符擦除。

二、c_oflag flag constants(输出模式标识常量):

OPOST

Enable implementation-defined output processing.
开启实现后输出

OLCUC

(not in POSIX) Map lowercase characters to uppercase on output.
(不在 POSIX 中)在输出时将小写字符映射为大写。

ONLCR

(XSI) Map NL to CR-NL on output
(XSI) 在输出时将 NL 映射到 CR-NL

OCRNL

Map CR to NL on output.
在输出时将 CR 映射为NL。

ONOCR

Don't output CR at column 0.
不要在第 0 列输出 CR。

ONLRET

Don't output CR.
不输出 CR。

OFILL

Send fill characters for a delay, rather than using a timed delay.
发送填充字符以延迟终端输出而不是使用时间延迟

OFDEL

Fill character is ASCII DEL (0177).  If unset, fill character 
is ASCII NUL ('\0').  
(Not implemented onLinux.)
填充字符为 ASCII DEL (0177)。 如果未设置,填充字符为 ASCII NUL ('\0')。 (未在 Linux 上实现。)

NLDLY

Newline delay mask.  Values are NL0 and NL1.  [requires _BSD_SOURCE 
or _SVID_SOURCE or _XOPEN_SOURCE]
换行延迟。值为 NL0 和 NL1。 [需要 _BSD_SOURCE 或 _SVID_SOURCE 或 _XOPEN_SOURCE]

CRDLY

Carriage return delay mask.  Values are CR0, CR1, CR2, or CR3. 
 [requires _BSD_SOURCE or _SVID_SOURCE or _XOPEN_SOURCE]
 回车延迟。 值为 CR0、CR1、CR2 或 CR3。 [需要 _BSD_SOURCE 
 或 _SVID_SOURCE 或 _XOPEN_SOURCE]

TABDLY

Horizontal tab delay mask.  Values are TAB0, TAB1, TAB2,TAB3 (
or XTABS, but see the BUGS section).  A value of TAB3, that is, XTABS, 
expands tabs to spaces (with tab stops every eight columns). 
 [requires _BSD_SOURCE or _SVID_SOURCE or _XOPEN_SOURCE]

水平制表符延迟。 值为 TAB0、TAB1、TAB2、TAB3(或 XTABS,但请参阅 BUGS 部分)。 TAB3 的值,即 XTABS,将制表符扩展为空格(每八列制表符停止)。 [需要 _BSD_SOURCE 或 _SVID_SOURCE 或 _XOPEN_SOURCE]

BSDLY

Backspace delay mask.  Values are BS0 or BS1.  (Has never been implemented.)  
[requires _BSD_SOURCE or _SVID_SOURCE or_XOPEN_SOURCE]

退格延迟。 值为 BS0 或 BS1。 (从未实现过。)[需要 _BSD_SOURCE 或 _SVID_SOURCE 或_XOPEN_SOURCE]

VTDLY

Vertical tab delay mask.  Values are VT0 or VT1.

垂直制表延迟。 值为 VT0 或 VT1。

FFDLY

Form feed delay mask.  Values are FF0 or FF1. 
 [requires _BSD_SOURCE or _SVID_SOURCE or _XOPEN_SOURCE]

换页延迟。 值为 FF0 或 FF1。 [需要 _BSD_SOURCE 或 _SVID_SOURCE 或 _XOPEN_SOURCE]

三、c_cflag flag constants(控制模式标志常量):

CBAUD

(not in POSIX) Baud speed mask (4+1 bits).  [requires _BSD_SOURCE or _SVID_SOURCE]

(不在 POSIX 中)波特率掩码(4+1 位)。 [需要 _BSD_SOURCE 或 _SVID_SOURCE]

CBAUDEX

(not in POSIX) Extra baud speed mask (1 bit), included inCBAUD. [requires _BSD_SOURCE or _SVID_SOURCE]
(不在 POSIX 中)额外的波特率掩码(1 位),包含在 CBAUD 中。 [需要 _BSD_SOURCE 或 _SVID_SOURCE]

 (POSIX says that the baud speed is stored in the termios structure without specifying where precisely, and provides cfgetispeed() and cfsetispeed() for getting at it.  Some systems use bits selected by CBAUD in c_cflag, other systems use separate fields, for example, sg_ispeed andsg_ospeed.)
 POSIX规定波特率存储在termios中结构没有指定精确的位置,并提供cfgetispeed() 和 cfsetispeed() 来获得它。 一些系统使用 CBAUD 在 c_cflag 中选择的位,其他系统使用单独的字段,例如 sg_ispeed 和sg_ospeed

CSIZE

Character size mask.  Values are CS5, CS6, CS7, or CS8.
字符大小。 值为 CS5、CS6、CS7 或 CS8。

CSTOPB

Set two stop bits, rather than one.
设置两个停止位,而不是一个。

CREAD

Enable receiver.
启用接收器

PARENB

Enable parity generation on output and parity checking for input.
启用在输出时奇偶校验生成和在输入时奇偶校验。

PARODD

If set, then parity for input and output is odd; otherwise even parity is used.
如果设置,则输入和输出的奇偶校验使用奇数; 否则使用偶校验。

HUPCL

Lower modem control lines after last process closes the device (hang up).
在最后一个进程关闭设备后降低调制解调器控制线(挂断)。

CLOCAL

Ignore modem control lines.
忽略调制解调器控制线。

LOBLK

(not in POSIX) Block output from a noncurrent shell layer. For use by shl (shell layers).  (Not implemented on  Linux.)
(不在 POSIX 中)阻止来自非当前 shell 层的输出。 供 shl(外壳层)使用。 (未在 Linux 上实现。)

CIBAUD

(not in POSIX) Mask for input speeds.  The values for the CIBAUD bits are the same as the values for the CBAUD bits, shifted left IBSHIFT bits.  [requires _BSD_SOURCE or  _SVID_SOURCE] (Not implemented on Linux.)
(不在 POSIX 中)输入的速度。 CIBAUD 位的值与 CBAUD 位的值相同,左移 IBSHIFT 位。 [需要 _BSD_SOURCE 或 _SVID_SOURCE](未在 Linux 上实现。)

CMSPAR

(not in POSIX) Use "stick" (mark/space) parity (supported on certain serial devices): 
if PARODD is set, the parity bit is always 1; if PARODD is not set, then the parity bit is always 0. 
 [requires _BSD_SOURCE or _SVID_SOURCE]
(不在 POSIX 中)使用“stick”(标记/空格)奇偶校验(在某些串行设备上支持):如果设置了 PARODD,则奇偶校验位始终为 1; 如果未设置 PARODD,则奇偶校验位始终为 0。 [需要 _BSD_SOURCE 或 _SVID_SOURCE]

CRTSCTS

 (not in POSIX) Enable RTS/CTS (hardware) flow control. [requires _BSD_SOURCE or _SVID_SOURCE]
 (不在 POSIX 中)启用 RTS/CTS(硬件)流控制。 [需要 _BSD_SOURCE 或 _SVID_SOURCE]

四、c_lflag flag constants(本地模式标志常量):

ISIG

When any of the characters INTR, QUIT, SUSP, or DSUSP are received, generate the corresponding signal.
当接收到任何字符 INTR、QUIT、SUSP 或 DSUSP 时,生成相应的信号。

ICANON

Enable canonical mode (described below).
启用规范模式(如下所述)。

XCASE

 (not in POSIX; not supported under Linux) If ICANON is also set, terminal is uppercase only. 
 Input is converted   to lowercase, except for characters preceded by \. 
 On output, uppercase characters are preceded by \ and lowercase characters are converted to uppercase.
 [requires _BSD_SOURCE or _SVID_SOURCE or _XOPEN_SOURCE]
(在 POSIX 中不支持;在 Linux 下不支持)如果还设置了 ICANON,则终端仅是大写字母。 
输入转换为小写,但以 \ 开头的字符除外。 输出时,大写字符以 \ 开头,小写字符转换为大写。 
[需要 _BSD_SOURCE 或 _SVID_SOURCE 或 _XOPEN_SOURCE]

ECHO

Echo input characters.
回显输入字符。

ECHOE

If ICANON is also set, the ERASE character erases the preceding input character,
 and WERASE erases the preceding word.
如果还设置了 ICANON,则 ERASE 字符会擦除前面的输入字符,而 WERASE 会擦除前面的单词。

ECHOK

If ICANON is also set, the KILL character erases the current line.
如果还设置了 ICANON,则 KILL 字符会擦除当前行。

ECHONL

If ICANON is also set, echo the NL character even if ECHO is not set.
如果还设置了 ICANON,即使 ECHO 未设置,也要回显 NL 字符。

ECHOCTL

(not in POSIX) If ECHO is also set, terminal special characters other than TAB, NL, START, and STOP 
are echoed as ^X, where X is the character with ASCII code 0x40  greater than the special character.  
For example, character 0x08 (BS) is echoed as ^H.  [requires _BSD_SOURCE or_SVID_SOURCE]
(不在 POSIX 中)如果还设置了 ECHO,则除 TAB、NL、START 和 STOP 之外的终端特殊字符将作为 ^X 回显,
其中 X 是 ASCII 代码比特殊字符大 0x40 的字符。 例如,字符 0x08 (BS) 回显为 ^H。 [需要 _BSD_SOURCE 或 _SVID_SOURCE]

ECHOPRT

 (not in POSIX) If ICANON and ECHO are also set, characters are printed
  as they are being erased.  [requires _BSD_SOURCE or _SVID_SOURCE]
   (不在 POSIX 中)如果还设置了 ICANON 和 ECHO,则在擦除字符时打印字符。 
   [需要 _BSD_SOURCE 或 _SVID_SOURCE]

ECHOKE

(not in POSIX) If ICANON is also set, KILL is echoed by erasing each character on the line, as specified by 
ECHOE  and ECHOPRT.  [requires _BSD_SOURCE or _SVID_SOURCE]
(不在 POSIX 中)如果还设置了 ICANON,则通过擦除行上的每个字符来回显 KILL,如指定的
回声和回声。 [需要 _BSD_SOURCE 或 _SVID_SOURCE]

DEFECHO

(not in POSIX) Echo only when a process is reading.  (Not  implemented on Linux.)
(不在 POSIX 中)仅在进程读取时回显。 (未在 Linux 上实现。)

FLUSHO

(not in POSIX; not supported under Linux) Output is being   flushed.  This flag is toggled by typing 
the DISCARD character.  [requires _BSD_SOURCE or _SVID_SOURCE]
(不在 POSIX 中;在 Linux 下不支持)正在刷新输出。 通过键入来切换此标志丢弃字符。 [需要 _BSD_SOURCE 或 _SVID_SOURCE]

NOFLSH

Disable flushing the input and output queues when  generating signals for the INT, QUIT, and SUSP characters.
在为 INT、QUIT 和 SUSP 字符生成信号时禁用刷新输入和输出队列。

TOSTOP

Send the SIGTTOU signal to the process group of a  background process which tries to write to its
 controlling  terminal.
 将 SIGTTOU 信号发送到后台进程的进程组,该进程组尝试写入其控制终端。

PENDIN

(not in POSIX; not supported under Linux) All characters   in the input queue are reprinted when the 
next character is read.  (bash(1) handles typeahead this way.)  [requires _BSD_SOURCE or _SVID_SOURCE]
(不在 POSIX 中;在 Linux 下不支持)输入队列中的所有字符在读取下一个字符。 (bash(1) 以这种方式处理 typeahead。) [需要 _BSD_SOURCE 或 _SVID_SOURCE]

IEXTEN

Enable implementation-defined input processing.  This flag, as well as ICANON must be enabled for the special
 characters EOL2, LNEXT, REPRINT, WERASE to be interpreted,  and for the IUCLC flag to be effective.
 启用处理后输入。 此标志以及 ICANON 必须为特殊启用字符 EOL2、LNEXT、REPRINT、WERASE 被解释,并使 IUCLC 标志有效。

五、示例

int SerialPort::setParity(int fd, int databits, int stopbits, char parity) {
    struct termios options;
    if (tcgetattr(fd, &options)) {
        LOGD("Get attr failed");
        return FALSE;
    }
    options.c_cflag &= ~CSIZE;
    //设置数据位
    switch (databits)
    {
        case 7:
            options.c_cflag |= CS7;
            break;
        case 8:
            options.c_cflag |= CS8;
            break;
        default:
            LOGE("Unsupported data size!");
            return FALSE;
    }
    //设置校验位
    switch (parity) {
        case 'n':
        case 'N':
            options.c_cflag &= ~PARENB;                         /* Clear parity enable */
            options.c_iflag &= ~INPCK;                          /* Enable parity checking */
            break;
        case 'o':
        case 'O':
            options.c_cflag |= (PARODD | PARENB);               /* Set odd checking */
            options.c_iflag |= INPCK;                           /* Disnable parity checking */
            break;
        case 'e':
        case 'E':
            options.c_cflag |= PARENB;                          /* Enable parity */
            options.c_cflag &= ~PARODD;                         /* Transformation even checking */
            options.c_iflag |= INPCK;                           /* Disnable parity checking */
            break;
        case 'S':
        case 's':  /*as no parity*/
            options.c_cflag &= ~PARENB;
            options.c_cflag &= ~CSTOPB;
            break;
        default:
            LOGE("Unsupported parity!");
            return FALSE;
    }
    /* 设置停止位*/
    switch (stopbits) {
        case 1:
            options.c_cflag &= ~CSTOPB;
            break;
        case 2:
            options.c_cflag |= CSTOPB;
            break;
        default:
            LOGE("Unsupported stop bits!");
            return FALSE;
    }
    /* Set input parity option */
    if (parity != 'n')
        options.c_iflag |= INPCK;
    tcflush(fd, TCIFLUSH);
    options.c_cc[VTIME] = 150;                                  /* Set timeout to 15 seconds */
    options.c_cc[VMIN] = 0;                                     /* Update the options and do it NOW */
    if (tcsetattr(fd, TCSANOW, &options) != 0) {
        LOGE("Set attr failed");
        return FALSE;
    }
    return TRUE;
}

源码已同步gitHub:https://github.com/AinialJing/JniDemo
参考地址:https://man7.org/linux/man-pages/man3/termios.3.html

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值