⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 result.c

📁 利用两台计算机
💻 C
字号:
#include "stdio.h"
#include "dos.h"
#include "fcntl.h"
#include "io.h"
#include "conio.h"



float f,g;                      /*用于保存文件大小*/
int com=0,bit=0;                /*全局变量,com口相关数据*/
int flag=1,handle,i=0,flag1=1;
unsigned char p,q;
long a[3];                      /*数组,用于传送文件大小*/
long k,count=0;
FILE *fp;                       /*文件指针*/
char filename[20];              /*文件名*/
void interrupt (*oldvect)();    /*声明一中断服务程序*/

void SendEmpty()                /*发送保持寄存器为空*/
{
int state;
do
  {
  state=inportb(com+5);
  state=state&0x40;
  }while(!state);
}

void RecEmpty()                 /*接收数据寄存器就绪*/
{
int state;
do
  {
  state=inportb(com+5);
  state=state&0x01;
  }while(!state);
}



void Initcom()            /*初始化com口,即8250*/
{
outportb(com+3,0x80);     /*使通信线路寄存器最高位置1*/
outportb(com,0x01);       /*置波特率115200*/
outportb(com+1,0x00);
outportb(com+3,0x03);     /*帧格式:无奇偶校验,停止位1,字符长度8*/
outportb(com+4,0x0b);     /*MODEM有效*/
}

void Open()               /*打开com口*/
{
Initcom();
oldvect=getvect(bit);     /*取得中断向量入口*/
disable();                /*禁止所有中断*/
if(com==0x3f8)            /*开放IRQ4或IRQ3*/
   outportb(0x21,inportb(0x21)&0xef);
else 
   outportb(0x21,inportb(0x21)&0xf7);
enable();                 /*允许中断*/
}

void Setting(void interrupt program()) /*设置program的中断入口地址为bit*/
{
disable();
setvect(bit,program);
enable();
}

void Sendchar()                  /*发送字符*/
{
q=getchar();
while(q!='\n')                   /*输入字符结束符\n*/
  {
   while(!(inportb(com+5)&0x20));/*发送数据寄存器为空*/
   outportb(com,q);              /*字符送去发送保持寄存器*/
   count++;                      /*计算发送字符的个数*/
   q=getchar();
  }
}

void interrupt Receivechar()     /*接收字符*/
{
while(!(inportb(com+5)&0x01));   /*接收数据寄存器就绪*/
q=inportb(com);                  /*从接收寄存器中取出字符*/
printf("%c",q);
count++;
outportb(0x20,0x20);             /*恢复8259服务寄存器,使8259能响应下一个中断*/
} 

void Sendfile()                  /*发送文件*/
{                                /*将发送文件大小存放在数组中*/
a[0]=k>>24;
a[1]=k<<8;
a[1]=a[1]>>24;
a[2]=k<<16;
a[2]=a[2]>>24;
a[3]=k<<24;
a[3]=a[3]>>24;
for(i=0;i<4;i++)                  /*将文件大小发送到另一个com口*/
   {
   SendEmpty();
   outportb(com,a[i]);
   }
for(RecEmpty();inportb(com)!='#';RecEmpty());  /*信息回馈*/
while(count<=k)
   {
   SendEmpty();
   q=fgetc(fp);
   if((inportb(com+5)&0x1e)==0)     /*发送的数据无错误*/
      outportb(com,q);              /*将读出的字符送去发送保持寄存器*/
   else
      outportb(com,'?');            /*发送的数据出现错误*/
   count++;
   printf("\r%.2f KB have been sent.",(count/1024.0));
   }
}


void interrupt Receivefile()      /*接收文件*/
{
if(flag1==1)
   {
   for(i=0;i<=3;i++)                 /*接收发送文件大小*/
      {
      RecEmpty();
      a[i]=inportb(com);         /*放于数组中*/
      }
   a[0]=a[0]<<24;
   a[1]=a[1]<<16;
   a[2]=a[2]<<8;
   k=a[0]|a[1]|a[2]|a[3];
   printf("Please input a file name to receive: ");
   scanf("%s",filename);                    /*目的文件名*/
   if((fp=fopen(filename,"wb"))==NULL)      /*打开文件失败*/
      {
      printf("Can not open the file.\n");
      }
   SendEmpty();
   outportb(com,'#');                       /*发送回馈信息*/
   flag1=0;
   }
else
   {
   q=inportb(com);
   if((inportb(com+5)&0x1e)==0)       /*数据无错误*/
      fputc(q,fp);                    /*将数据写入目标文件*/
   else
      fputc('*',fp);
   count++;
   printf("\r%.2f KB have been received.",(count/1024.0));
   }
if(count==k)
   {
   flag=0;
   }
outportb(0x20,0x20);  
}

 
void Close()                          /*关闭com口,退出程序*/
{
disable();
outportb(com+1,0x00);                 /*屏蔽8250内部的所有中断*/
outportb(com+4,0x00);                 /*使modem寄存器无效*/
outportb(com,0x00);
if(com==0x38f)
   {
   outportb(0x21,inport(0x21)|0x10);  /*中断屏蔽寄存器关闭com1口中断*/
   outportb(0x20,0x20);
   setvect(0x0c,oldvect);
   }
else 
   {
   outportb(0x21,inport(0x21)|0x08);  /*中断屏蔽寄存器关闭com2口中断*/
   outportb(0x20,0x20);
   setvect(0x0b,oldvect);
   }
enable();
exit(0);
}

void Surface()
{
int y;
count=0;           /*恢复计数值*/
i=0;
flag=1;
flag1=1;
clrscr();          /*清除当前字符窗口所有字符*/
textcolor(11);
cprintf("    Class Six of Computer Science and Technology    3105007047    xiongjiale\r");
cprintf("\n\n\n\n              Welcome to use Two aircraft communications!\r");
cprintf("\n\n          =============================================================\r\n");
cprintf("             *    1.send character   *    2.receive character    *\r\n");
cprintf("            ***   3.send file       ***   4.receive file        ***\r\n");
cprintf("             |    5.exit             *                           |\r\n");
cprintf("          =============================================================\r\n");
cprintf("\nChose the operation you want: ");
scanf("%d",&y);
getchar();
switch(y)
{
case 1:printf("Please input the characters: ");
       Sendchar();                       /*用查询方式发送字符*/
       printf("Have send %d characters.",count);
       break;
case 2:Setting(Receivechar);             /*用中断接收字符*/
       outportb(com+1,0x01);             /*打开允许接收器数据就绪中断*/
       outportb(com+1,0);                /*关闭允许接收器数据就绪中断*/
       printf("\nHave receive %d characters.",count);
       break;
case 3:printf("Please input the file name you want to send: ");
       scanf("%s",filename);             /*输入要发送的文件名*/
       handle=open(filename,O_RDONLY);
       k=filelength(handle);             /*算出要发送的文件的大小,以字节为单位*/
       g=k/1024.0;
       printf("\nThe size of the file:%.2f KB.\n",g);
       if((fp=fopen(filename,"rb"))==NULL)   /*打开文件失败*/
          {
          printf("Can not open the file.\n");
          break;
          }
       else
          Sendfile();
       fclose(fp);                           /*关闭文件*/
       break;
case 4:Setting(Receivefile);                 /*用中断接收文件*/
       outportb(com+1,0x01);
       while(flag==1);
       printf("\n\t\t\tThe End.........\n");
       fclose(fp);
       outportb(com+1,0);
       break;
case 5:Close();                              /*关闭com口,退出程序*/
       break;
}
}


void main()         /*主函数*/
{
int x;
textcolor(13);
cprintf("    Class Six of Computer Science and Technology    3105007047    xiongjiale\r");
cprintf("\n\n\n\n             Computer interface and communication Curriculum Design\r\n");
cprintf("                       ------Two aircraft communications\r\n");
cprintf("\n\n          =============================================================\r\n");
cprintf("                    #*#  Chose the 'com' you want to open   #*#\r\n");
cprintf("                    #*#                                     #*#                      \r");
cprintf("                    #*#    1.open com 1      2.open com 2   #*#\r\n");
cprintf("                    #*#    3.exit                           #*#\r\n");
cprintf("          =============================================================\r\n");
scanf("%d",&x);
while(1)
{
switch(x)
{
case 1:com=0x3f8;         /*为全局变量赋值*/
       bit=0x0c;
       Open();            /*初始化com1*/
       Surface();         /*进入com1的操作*/
       break;
case 2:com=0x2f8;         /*为全局变量赋值*/
       bit=0x0b;
       Open();            /*初始化com2*/
       Surface();         /*进入com2的操作*/
       break;
case 3:exit(0);           /*退出程序*/
}
printf("\n\n\n\n\t\t\tPress any key to return......");
getch();
}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -