




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
Linux串行口是計(jì)算機(jī)一種慣用的接口,含有連接線少,通訊簡樸,得到廣泛的RS-232-C接口(EIARS-232-C)1970年由美國電子工業(yè)協(xié)會(huì)(EIA)聯(lián)合貝爾系統(tǒng)、調(diào)制解調(diào)器廠家及計(jì)算機(jī)終端生產(chǎn)廠家共同制(DTE)(DCE)255連4%50英尺。linux文獻(xiàn)中,全部的設(shè)備文獻(xiàn)普通都位于/dev下,其中串口一、串口二/dev/ttyS0/dev/ttyS1linuxreadwrite函數(shù)來完畢,#include#include<stdio.h>//#include#include<fcntl.h>//#include<unistd.h>linux原則函數(shù)定義#include<sys/ioctl.h>#include<termios.h> POSIX終端控制訂義#include<sys/time.h>#includeintintchar*device=//設(shè)備途徑,初始使用{//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/*輸入模式標(biāo)志/*輸出模式標(biāo)志/*控制模式標(biāo)志/*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/*設(shè)立數(shù)據(jù)位數(shù){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)*設(shè)立為奇效驗(yàn)*/options.c_iflag|=INPCK;//INPCK:奇偶校驗(yàn)使能case'e':caseoptions.c_cflag|=PARENB; /*Enableparity*/options.c_cflag&=~PARODD; /*轉(zhuǎn)換為偶效驗(yàn)*/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個(gè)停止位caseoptions.c_cflag|=CSTOPB;//2個(gè)停止位fprintf(stderr,"Unsupportedstopbits\n");return(FALSE);}////options.c_cc[VTIME]=150;/*設(shè)立超時(shí)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,//將輸入波特率設(shè)為//將輸出波特率設(shè)為19200cfsetispeed(&newtio,B19200);cfsetospeed(&newtio,//newtio.c_cflag|=(CLOCAL|//無校驗(yàn)8位數(shù)據(jù)位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)設(shè)立串口接受端口集tv.tv_sec=2;tv.tv_usec=while(FD_ISSET(fd,&rfds)){//每次循環(huán)都要清空,否則不會(huì)檢測到有變化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的函數(shù)格式(我所說的是Unix系統(tǒng)下的伯克利socketintselect(intintselect(intmaxfdp,fd_set*readfds,fd_set*writefds,fd_set*errorfds,structstruct能夠理解為一種集合,這個(gè)集合中寄存的是文獻(xiàn)描述符(filedescriptor),即socket句柄就是一種文獻(xiàn)描述符。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):檢查集合中指定的文獻(xiàn)描述符與否能夠structstructstructlongtv_sec;longtv_unsec;}selectint1Windows中這個(gè)參數(shù)的值無所謂,能夠設(shè)立不對的。fd_setfd_set構(gòu)造的指針,這個(gè)集合中應(yīng)當(dāng)涉及文獻(xiàn)描述符,我們是要監(jiān)如果這個(gè)集合中有一種文獻(xiàn)可讀,select0的值,表達(dá)有timeout參數(shù)再判斷與否超時(shí),若超出timeout的時(shí)間,select0NULL值,表達(dá)fd_setfd_set構(gòu)造的指針,這個(gè)集合中應(yīng)當(dāng)涉及文獻(xiàn)描述符,我們是要監(jiān)如果這個(gè)集合中有一種文獻(xiàn)可寫,select0的值,表達(dá)有timeout參數(shù)再判斷與否超時(shí),若超出timeout的時(shí)間,select0NULL值,表達(dá)fd_setstructtimeval*selectselect處在三種狀態(tài),NULLselect置于阻塞狀態(tài),000,有變化返回一種正值;第三,timeout0selecttimeout時(shí)間內(nèi)阻塞,超時(shí)負(fù)值:select錯(cuò)誤正值:某些文獻(xiàn)可讀寫或出錯(cuò)0:等待超時(shí),沒有可讀selectselectFD_ZEROFD_SET來初始selectFD_ISSET測試描述符集,F(xiàn)D_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;//顧客號(hào)高8位unsignedcharUSER_NUMBER_L;//顧客號(hào)低8位unsignedcharUSER_RIGHT; //顧客權(quán)限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);//設(shè)立串口接受端口集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')) 確實(shí){retval=select(fd+1,&rfds,NULL,NULL,&tv);if(!retval)//nodatas{}}{
}}}}voidinit_ttyS(int{structtermiosbzero(&newtio,tcgetattr(fd //cfsetispeed(&newtio //將輸入波特率設(shè)為cfsetospeed(&newtio,B19200); //將輸出波特率設(shè)為19200newtio.c_cflag|=(CLOCAL|CREAD); //使能接受并使能本地狀態(tài)
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度生豬養(yǎng)殖與農(nóng)業(yè)產(chǎn)業(yè)扶貧合作協(xié)議
- 二零二五年度制造業(yè)工傷責(zé)任保險(xiǎn)合同
- 2025年度男方道歉夫妻共同生活保證協(xié)議
- 2025年度飯店短期勞務(wù)合同-客房服務(wù)員職業(yè)健康與安全協(xié)議
- 二零二五年度物業(yè)公司員工勞動(dòng)合同(含社區(qū)文化活動(dòng))
- 監(jiān)理技術(shù)服務(wù)合同
- 綠色數(shù)據(jù)中心建設(shè)運(yùn)營合同
- 環(huán)境影響評估結(jié)果展示表
- 股份制企業(yè)股權(quán)分配與管理制度文書
- 財(cái)務(wù)與成本控制管理細(xì)則
- 2024年高考英語易錯(cuò)題 閱讀理解:推理判斷題4大陷阱(教師版新高考專用)
- 醫(yī)院環(huán)境衛(wèi)生學(xué)監(jiān)測和院感控制課件
- 《力與形變》教學(xué)課件(一)
- 湖北省2024年村干部定向考試真題
- 部編版三年級語文下冊期中試卷及參考答案
- JT-T-1199.1-2018綠色交通設(shè)施評估技術(shù)要求第1部分:綠色公路
- 酒店能耗分析報(bào)告
- 桃花紅杏花紅混聲合唱簡譜
- DL-T995-2016繼電保護(hù)和電網(wǎng)安全自動(dòng)裝置檢驗(yàn)規(guī)程
- 2024年蘇州農(nóng)業(yè)職業(yè)技術(shù)學(xué)院單招職業(yè)適應(yīng)性測試題庫含答案
- 2024年江蘇經(jīng)貿(mào)職業(yè)技術(shù)學(xué)院單招職業(yè)適應(yīng)性測試題庫含答案
評論
0/150
提交評論