#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <conio.h>
#define BUFFLEN 1024*16 /*定义接收缓冲区大小*/
int port; /*定义端口*/
double bt=115200;
int st=1,dt=8,flag; /*bt,st,dt分别是当前的波特率,停止位,数据。*/
unsigned char state; /*端口状态*/
char string;
FILE *fp;/*文件指针*/
void interrupt (*old_int)(void);
void interrupt far receive_intr();/*中断服务程序,用于中断接收文件*/
Send_msg()/*发送字符函数*/
{
char send;
clrscr();
printf("\nSend Message: ( Press Esc to return)\n\n");
while(1)
{
state=inportb(port+5); /*取得发送状态寄存器状态*/
if(kbhit) /*有键按下*/
send=getch();
if(send=='\r') /*遇到回车符就换行显示*/
send='\n';
putchar(send);
if((state&0x20)!=0) /*如果发送保持寄存器为空*/
{
if(send=='\x1B') /*结束符号Esc*/
{
outportb(port,send); /*向发送保持寄存器写一字节数据*/
printf("\n\n\n * Press any key to return to mainmenu. *\n");
getch();
MainMenu();
}
outportb(port,send); /*不断查询发送*/
}
}
}
Receive_msg() /* 接收字符函数*/
{
char receive;
clrscr();
printf("\n\nReceiving Message:\n\n");
while(1)
{
state=inportb(port+5); /*取得接收状态寄存器状态*/
if((state&0x01)!=0) /*测试是否接收到数据*/
{
receive=inportb(port); /*读取接收状态寄存器的数据*/
if(receive=='\x1B') /*结束符号Esc*/
{
printf("\n\nEnd receive Message!");
printf("\n\n\n * Press any key to return to mainmenu. *\n");
getch();
MainMenu();
}
putchar(receive); /*不断显示接收字符*/
}
}
}
void send_w()/*发送等待,检测发送寄存器*/
{
do{
state=inportb(port+5)&0x20; /*查询LSR中THRE*/
}
while(!state);
}
void receive_w() /*接收等待,检测接受寄存器*/
{
do{
state=inportb(port+5)&0x01; /*查询LSR中DR*/
}
while(!state);
}
long file_size(FILE *fp)/*计算文件大小*/
{
long file_len;
fseek(fp,0,SEEK_END);/*在文件指针中定位*/
file_len=ftell(fp);
fseek(fp,0,SEEK_SET);
return file_len; /*返回文件大小*/
}
void send_file()
{
FILE *fp;
char buf[BUFFLEN],filename[20]; /*缓存*/
long part[4],file_len,size; /*用来存放文件大小的4个字节*/
int i=0;
printf("\nPlease input the name of the file that you want to send:");
scanf("%s",filename);
if((fp=fopen(filename,"rb+"))==NULL)/*以二进制方式打开文件*/
{
printf("\nCan not open the file! please try again \n");
printf("\npress anykey to go back mainmenu");
getch();
main();
}
file_len=file_size(fp); /*获得文件长度*/
printf("\n\t\tthe file you want to send is %3.2f(kb)\n",file_len*1.0/1024);
send_w(); /*发送等待*/
outportb(port,'@'); /*发送开始字符,发送文件开始的握手信号*/
part[3]=(int)(file_len/16777216); /*将文件大小的值存进part[]*/
part[2]=(int)(file_len%16777216/65536);
part[1]=(int)(file_len%65536/256);
part[0]=(int)(file_len%256);
send_w(); /* 发送等待*/
for( i=0;i<4;i++)
{
send_w();
outportb(port,part[i]); /*发送文件的大小*/
}
/* 发送文件长度结束 */
receive_w(); /*接收等待*/
state=inportb(port);
if(state!='y') /*如果接方创建文件失败,则返回一个信息给发送方*/
{
printf("\tthe receiver creat file fail,please try again!\n");
printf("\n\tpress anykey to go back mainmenu");
getch();
main();
}
else
{
printf("\t\tthe receiver has created the sending file successfully !\n");
printf("\t\tbeginning to send the file....\n");
}
size = file_len;
while(size>BUFFLEN) /* begin 发送文件,每发16k字节发一个字母当校验用*/
{
fread(buf,BUFFLEN,1,fp); /*把文件的16k字节读到缓存*/
for(i=0;i<BUFFLEN;i++)
{
send_w();
outportb(port,buf[i]); /*发送文件*/
}
size-=BUFFLEN; /*文件大小减少16k*/
}
if(size>0)
{
fread(buf,size,1,fp);
for(i=0;i<size;i++)
{
send_w();
outportb(port,buf[i]); /*把剩余的不到16k的字节发送完*/
}
}
printf("\n\t\tFile had sent already! the size of the file are %3.2fK\n",file_len*1.0/1024);
fclose(fp);/*关闭文件*/
printf("\npress anykey to go back mainmenu");
getch();
main();
}
void receive_file()/*接收文件*/
{
int j;
FILE *fp;
char buf[BUFFLEN],filepath[20];
long file=0,size,len[4];
printf("\n\t\tReceive the size of the file !\n");
for(j=0;j<4;j++)
{
receive_w();
len[j]=inportb(port); /*接收文件的大小*/
}
size=len[3]*16777216+len[2]*65536+len[1]*256+len[0]; /*计算文件的大小*/
printf("\t\tThe receiving file's size is:%3.2f kb\n",size * 1.0 / 1024);
printf("\n\n\tPlease input the saving path of the file:");
scanf("%s",filepath);
if((fp=fopen(filepath,"wb+"))==NULL)
{
printf("\n\t\tCann't open the file %s!",filepath);
outportb(port,'n'); /*如果创建文件失败就发送一个字符'n'给发送方*/
printf("\npress anykey to go back mainmenu");
getch();
main();
}
else
{
send_w();
outportb(port,'y'); /*如果创建文件成功就发送一个字符'y'给发送方*/
}
printf("\n\t\tBegin to receive the file \n");
while(size>BUFFLEN) /*循环接收16k字节*/
{
for(j=0;j<BUFFLEN;j++)
{
receive_w();
buf[j]=inportb(port); /*接收文件数据*/
}
fwrite(buf,j,1,fp); /*把接收的数据写到文件中*/
size-=BUFFLEN;
file=file+BUFFLEN;
clrscr();
printf("\n\t\tfile sending....... \n");
}
if(size>0)
{
for(j=0;j<size;j++)
{
receive_w();
buf[j]=inportb(port); /*接收完剩下的不到16k的字节*/
}
fwrite(buf,j,1,fp); /*写到文件中*/
}
file=file+size;
printf("\n\nReceive completely! the total size you have received is %3.2f Kb\n",file*1.0/1024);
fclose(fp);
printf("\npress anykey to go back mainmenu");
getch();
main();
}
void close_intr()/*关闭中断*/
{
disable(); /*关闭中断*/
outportb(port+1,0x00); /*恢复原来状态,不允许接收中断*/
outportb(port+4,0x03); /*设置MODEM控制寄存器,恢复原来状态*/
outportb(0x21,inportb(0x21)|0x18); /*8259复位原先的IR4的状态*/
enable();
if(port==0x3f8)
{
setvect(0x0c,old_int); /*恢复原来的中断向量*/
}
else setvect(0x0b,old_int);
}
void interrupt far receive_intr(void) /*中断服务程序用于接收文件*/
{
state=inportb(port);
if(state=='@') /*当接收到发送文件开始的握手信号时,开始接收*/
{
flag=0;
receive_file();
}
else printf("\n\t\t Error!\n");
printf("\n");
outportb(0x20,0x20); /*写EOI*/
}
void getfile()
{
if(port==0x3f8)
{
old_int = getvect(0x0c); /*获8259 IRQ4中断类型P236*/
}
else old_int = getvect(0x0b); /*获8259 IRQ3中断类型P236*/
disable(); /*初始化中断系统时,应该关闭中断,初
接口课程设计(C语言编写)
需积分: 9 157 浏览量
2009-01-04
18:44:07
上传
评论
收藏 401KB RAR 举报
nhz0629
- 粉丝: 0
- 资源: 1
最新资源
- 采用P-f和Q-V滞控的去中心化逆变器型交流微电网的模拟(Simulink仿真实现)
- 彩虹聚合二级域名DNS管理系统源码v1.3
- 【TOF相机笔记3】Simulink使用方法
- 算法部署-基于C++和Python使用ONNXRuntime部署RT-DETR目标检测算法-附项目源码-优质项目实战.zip
- Bitree.cpp
- 改变浏览器大小,图片(img)内容居中显示
- 全景分割-基于FAIR-DETR对Cityscapes数据集进行微调实现全景分割-附项目源码-优质项目实战.zip
- Tru master.m4a
- 基于ELMAN神经网络的用气量预测,基于ELMAN的天然气消费量预测(代码完整,数据齐全)
- 基于Vue3+ThreeJS实现机械臂控制和预览+源码+开发文档+代码解析(高分优秀项目)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈