Linux下串口程序开发_第1页
Linux下串口程序开发_第2页
Linux下串口程序开发_第3页
Linux下串口程序开发_第4页
Linux下串口程序开发_第5页
已阅读5页,还剩11页未读 继续免费阅读

付费下载

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Linux串行口是计算机一种惯用的接口,含有连接线少,通讯简朴,得到广泛的RS-232-C接口(EIARS-232-C)1970年由美国电子工业协会(EIA)联合贝尔系统、调制解调器厂家及计算机终端生产厂家共同制(DTE)(DCE)255连4%50英尺。linux文献中,全部的设备文献普通都位于/dev下,其中串口一、串口二/dev/ttyS0/dev/ttyS1linuxreadwrite函数来完毕,#include#include<stdio.h>//#include#include<fcntl.h>//#include<unistd.h>linux原则函数定义#include<sys/ioctl.h>#include<termios.h> POSIX终端控制订义#include<sys/time.h>#includeintintchar*device=//设备途径,初始使用{//if(!strcmp(argv[t],"-d")&&(argc>{device=}}{printf("cannotuse/dev/tts/1\n");return-1;}fd=open(device,//不允许使用UART1,由于它已和PC//if(fd //{printf("opendeviceerror\n");return-1;}struct unsignedstruct unsignedshortunsignedshortunsignedshortunsignedshortunsignedcharunsignedchar/*输入模式标志/*输出模式标志/*控制模式标志/*localmodeflags/*linediscipline/*controlcharactersstructstructtermiosOpt;tcgetattr(fd,&Opt);////*@briefintspeedintintspeed_arr[]={B38400,B19200,B9600,B4800,B2400,B1200,B300,B38400,B19200,B9600,B4800,B2400,B1200,B300,};intname_arr[]={38400,19200,9600,4800,2400,1200,300,19200,9600,4800,2400,1200,300,voidset_speed(intfd,intspeed){ structtermios tcgetattr(fd,&Opt);for(i=0;i<sizeof(speed_arr)/sizeof(int);i++){if(speed==name_arr[i]){tcflush(fd,TCIOFLUSH);cfsetispeed(&Opt,speed_arr[i]);cfsetospeed(&Opt,status=tcsetattr(fd1,TCSANOW,&Opt);if(status!=0){perror("tcsetattrfd1");}}}}类型intdatabits类型intstopbits类型int取值为7或者取值为1或者parity类型intintset_Parity(intfd,intdatabits,intstopbits,int{structtermiosif(tcgetattrfd,&options)!=0){//perror("SetupSerial1");}options.c_cflag&=switch(databits/*设立数据位数{caseoptions.c_cflag|=CS7;caseoptions.c_cflag|=CS8;fprintf(stderr,"Unsupporteddatasize\n");return}switch(parity){case //caseoptions.c_cflag&=~PARENB; /*Clearparityenable*/casecaseoptions.c_cflag|(PARODD|PARENB)*设立为奇效验*/options.c_iflag|=INPCK;//INPCK:奇偶校验使能case'e':caseoptions.c_cflag|=PARENB; /*Enableparity*/options.c_cflag&=~PARODD; /*转换为偶效验*/options.c_iflag|=INPCK; /*Disnableparitychecking*/case Spacecase's':/*asnoparity*/options.c_cflag&=~PARENB;options.c_cflag&=~CSTOPB;options.c_iflag|=INPCK;fprintf(stderr,"Unsupportedfprintf(stderr,"Unsupportedparity\n");return(FALSE);}//switch{caseoptions.c_cflag&=~CSTOPB;//1个停止位caseoptions.c_cflag|=CSTOPB;//2个停止位fprintf(stderr,"Unsupportedstopbits\n");return(FALSE);}////options.c_cc[VTIME]=150;/*设立超时15seconds*/options.c_cc[VMIN]0*UpdatetheoptionsanddoitNOW*///if(tcsetattr(fd,TCSANOW,&options)!={perror("SetupSerial3");return(FALSE);}return}newtio.c_lflag&=~(ICANONnewtio.c_lflag&=~(ICANON|ECHO|ECHOE|ISIG);//newtio.c_oflag&~(OPOST);//voidvoidinit_ttyS(int{structtermiosbzero(&newtio,//tcgetattr(fd,//将输入波特率设为//将输出波特率设为19200cfsetispeed(&newtio,B19200);cfsetospeed(&newtio,//newtio.c_cflag|=(CLOCAL|//无校验8位数据位1位停止位newtio.c_cflag&~PARENB;newtio.c_cflag&~CSTOPB;newtio.c_cflag&=~CSIZE;8newtio.c_cflag|=//newtio.c_lflag&=~(ICANON|ECHO|ECHOE|ISIG);newtio.c_oflag&=~(OPOST);// = =//tcflush(fd,//}charbuff[1024]; intreadBytecharbuff[1024]; intreadByte=void{//read(fd,void{//read(fd,RXBUF,#ifintret,n,pos,retval;fd_setrfds;structtimevaltvpos0;//for(n=0;n<RX_len;{RXBUF[n]=}FD_ZERO(&rfds);//清空串口接受端口集FD_SET(fd,&rfds)设立串口接受端口集tv.tv_sec=2;tv.tv_usec=while(FD_ISSET(fd,&rfds)){//每次循环都要清空,否则不会检测到有变化FD_ZERO(&rfds);//清空串口接受端口集FD_SET(fd,&rfds);//retval=select(fd+1,&rfds,NULL,NULL,&tv);if(retval==-1){}else{//retret=read(fd,RXBUF,RX_len);pos+=ret;//printf("ret=%dif((RXBUF[pos-2]'\r'&RXBUF[pos-1]'\n'){retval=select(fd+1,&rfds,NULL,NULL,&tv);if(!retval)//nodatas{}}}{}}}com口堵塞,程序就死了。Select的函数格式(我所说的是Unix系统下的伯克利socketintselect(intintselect(intmaxfdp,fd_set*readfds,fd_set*writefds,fd_set*errorfds,structstruct能够理解为一种集合,这个集合中寄存的是文献描述符(filedescriptor),即socket句柄就是一种文献描述符。fd_set集合能够通过某些宏由人为来操作,例FD_ZERO(fd_set*set):FD_SET(intfd,fd_set*set):FD_CLR(intfd,fd_set*set):FD_ISSET(intfd,fd_set*set):检查集合中指定的文献描述符与否能够structstructstructlongtv_sec;longtv_unsec;}selectint1Windows中这个参数的值无所谓,能够设立不对的。fd_setfd_set构造的指针,这个集合中应当涉及文献描述符,我们是要监如果这个集合中有一种文献可读,select0的值,表达有timeout参数再判断与否超时,若超出timeout的时间,select0NULL值,表达fd_setfd_set构造的指针,这个集合中应当涉及文献描述符,我们是要监如果这个集合中有一种文献可写,select0的值,表达有timeout参数再判断与否超时,若超出timeout的时间,select0NULL值,表达fd_setstructtimeval*selectselect处在三种状态,NULLselect置于阻塞状态,000,有变化返回一种正值;第三,timeout0selecttimeout时间内阻塞,超时负值:select错误正值:某些文献可读写或出错0:等待超时,没有可读selectselectFD_ZEROFD_SET来初始selectFD_ISSET测试描述符集,FD_CLR来去除描述符集。charbuffer[1024];charbuffer[1024]; nByte=write(fd,buffer#include<stdio.h>#include<string.h>#include<stdlib.h>#include<fcntl.h>#include<unistd.h>#include<sys/ioctl.h>#include<stdio.h>#include<string.h>#include<stdlib.h>#include<fcntl.h>#include<unistd.h>#include<sys/ioctl.h>#include<termios.h>#include<sys/select.h>#include//#include#defineACK_SUCCESS#defineACK_FAIL#defineACK_FULL#define //#defineACK_USER_EXIST////////#define#define//externunsignedcharTXBUF[9900];externunsignedcharRXBUF[9900];externunsignedcharrev_ok;externunsignedintTX_len;externunsignedintexternunsignedcharone_onecontrast(unsignedcharuser_number_h,unsignedcharexternunsignedcharone_morecontrast(void);externunsignedcharGet_UserNumber_Right(void);externunsignedcharset_addmode(unsignedcharexternunsignedcharadd_Fingerprint(unsignedchartime_number,unsignedcharunsignedcharuser_number_l,unsignedexternunsignedcharexternunsignedchardel_oneuser(unsignedcharuser_number_h,unsignedcharexternunsignedcharintunsignedcharUSER_NUMBER_H;//顾客号高8位unsignedcharUSER_NUMBER_L;//顾客号低8位unsignedcharUSER_RIGHT; //顾客权限unsignedchar //unsignedchar //unsignedcharCharToHex(unsignedcharch);unsignedcharselect_one_onecontrast(void);voidselect_Get_Usernumber_right(void); intSERIAL_TX(void){intretwrite(fdTXBUF, //if(ret //{ printf("Writedeviceerror!\n");return-1; ret=}return} voidSERIAL_RX(void){//read(fd,RXBUF,#ifintret,n,pos,retval;fd_setrfds;structtimevaltvpos0;//指向接受缓冲tv.tv_sec=2;tv.tv_usec=for(n=0;n<RX_len;{RXBUF[n]=} //while(1)//{FD_ZERO(&rfds);//清空串口接受端口集FD_SET(fd,&rfds);//设立串口接受端口集retvalselect(fd+1,&rfds,NULL,NULL,&tv);if(retval==-1){}else{//ret=read(fd,RXBUF,RX_len);pos+=ret;//printf("ret=%dif((RXBUF[pos-2]=='\r')&(RXBUF[pos-1]=='\n')) 确实{retval=select(fd+1,&rfds,NULL,NULL,&tv);if(!retval)//nodatas{}}{

}}}}voidinit_ttyS(int{structtermiosbzero(&newtio,tcgetattr(fd //cfsetispeed(&newtio //将输入波特率设为cfsetospeed(&newtio,B19200); //将输出波特率设为19200newtio.c_cflag|=(CLOCAL|CREAD); //使能接受并使能本地状态

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论