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

下载本文档

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

文档简介

1、面就是用C语言编写控制串行口的程序。include dos.h#include stdlib.h #include stdio.h #define PORT 0 void SendFile(char *fname); /* 发送文件 */ void Send(int s); /* 发送一个字节 */ void SendFileName(char *fname); /* 发送 文件名 */ void ReceiveFile(); /* 接收文件 7 void GetFileName(char *f); /* 接收文件名 */ void lnitPort(int port,unsigned cha

2、r para); /* 初始化端口 */ void SendPort(int port,char c); /* 端发送 */ int ReadPort(int port);广读端字节 7 int CheckState(int port);广检查端 口状态 */int Receive(int port,int *G); /* 接收一个字节 */ main(int argc,char *argv)if(argc2)printf(Please in put R(receive) or S(se nt) parametre:); exit(1);lnitPort(PORT,231);if(*argv1

3、=,mSm,) /*检查选择的有效性*/SendFile(argv );else if(*argv1=R,M,)ReceiveFile();elseprintf(HError parame nt.Please in put agai n.);exit(1);)void SendFile(char 幷name)(FILE *fp;int ch,s;if(fp=fopen(fname,rb)=NULL)printf(,Can,mt open the fileAn);exit(1);)SendFileName(f name);doch=(i nt)getc(fp);if(ferror(fp)prin

4、廿(”Error reading file.nH);break;)4位*/s=ch%16; /*取文件中一个字节的低Sen d(s);s=ch/16; /*取文件中一彳、字节的高Send(s);while(!feof(fp);s=46;/*发送文件结束信息*/Send(s);Send(s);fclose(fp);void Send(s)int s;int G;SendPort(PORT,s);G=ReadPort(PORT); /* 等待握手信号 */ if(s!=G) s=s+16;doSendPort(PORT,s);G=ReadPort(PORT);/* 等待握手信号 */while(s

5、!=G);void SendFileName(fname)char 节name;(int s,ch;printf(”Now transmit the file.Please wait”);while(*fname)ch=(int)f name+;s=ch%16;广取文件名中一个字节的低4位*/Send(s);s=ch/16;Send(s);/*取文件名中一个字节的低4位*/s=32;/*发送文件名结束标志*/Sen d(s);Send(s);void ReceiveFile()FILE *fp;char ch;int G1,G2,G3;char fn ame15;GetFileName(fna

6、me);prin廿(“Receiving file %s.n”,fname);remove(fname);if(fp=fopen(fname,wb)=NULL)(printf(Can“”t open output file.nH);exit(1);/*循环为检测每次接受的数据是否为新数据,如果不是,*/*则用此次接收的数据覆盖上次接收的数据*/G1 =ReadPort(PORT);G2=Receive(POR T,&G1);doG3=Receive(POR T,&G2);ch=(char)(G1%16+G2*16);/*恢复分幵的数据,组合高4位和低4位7 putc(ch,fp);if(fer

7、ror(fp)printf(nError writing file.);exit(1);G2=Receive(POR T,&G3);G1 =G3;while(G1/16!=48);prin tf(nTransmit fin ished.);fclose(fp);int Receive(port,G) int port,*G;SendPort(port,*G);GM=ReadPort(port);if(GM/16=0)return GM;else if(GM/16=1)do*G=GM;SendPort(port,GM);GM=ReadPort(port);while(GM/16=1);retur

8、n GM;)void GetFileName(char *f)intG1,G2,G3;char ch;G1 =ReadPort(PORT);G2=ReadPort(PORT);doG3=Receive(PORT,&G3); ch=(char)(G1%16+G2/16);*f=ch;*f+;G2=Receive(POR T,&G3);G1 =G3;while(G1/16!=32);printf(nFile name transmit finished.n);void InitPort(port,para) unsigned char para; union REGS reg;reg.x.dx=p

9、ort;reg.h.ah=O;reg.h.al=para;int86(0x14,?,?);)void SendPort(port5c)int port;char c;union REGS reg;reg.x.dx=port;reg.h.al=c;reg.h.ah=1;int86(0x14,?,?);if(reg.h.ah&128)printf(nSend mistakes!);exit(1);)int ReadPort(port)int port;union REGS reg; while(!(CheckState(port)&256)if(kbhit()/*如端口长期主数据可人为终止等律*/ printfCPress any key to exit.);getch();exit(1);)reg.x.dx=port;reg.h.ah=2;if(reg.h.ah&128)print

温馨提示

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

评论

0/150

提交评论