Serial Programming Guide for POSIX Operating Systems(2)

 
第二章 配置串口
 
POSIX 终端接口
         很多系统都支持POSIX 终端接口来完成baud rate, character size等的设定。Posix结构在<termios.h>中,它最重要的两个函数是tcgetattr()和tcsetattr()。
 
Table 3 - Termios Structure Members
Member
Description
c_cflag
Control options
c_lflag
Line options
c_iflag
Input options
c_oflag
Output options
c_cc
Control characters
c_ispeed
Input baud (new interface)
c_ospeed
Output baud (new interface)
 
Control Options
        c_cflag控制波特率,数据位,奇偶校验位,停止位以及硬件流控制。常用的设置以constant形式提供。
Table 4 - Constants for the c_cflag Member
Constant
Description
CBAUD
Bit mask for baud rate
B0
0 baud (drop DTR)
B50
50 baud
B75
75 baud
B110
110 baud
B134
134.5 baud
B150
150 baud
B200
200 baud
B300
300 baud
B600
600 baud
B1200
1200 baud
B1800
1800 baud
B2400
2400 baud
B4800
4800 baud
B9600
9600 baud
B19200
19200 baud
B38400
38400 baud
B57600
57,600 baud
B76800
76,800 baud
B115200
115,200 baud
EXTA
External rate clock
EXTB
External rate clock
CSIZE
Bit mask for data bits
CS5
5 data bits
CS6
6 data bits
CS7
7 data bits
CS8
8 data bits
CSTOPB
2 stop bits (1 otherwise)
CREAD
Enable receiver
PARENB
Enable parity bit
PARODD
Use odd parity instead of even
HUPCL
Hangup (drop DTR) on last close
CLOCAL
Local line - do not change "owner" of port
LOBLK
Block job control output
CNEW_RTSCTS
CRTSCTS
Enable hardware flow control (not supported on all platforms)
 
        c_cflag成员中,CLOCAL和CREAD始终应该Enabled。CBAUD, B9600, etc是早期的接口,现在则被c_ispeed和c_ospeed所替代。
        小建议:设置c_flag等标志时,最好使用位操作AND, OR和NOT等.
 
Setting the Baud Rate
波特率在不同的操作系统中存储于不同的地方,在老操作系统中,存储于c_cflag中,而现在的操作系统中都存储于c_ispeed和c_ospeed成员变量中.可以运用函数 cfsetospeed(3)cfsetispeed(3)来设置波特率。
 
Listing 2– Setting the baud rate.
struct termios options;
/* getthe current options for the port…*/
tcgetattr(fd, &options);
/*set the baud rate to 19200… */
cfsetispeed(&options,B19200);
cfsetospeed(&options, B19200);
/*Enable the receive and set the local modet…*/
options.c_cflag |= (CLOCAL | CREAD);
/* set the new options for the port…*/
tcsetattr(fd, TCSCANOW, &options);
 
在函数tcsetattr中,TCSANNOW表示新设置的options立即起效。
                 
Constant
Description
TCSANOW
新的改变立即起效
TCSADRAIN
新的改变等到数据传输完毕才改变
TCSAFLUSH
刷新输入和输出缓冲区,再改变设置。
 
Setting the Character Size
options.c_cflag &= ~CSIZE; /*Mask the character size bits */
options.c_cflag |= CS8;           /* Select 8 data bits                  */
 
Setting Parity Checking
 
l            No Parity(8N1)
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
l            Even parity(7E1)
options.c_cflag |= PARENB;
options.c_cflag &= ~PARODD;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS7;
l            Odd parity(7O1)
options.c_cflag |= PARENB;
options.c_cflag |= PARODD;
options.c_cflag &=~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS7;
l            Space parity is setup the same as no parity(7S1)
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
Setting Hardware Flow Control
有些UNIX系统支持硬件流控制,即CTS(clear to send)和RTS(request to send)。如果c_cflag有常数CNEW_RTSCTS或CRTSCTS,则就有可能支持硬件流控制。
       options.c_cflag |= CNEW_RTSCTS; 
       options.c_cflag &= ~CNEW_RTSCTS;
 
Local Options
c_lflag控制着串口驱动怎样处理输入字符。c_lflag控制规范输入或者非规范输入。
 
Constant
Description
ISIG
使终端信号 SIGINTR, SIGSUSP, SIGDSUSP SIGQUIT 起作用
ICANON
使能规范输入(否则为非规范输入)
XCASE
规范大 / 小写表示
ECHO
回送
ECHOE
可见擦除符
ECHOK
回送 kill 字符
ECHONL
回送 NL
NOFLSH
在中断和退出键后不刷清
IEXTEN
使扩充的输入字符处理起作用
ECHOCTL
回送控制字符为 ^, 删除为 ~
ECHOPRT
硬拷贝的可见擦除方式
ECHOKE
kill 的可见擦除方式
FLUSHO
刷清输出
PENDIN
重新打印
TOSTOP
对于后台输出发送信号 SIGTTOU
 
规范输入
规范输入是当输入一行(以CR/LF为标志),终端驱动程序返回。
          options.c_lflag &= ~(ICANON|ECHO|ECHOE|ISIG);
非规范输入
在非规范输入中,数据不组成行的形式,输入是什么便是什么,即不处理特殊字符。至于读取多少字符,可以通过termios结构中的c_cc数组中的MIN和TIME来配合决定。
 
Input Options
成员c_iflag控制着处理从端口接收字符的过程。
 
Constant
Description
INPCK
打开输入奇偶校验
IGNPAR
忽略奇偶校验错
PARMRK
标记奇偶错
ISTRIP
去除奇偶校验位即第 8
IXON
使能输出软件流控制
IXOFF
使能输入软件流控制
IXANY
使任一字符都能启动输出
IGNBRK
忽略 BREAK 条件
BRKINT
收到 BREAK 时产生 SIGINT 信号
INLCR
将输入的 NL 转换为 CR 信号
IGNCR
忽略 CR
ICRNL
将输入的 CR 信号转换为 NL
IUCLC
将输入的大写字母转换为小写字母
IMAXBEL
在输入队列满时振铃
 
Setting Input Parity Options
当在c_cflag成员中打开奇偶校验使能(PARENB)时,应该在c_iflag中设定相应的标志。通常需要设定INPCK, IGNPAR, PARMRK和ISTRIP。
options.c_iflag |= (INPCK|ISTRIP);
当IGNPAR使能时,当字符奇偶校验出错时,会将NUL字符(000)送给程序。
Setting Software Flow Control
使能软件流控制需要使用IXON, IXOFF和IXANY。
options.c_iflag |= (IXON|IXOFF|IXANY);
关闭软件流控制如下:
options.c_ifla &= ~(IXON|IXOFF|IXANY);
其中XON(启动数据)和XOFF(停止数据)定义在c_cc数组中。
 
 
Output Options
成员c_oflag包含输出选项。
Constant
Description
OPOST
执行输出处理
OLCUC
将输出的小写字母转换为大写字母
ONLCR
NL 转换为 CR-NL
OCRNL
CR 转换为 NL
NOCR
No CR output at column 0
ONLRET
NL 执行 CR 功能
OFILL
对于延迟使用填充符
OFDEL
填充符为 DEL ,否则为 NUL
NLDLY
延迟屏蔽
NL0
没有延迟
NL1
延迟 100 毫秒
CRDLY
CR 延迟屏蔽
CR0
CR 没有延迟
CR1
延迟时间取决于当前列位置
CR2
延迟 100 毫秒
CR3
延迟 150 毫秒
TABDLY
水平制表符延迟屏蔽
TAB0
TAB 没有延迟
TAB1
延迟取决于当前列位置
TAB2
延迟 100 毫秒
TAB3
将水平制表符扩展为空格
BSDLY
退格延迟屏蔽
BS0
退格延迟为 0
BS1
退格延迟为 50 毫秒
VTDLY
垂直制表符延迟屏蔽
VT0
垂直制表符延迟为 0
VT1
垂直制表符延迟为 2
FFDLY
换页延迟屏蔽
FF0
换页延迟为 0
FF1
换页延迟为 2
 
Choosing Processed Output
options.c_oflag |= OPOST;
最可能使用的是ONLCR选项
Choosing Raw Output
             options.c_oflag &= ~OPOST;
此时其他选项无效。
 
Control Characters
c_cc数组包好了定义的控制字符。
 
Constant
Description
Key
VINTR
Interrupt
CTRL-C
VQUIT
Quit
CTRL-Z
VERASE
Erase
Backspace(BS)
VKILL
Kill-line
CTRL-U
VEOF
End-of-file
CTRL-D
VEOL
End-of-line
Carriage return(CR)
VEOL2
Second end-of-line
Line feed(LF)
VMIN
minimum number of character to read
 
VSTART
Start-flow
CTRL-Q(XON)
VSTOP
Stop flow
CTRL-S(XOFF)
VTIME
Time to wait for data(tenths of seconds)
 
 
Setting Software Flow Control Characters
VSTART和VSTOP设定启动符(021 octal)和停止数据符(023 octal)。在ASCII中代表XON和XOFF。
Setting Read Timeouts
VMIN制定了所需读取的最小字符,而VTIME则设定了等待的时间。两者配合使用便可完成非规范方式下的数据读取。
 
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值