Linux下串口程序实例.pdf

Linux下串口程序实例.pdf

ID:57637676

大小:59.50 KB

页数:4页

时间:2020-08-29

Linux下串口程序实例.pdf_第1页
Linux下串口程序实例.pdf_第2页
Linux下串口程序实例.pdf_第3页
Linux下串口程序实例.pdf_第4页
资源描述:

《Linux下串口程序实例.pdf》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库

1、#include/*标准输入输出定义*/#include/*标准函数库定义*/#include/*Unix标准函数定义*/#include/**/#include/**/#include/*文件控制定义*/#include/*PPSIX终端控制定义*/#include/*错误号定义*//***@brief设置串口通信速率*@paramfd类型int打开串口的文件句柄*@paramspeed类型int串口速度*@returnvoid

2、*/intspeed_arr[]={B38400,B19200,B9600,B4800,B2400,B1200,B300,B38400,B19200,B9600,B4800,B2400,B1200,B300,};intname_arr[]={38400,19200,9600,4800,2400,1200,300,38400,19200,9600,4800,2400,1200,300,};voidset_speed(intfd,intspeed){inti;intstatus;structtermiosOpt;tcgetattr(fd,&Opt);for(i=0;i

3、rr)/sizeof(int);i++){if(speed==name_arr[i]){tcflush(fd,TCIOFLUSH);cfsetispeed(&Opt,speed_arr[i]);cfsetospeed(&Opt,speed_arr[i]);status=tcsetattr(fd,TCSANOW,&Opt);if(status!=0)perror("tcsetattrfd1");return;}tcflush(fd,TCIOFLUSH);}}/***@brief设置串口数据位,停止位和效验位*@paramfd类型int打开的串口文件句柄**@paramdatabits类型int数

4、据位取值为7或者8**@paramstopbits类型int停止位取值为1或者2**@paramparity类型int效验类型取值为N,E,O,,S*/intset_Parity(intfd,intdatabits,intstopbits,intparity){structtermiosoptions;if(tcgetattr(fd,&options)!=0){perror("SetupSerial1");return(FALSE);}options.c_cflag&=~CSIZE;switch(databits)/*设置数据位数*/{case7:options.c_cflag

5、=CS7;br

6、eak;case8:options.c_cflag

7、=CS8;break;default:fprintf(stderr,"Unsupporteddatasize");return(FALSE);}switch(parity){case'n':case'N':options.c_cflag&=~PARENB;/*Clearparityenable*/options.c_iflag&=~INPCK;/*Enableparitychecking*/break;case'o':case'O':options.c_cflag

8、=(PARODD

9、PARENB);/*设置为奇效验*/options.c_

10、iflag

11、=INPCK;/*Disnableparitychecking*/break;case'e':case'E':options.c_cflag

12、=PARENB;/*Enableparity*/options.c_cflag&=~PARODD;/*转换为偶效验*/options.c_iflag

13、=INPCK;/*Disnableparitychecking*/break;case'S':case's':/*asnoparity*/options.c_cflag&=~PARENB;options.c_cflag&=~CSTOPB;break;default:fprintf(stderr,

14、"Unsupportedparity");return(FALSE);}/*设置停止位*/switch(stopbits){case1:options.c_cflag&=~CSTOPB;break;case2:options.c_cflag

15、=CSTOPB;break;default:fprintf(stderr,"Unsupportedstopbits");return(FALSE);}

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。