面就是用C语言编写控制串行口的程序.doc_第1页
面就是用C语言编写控制串行口的程序.doc_第2页
面就是用C语言编写控制串行口的程序.doc_第3页
面就是用C语言编写控制串行口的程序.doc_第4页
面就是用C语言编写控制串行口的程序.doc_第5页
已阅读5页,还剩3页未读, 继续免费阅读

下载本文档

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

文档简介

1、。面就是用 C 语言编写控制串行口的程序。#includedos.h#includestdlib.h#includestdio.h#definePORT0void SendFile(char*fname);/*发送文件 */void Send(ints);/* 发送一个字节*/void SendFileName(char*fname);/* 发送文件名 */void ReceiveFile();/* 接收文件 */void GetFileName(char*f);/* 接收文件名 */void InitPort(intport,unsignedchar para);/* 初始化端口 */voi

2、d SendPort(intport,charc);/* 端口发送 */intReadPort(intport);/* 读端口字节 */intCheckState(intport);/* 检查端口状态*/intReceive(intport,int*G);/* 接收一个字节 */main(intargc,char*argv)if(argc2)printf(PleaseinputR(receive)or S(sent)parametre:);exit(1);InitPort(PORT,231);if(*argv1=S)/* 检查选择的有效性*/SendFile(argv2);else if(*a

3、rgv1=R)ReceiveFile();elseprintf(Errorparament.Pleaseinputagain.);。1。exit(1);void SendFile(char*fname)FILE *fp;intch,s;if(fp=fopen(fname,rb)=NULL)printf(Cantopen the file.n);exit(1);SendFileName(fname);doch=(int)getc(fp);if(ferror(fp)printf(Errorreadingfile.n);break;s=ch%16; /* 取文件中一个字节的低 Send(s);4位*

4、/s=ch/16;/* 取文件中一个字节的高4位*/Send(s);while(!feof(fp);s=46; /* 发送文件结束信息*/Send(s);Send(s);fclose(fp);void Send(s)。2。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;int s,ch;printf(Now tra

5、nsmit the file.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;。3。char ch;intG1,G2,G3;char fname15;GetFileName(fname);printf(Receivingfile%s.n,fname);remove(fname);if(fp=fop

6、en(fname,wb)=NULL)printf(Cantopen output file.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=G

7、3;while(G1/16!=48);printf(nTransmitfinished.);fclose(fp);intReceive(port,G)。4。intport,*G;intGM;SendPort(port,*G);GM=ReadPort(port);if(GM/16=0)returnGM;else if(GM/16=1)do*G=GM;SendPort(port,GM);GM=ReadPort(port);while(GM/16=1);returnGM;void GetFileName(char*f)intG1,G2,G3;char ch;G1=ReadPort(PORT);G2=

8、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);。5。printf(Filenametransmitfinished.n);void InitPort(port,para)intport;unsigned char para;union REGSreg;reg.x.dx=port;reg.h.ah=0;reg.h.al=para;int86(0x14,?,?);void SendPort(port,c)intpor

9、t;char c;union REGSreg;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;union REGSreg;。6。while(!(CheckState(port)&256)if(kbhit()/*如端口长期无数据可人为终止等待*/printf(Pressany key to exit.);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;intCheck

温馨提示

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

评论

0/150

提交评论