最新面就是用C語言編寫控制串行口的程序資料_第1頁
最新面就是用C語言編寫控制串行口的程序資料_第2頁
最新面就是用C語言編寫控制串行口的程序資料_第3頁
最新面就是用C語言編寫控制串行口的程序資料_第4頁
最新面就是用C語言編寫控制串行口的程序資料_第5頁
全文預覽已結束

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

1、面就是用C語言編寫控制串行口的程序。#includedos.h#includestdlib.h#includestdio.h#definePORT0voidSendFile(char*fname);/*發(fā)送文件*/voidSend(ints);/*發(fā)送一個字節(jié)*/voidSendFileName(char*fname);/*發(fā)送文件名*/voidReceiveFile();/*接收文件*/voidGetFileName(char*f);/*接收文件名*/voidInitPort(intport,unsignedcharpara);/*初始化端口*/voidSendPort(intport,ch

2、arc);/*端口發(fā)送*/intReadPort(intport);/*讀端口字節(jié)*/intCheckState(intport);/*檢查端口狀態(tài)*/intReceive(intport,int*G);/*接收一個字節(jié)*/main(intargc,char*argv)if(argc2)printf(PleaseinputR(receive)orS(sent)parametre:);exit(1);InitPort(PORT,231);if(*argv1=S)/*檢查選擇的有效性*/SendFile(argv2);elseif(*argv1=R)ReceiveFile();elseprintf

3、(Errorparament.Pleaseinputagain.);exit(1);voidSendFile(char*fname)FILE*fp;intch,s;if(fp=fopen(fname,rb)=NULL)printf(Cantopenthefile.n);exit(1);SendFileName(fname);doch=(int)getc(fp);if(ferror(fp)printf(Errorreadingfile.n);break;s=ch%16;/*取文件中一個字節(jié)的低4位*/Send(s);s=ch/16;/*取文件中一個字節(jié)的高4位*/Send(s);while(!f

4、eof(fp);s=46;/*發(fā)送文件結束信息*/Send(s);Send(s);fclose(fp);voidSend(s)ints;intG;SendPort(PORT,s);G=ReadPort(PORT);/*等待握手信號*/if(s!=G)s=s+16;doSendPort(PORT,s);G=ReadPort(PORT);/*等待握手信號*/while(s!=G);voidSendFileName(fname)char*fname;ints,ch;printf(Nowtransmitthefile.Pleasewait.);while(*fname)ch=(int)fname+;s

5、=ch%16;/*取文件名中一個字節(jié)的低4位*/Send(s);s=ch/16;Send(s);/*取文件名中一個字節(jié)的低4位*/s=32;/*發(fā)送文件名結束標志*/Send(s);Send(s);voidReceiveFile()FILE*fp;charch;intG1,G2,G3;charfname15;GetFileName(fname);printf(Receivingfile%s.n,fname);remove(fname);if(fp=fopen(fname,wb)=NULL)printf(Cantopenoutputfile.n);exit(1);/*循環(huán)為檢測每次接受的數(shù)據(jù)是否

6、為新數(shù)據(jù),如果不是,*/*則用此次接收的數(shù)據(jù)覆蓋上次接收的數(shù)據(jù)*/G1=ReadPort(PORT);G2=Receive(PORT,&G1);doG3=Receive(PORT,&G2);ch=(char)(G1%16+G2*16);/*恢復分開的數(shù)據(jù),組合高4位和低4位*/putc(ch,fp);if(ferror(fp)printf(nErrorwritingfile.);exit(1);G2=Receive(PORT,&G3);G1=G3;while(G1/16!=48);printf(nTransmitfinished.);fclose(fp);intReceive(port,G)i

7、ntport,*G;intGM;SendPort(port,*G);GM=ReadPort(port);if(GM/16=0)returnGM;elseif(GM/16=1)do*G=GM;SendPort(port,GM);GM=ReadPort(port);while(GM/16=1);returnGM;voidGetFileName(char*f)intG1,G2,G3;charch;G1=ReadPort(PORT);G2=ReadPort(PORT);doG3=Receive(PORT,&G3);ch=(char)(G1%16+G2/16);*f=ch;*f+;G2=Receive(

8、PORT,&G3);G1=G3;while(G1/16!=32);printf(Filenametransmitfinished.n);voidInitPort(port,para)intport;unsignedcharpara;unionREGSreg;reg.x.dx=port;reg.h.ah=0;reg.h.al=para;int86(0 x14,?,?);voidSendPort(port,c)intport;charc;unionREGSreg;reg.x.dx=port;reg.h.al=c;reg.h.ah=1;int86(0 x14,?,?);if(reg.h.ah&128)printf(nSendmistakes!);exit(1);intReadPort(port)intport;unionREGSreg;while(!(CheckState(port)&256)if(kbhit()/*如端口長期無數(shù)據(jù)可人為終止等待*/printf(Pressanykeytoexit.);getch();exit(1);reg.x.dx=port;reg.h.ah=2;int86(0 x14,?,?);if(reg.h.ah&128)prin

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論