📄 lyb.c
字号:
while(!state)
{
state=inportb(0x2FD); /*循环检测,当state&0x20为1时表示可以发送数据*/
state=state&0x20;
}
}
void close2_intr()/*关闭中断*/
{
unsigned char i;
disable(); /*关闭中断*/
outportb(0x2F9,0x00); /*恢复原来状态,不允许接收中断*/
outportb(0x2FC,0x03); /*设置MODEM控制寄存器,恢复原来状态*/
i=inportb(0x21);
i=i|0x10; /*8259复位原先的IR4的状态*/
outportb(0x21,i);
enable(); /*开中断*/
setvect(0x0c,old_int); /*恢复原来的中断向量*/
}
void receive2_w() /*接收等待,检测接受寄存器*/
{
state=inportb(0x2FD); /*2FD为线路状态寄存器*/
state=state&0x01; /*判断接受数据是否准备好*/
while(!state)
{
state=inportb(0x2FD);
state=state&0x01;
}
}
void send2_ch() /*发送字符,字符串*/
{
unsigned char ch;
printf("\1 Please input the strings you want to send:");
K:
send2_w(); /*发送之前查询发送寄存器是否为空*/
ch=getch();
printf("%c",ch);
if(ch!=13) /*输入字符不是回车时继续发送*/
{
outportb(0x2F8,ch); /*发送字符到接收寄存器*/
goto K;
}
else printf("\n\n\t\tSend have completed!\n");
}
void send2_file()
{
FILE *fp;
char buf[SIZE]; /*缓存*/
char ch;
long len1[4]; /*用来存放文件大小的4个字节*/
long count;
char filename[20]; /*文件名*/
long file_len; /*文件大小*/
long size;/*还没有发送的文件长度*/
int i=0;
long file=0;
double time,time1; /*用于计算发送时间*/
double time2;
double speed; /*用于计算发送速度*/
double baifen;
float file1;
printf("\nPlease input the name of the file that you want to send:");
scanf("%s",filename);
if((fp=fopen(filename,"rb+"))==NULL)/*以二进制方式打开文件*/
{
printf("\t\tCann't open the file! \n");
return;
}
file_len=file_size(fp); /*获得文件长度*/
send2_w(); /*发送等待*/
printf("\t\tfile_len= %3.2f kb\n",file_len*1.0/1024);
outportb(0x2F8,'@'); /*发送开始字符,发送文件开始的握手信号*/
/* 首先 发送文件长度*/
len1[0]=file_len>>24;
len1[1]=file_len<<8; /*通过移位把32位的数据截成4个8位数据,发送文件长度大小给接收方*/
len1[1]=len1[1]>>24;
len1[2]=file_len<<16;
len1[2]=len1[2]>>24;
len1[3]=file_len<<24;
len1[3]=len1[3]>>24;
send2_w(); /* 发送等待*/
for( i=0;i<4;i++)
{
send2_w();
outportb(0x2F8,len1[i]); /*发送文件的大小*/
}
/* 发送文件长度结束 */
receive2_w(); /*接收等待*/
state=inportb(0x2F8);
if(state!='y') /*如果接方创建文件失败,则返回一个信息给发送方*/
{
printf("\t\tCreat file fail,please try again!\n");
return;
}
else
{
printf("\t\tthe Receiver has created the sending file successfully !\n");
printf("\t\tbeginning to send the file....\n");
}
i=0;
size = file_len;
time=(double)clock(); /*计算开始发送的时间*/
while(size>SIZE) /* begin 发送文件,每发16k字节发一个字母当校验用*/
{
fread(buf,SIZE,1,fp); /*把文件的16k字节读到缓存*/
for(i=0;i<SIZE;i++)
{
send2_w();
outportb(0x2F8,buf[i]); /*发送文件*/
}
size-=SIZE; /*文件大小减少16k*/
printf("size=%d",size);
send2_w();
outportb(0x2F8,'?'); /*发送一个标志'?'用于检查接收方是否已经接收完16k字节*/
receive2_w(); /*接收等待*/
ch=inportb(0x2F8);
if(ch!='!') /*如果对方发送回来提示效检字节错,则重发16k字节*/
{
fseek(fp,SIZE,SEEK_CUR);/*文件指针前移16k字节*/
size+=SIZE;
}
printf("%c\n",ch);
file=file+SIZE;
clrscr();
printf("\t\tYou have already sent %3.2f(kb),and left %3.2f(kb)\n",file * 1.0 / 1024,size * 1.0 / 1024);
}
if(size>0)
{
fread(buf,size,1,fp);
for(i=0;i<size;i++)
{
send2_w();
outportb(0x2F8,buf[i]); /*把剩余的不到16k的字节发送完*/
}
}
file=file+size;
size-=size;
time1=(double)clock(); /*得到发送结束后的时间*/
time2=(time1-time)/18.2; /*计算总共用去的时间*/
/*speed=file/time2; 计算传输速度*/
file1=(float)(file/(1024));
speed=file1/time2; /*计算传输速度*/
printf("\n\t\tFile had sent already! the size of the file are %3.2fK\n",file1);
printf("\n\t\tThe sending speed is :%3.2f(kb/s)",speed);
fclose(fp);/*关闭文件*/
return;
}
void receive2_ch() /*接收字符*/
{
unsigned char ch;
printf("\t\t\nThe strings that receive are:");
do{
do{
state=inportb(0x2Fd)&0x01; /*判断接收数据是否准备好*/
}while(state!=0x01);
ch=inportb(0x2F8); /*接收数据*/
printf("%c",ch);
}while(ch!='\n');
printf("\n\t\tReceive strings complete! \n");
}
void receive2_file()/*接收文件*/
{
unsigned char i;
int j=0;
FILE *fp;
char buf[SIZE]; /*缓存*/
char ch;
char filepath[20]; /*文件的存放路径*/
long file=0;
long size;
long len2[4];/*用来接收发送方传过来的文件大小*/
float file1;
printf("\n\t\tReceive the size of the file !\n");
for(j=0;j<4;j++)
{
receive2_w();
len2[j]=inportb(0x2F8); /*接收文件的大小*/
}
len2[0]= len2[0]<<24;
len2[1]= len2[1]<<16;
len2[2]= len2[2]<<8;
size= len2[0]|len2[1]|len2[2]|len2[3]; /*计算文件的大小*/
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(0x2F8,'n'); /*如果创建文件失败就发送一个字符'n'给发送方*/
return;
}
else
{
send2_w();
outportb(0x2F8,'y'); /*如果创建文件成功就发送一个字符'y'给发送方*/
}
printf("\n\t\tBegin to receive the file \n");
while(size>SIZE) /*循环接收16k字节*/
{
for(j=0;j<SIZE;j++)
{
receive2_w();
buf[j]=inportb(0x2F8); /*接收文件数据*/
}
receive2_w();
/*i = inportb(0x2F8); */
/*printf("i=%c",i); */
if(inportb(0x2F8)=='?') /*当接收到一个标志'?'时,表示已经接收完16k字节*/
{
fwrite(buf,j,1,fp); /*把接收的数据写到文件中*/
size-=SIZE;
send2_w(); /*发送等待*/
outportb(0x2F8,'!'); /*发送一个标志'!'用于答复发送方已经把16k数据写入了文件中*/
}
else
{
send2_w();
outportb(0x2F8,0x00); /*当没有接收到一个标志'?'时,表示需要重发刚才的16k字节*/
}
file=file+SIZE;
clrscr();
printf("\t\tYou have already received %3.2f(kb),and left %3.2f(kb)\n",file * 1.0/1024,size * 1.0 /1024);
}
if(size>0)
{
for(j=0;j<size;j++)
{
receive2_w();
buf[j]=inportb(0x2F8); /*接收完剩下的不到16k的字节*/
}
fwrite(buf,j,1,fp); /*写到文件中*/
}
file=file+size;
size-=size;
file1=(float)(file/(1024));
printf("\n\t\tYou have already received %3.2f(kb),and left %3.2f(kb)\n",file *1.0/1024,size *1.0/1024);
printf("\n\n\t\tReceive finish! the total size you have received is %3.2f Kb\n",file1);
fclose(fp);
return;
}
void interrupt far receive2_intr(void) /*中断服务程序用于接收文件*/
{
receive2_w(); /*接收等待*/
state=inportb(0x2F8);
/*printf("%c",state);*/
if(state=='@') /*当接收到发送文件开始的握手信号时,开始接收*/
{
flag=0;
receive2_file();
}
else printf("\n\t\t Error!\n");
/*int flag=0; */
close2_intr(); /*关闭中断*/
printf("\n");
outportb(0x20,0x20);
}
void file2_begin()
{
init_COM2_INT();
old_int = getvect(0x0b); /*获8259 RQ4中断类型*/
disable(); /*初始化中断系统时,应该关闭中断,初始化完成后再开放*/
state=inportb(0x21); /*0021H为8259口地址2,用来写ICW2、ICW4*/
state=state&0xe7; /*开8259 RQ4中断11101111*/
outportb(0x21,state); /*写OWC1使中断RQ4开放*/
setvect(0x0b,receive2_intr); /*置中断向量类型asyncint*/
enable();
while(flag){ } /*初始化完成后再开放中断*/
}
void main()
{
char c,d;
drawwindow();
printf("\n");
printf("please input the Baud Rate(input'0C'when the Baud Rate is 9600,or other): ");
scanf("%x",&PTL);
printf("\n");
c=getchar();
printf("Choose the COM :1--->com1;2--->com2 ");
scanf("%d",&r);
printf("\n");
printf("\n******************************************************\n");
printf("$$$$$$$$$$ Communication between two computers $$$$$$$$$\n");
printf("\n************* laiyangbo 3105007077 ******************\n");
printf("\n**** 1 Send strings ****\n");
printf("\n**** 2 Receive strings ****\n");
printf("\n**** 3 Send file ****\n");
printf("\n**** 4 Receive file ****\n");
printf("\n**** 5 Exit ****\n");
printf("\n******************************************************\n");
printf("\n Please input your choose : ");
c=getchar();
c=getchar();
d=getchar();
d=d+1;
if(r==1)
{
switch(c)
{
case '1':
clrscr();
printf("\t\t\t\t 1.send strings\n\n");
init_COM1_C();
send1_ch();
break;
case '2':
clrscr();
printf("\t\t\t\t 2.receive strings\n\n");
init_COM1_C();
receive1_ch();
break;
case '3':
clrscr();
printf("\t\t\t\t 3.send file \n\n");
init_COM1_C();
send1_file();
break;
case '4':
clrscr();
printf("\t\t\t\t 4.receive file \n");
init_COM1_INT();
file1_begin();
break;
case '5':
break;
}
getch();
}
if(r==2)
{
switch(c)
{
case '1':
clrscr();
printf("\t\t\t\t 1.send strings\n\n");
init_COM2_C();
send2_ch();
break;
case '2':
clrscr();
printf("\t\t\t\t 2.receive strings\n\n");
init_COM2_C();
receive2_ch();
break;
case '3':
clrscr();
printf("\t\t\t\t 3.send file\n\n");
init_COM2_C();
send2_file();
break;
case '4':
clrscr();
printf("\t\t\t\t 4.receive file\n");
init_COM2_INT();
file2_begin();
break;
}
getch();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -