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

上传人:鲁** 文档编号:488297004 上传时间:2024-01-27 格式:DOCX 页数:10 大小:22.72KB
返回 下载 相关 举报
面就是用C语言编写控制串行口的程序_第1页
第1页 / 共10页
面就是用C语言编写控制串行口的程序_第2页
第2页 / 共10页
面就是用C语言编写控制串行口的程序_第3页
第3页 / 共10页
面就是用C语言编写控制串行口的程序_第4页
第4页 / 共10页
面就是用C语言编写控制串行口的程序_第5页
第5页 / 共10页
点击查看更多>>
资源描述

《面就是用C语言编写控制串行口的程序》由会员分享,可在线阅读,更多相关《面就是用C语言编写控制串行口的程序(10页珍藏版)》请在金锄头文库上搜索。

1、面就是用C语言编写控制串行口的程序。之欧侯瑞 魂创作创作时间:贰零贰壹年柒月贰叁拾日#includedos.h#includestdlib.h#includestdio.h#define PORT 0voidSendFile(char*fname); /* 发送文件 */voidSend(int s);/* 发送一个字节 */void SendFileName(char *fname); /* 发送文件名 */void ReceiveFile(); /* 接收文件 */void GetFileName(char *f);/* 接收文件名 */void InitPort(int port,uns

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

3、if(*argv1=S)/* 检查选择的有效性 */SendFile(argv2);else if(*argv1=R)ReceiveFile();elseprintf(Error parament.Please input again.);exit(1);void SendFile(char *fname)FILE *fp;int ch,s;if(fp=fopen(fname,rb)=NULL)printf(Cantopen the file.n);exit(1);SendFileName(fname);doch=(int)getc(fp);if(ferror(fp)printf(Error

4、reading file.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);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!=G);vo

5、id SendFileName(fname)char *fname;wait.);int s,ch;printf(Now transmit the file.Pleasewhile(*fname)ch=(int)fname+;s=ch%16; /* 取文件名中一个字节的低4 位*/Send(s);s=ch/16;Send(s); /* 取文件名中一个字节的低4 位*/s=32;/* 发送文件名结束标记*/Send(s);Send(s);void ReceiveFile()FILE *fp;char ch;int G1,G2,G3;char fname15;GetFileName(fname);

6、printf(Receiving file %s.n,fname);remove(fname);if(fp=fopen(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

7、)printf(nError writing file.);exit(1);G2=Receive(PORT,&G3);G1=G3;while(G1/16!=48);printf(nTransmit finished.);fclose(fp);int Receive(port,G)int port,*G;int GM;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);return GM

8、;void GetFileName(char *f)int G1,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(PORT,&G3);G1=G3;finished.n);while(G1/16!=32);printf(File name transmitvoid InitPort(port,para)int port;unsigned char para;union REGS reg;reg.x.dx=port

9、;reg.h.ah=0;reg.h.al=para;int86(0x14,?,?);void SendPort(port,c)int port;char c;union REGS reg;reg.x.dx=port;reg.h.al=c;reg.h.ah=1;int8 6(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()/* 如端口长期无数据可人为终止等待*/printf(Press any key to exit.);getch();exit(1);reg.x.dx=port;reg.h.ah=2;int86(0x14,?,?);if(reg.h.ah&128)printf(nRead mistake!);exit(1);return reg.h.al;int CheckState(port)int port;union REGS reg;reg.x.dx=port;reg.h.ah=3;int86(0x14,?,?);return reg.x.ax;创作时间:贰零贰壹年柒月贰叁拾日

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 商业/管理/HR > 营销创新

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号