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

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

面就是用 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(); /* 接收文件 */ void GetFileName(char *f); /* 接收文件名 */ void InitPort(int port,unsigned 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); if(*argv[1]==S) /* 检查选择的有效性 */ SendFile(argv[2]); else if(*argv[1]==R) ReceiveFile(); else{ printf(Error parament.Please input again.); exit(1); } } void SendFile(char *fname) { FILE *fp; int ch,s; if((fp=fopen(fname,rb))==NULL) { printf(Cant open the file.\n); exit(1); } SendFileName(fname); do{ ch=(int)getc(fp); if(ferror(fp)){ printf(Error 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; do{ SendPort(PORT,s); G=ReadPort(PORT);/* 等待握手信号 */ }while(s!=G); } void SendFileName(fname) char *fname; { int s,ch; printf(Now transmit the file.Please wait...); while(*fname){ ch=(int)fname++; s=ch%16; /* 取文件名中一个字节的低 4 位 */ Send(s); s=ch/16; Send(s); /*取文件名中一个字节的低 4 位 */ } s=32; /* 发送文件名结束标志 */ Send(s); Send(s); } void ReceiveFi

文档评论(0)

1亿VIP精品文档

相关文档