面就是用C语言编写控制串行口的程序.doc

上传人:xin****828 文档编号:6713135 上传时间:2020-03-02 格式:DOC 页数:7 大小:16KB
返回 下载 相关 举报
面就是用C语言编写控制串行口的程序.doc_第1页
第1页 / 共7页
面就是用C语言编写控制串行口的程序.doc_第2页
第2页 / 共7页
面就是用C语言编写控制串行口的程序.doc_第3页
第3页 / 共7页
点击查看更多>>
资源描述
面就是用C语言编写控制串行口的程序。#includedos.h#includestdlib.h#includestdio.h#definePORT0voidSendFile(char*fname);/*发送文件*/voidSend(ints);/*发送一个字节*/voidSendFileName(char*fname);/*发送文件名*/voidReceiveFile();/*接收文件*/voidGetFileName(char*f);/*接收文件名*/voidInitPort(intport,unsignedcharpara);/*初始化端口*/voidSendPort(intport,charc);/*端口发送*/intReadPort(intport);/*读端口字节*/intCheckState(intport);/*检查端口状态*/intReceive(intport,int*G);/*接收一个字节*/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(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;/*取文件中一个字节的低4位*/Send(s);s=ch/16;/*取文件中一个字节的高4位*/Send(s);while(!feof(fp);s=46;/*发送文件结束信息*/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=ch%16;/*取文件名中一个字节的低4位*/Send(s);s=ch/16;Send(s);/*取文件名中一个字节的低4位*/s=32;/*发送文件名结束标志*/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);/*循环为检测每次接受的数据是否为新数据,如果不是,*/*则用此次接收的数据覆盖上次接收的数据*/G1=ReadPort(PORT);G2=Receive(PORT,&G1);doG3=Receive(PORT,&G2);ch=(char)(G1%16+G2*16);/*恢复分开的数据,组合高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)intport,*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(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(0x14,);voidSendPort(port,c)intport;charc;unionREGSreg;reg.x.dx=port;reg.h.al=c;reg.h.ah=1;int86(0x14,);if(reg.h.ah&128)printf(nSendmistakes!);exit(1);intReadPort(port)intport;unionREGSreg;while(!(CheckState(port)&256)if(kbhit()/*如端口长期无数据可人为终止等待*/printf(Pressanykeytoexit.);getch();exit(1);reg.x.dx=port;reg.h.ah=2;int86(0x14,);if(reg.h.ah&128)printf(nReadmistake!);exit(1);returnreg.h.al;intCheckState(port)intport;unionREGSreg;reg.x.dx=port;reg.h.ah=3;int86(0x14,);returnreg.x.ax;
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 图纸专区 > 中学资料


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!