免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 778 | 回复: 0
打印 上一主题 下一主题

Serial Programming Guide(2) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-01-10 16:03 |只看该作者 |倒序浏览
第二章配置串口
 
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, SIGDSUSPSIGQUIT起作用
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则设定了等待的时间。两者配合使用便可完成非规范方式下的数据读取。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP