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

📄 源程序.c

📁 接口与通信课程的课程设计
💻 C
字号:
#include "Stdio.h"
#include "Conio.h"
#include "Dos.h"
#include "Stdlib.h"
#include "String.h"
#include "Time.h"

unsigned char state;
char ch,string;
FILE *fp;

void Send_km() /*发送字符函数*/
{
  char send,choose;
loop: clrscr();
  printf("\n\n");
  printf("     *******************  Welcome!  *******************\n\n");
  printf("     Please end the characters with 'Esc'!\n\n");
  printf("     Please input the characters you want to send :\n\n");
  printf("     ");
  while(1)
  {
    state=inportb(0x3fd); /*取得发送状态寄存器状态*/
    if(kbhit)             /*有键按下*/
      send=getch();
    if(send=='\r')        /*遇到回车符就换行显示*/
      send='\n';
    putchar(send);
    if((state&0x20)!=0)   /*测试发送保持寄存器是否为空*/
    {
      if(send=='\x1B')    /*结束符号Esc*/
      {
        outportb(0x3f8,send);   /*向发送保持寄存器写一字节数据*/
        printf("\n\n     Do you see the Characters on the other computer? (y/n)\n");
        /*scanf("%c",&choose);*/
        choose=getch();
        if(choose=='y')
          return;
        if(choose=='n')
          goto  loop;
      }
      outportb(0x3f8,send);
    }
  }
  /*getch();*/
}

void Receive_km()   /* 接 收 字符函数*/
{
  char receive,choose;
loop:  clrscr();
  printf("\n\n");
  printf("     *******************  Welcome!  *******************\n\n");
  printf("     The received Characters are:\n\n");
  printf("      ");
  while(1)
  {
    state=inportb(0x3fd); /*取得 接 收  状态寄存器状态*/
    if((state&0x01)!=0)   /*测试是否接 收到数据*/
    {
      receive=inportb(0x3f8);   /*读取接 收  状态寄存器的数据*/
      if(receive=='\x1B')       /*结束符号Esc*/
      {
        printf("\n\n   End receive characters!");
        printf("\n\n   Do you see the characters? (y/n)\n");
        choose=getch();
        if(choose=='y')
          return;
        if(choose=='n')
          goto  loop;
      }
      putchar(receive);
    }
  }
  /*getch();*/
}

void Send_File()    /*发送文件函数*/
{
  int i=0;
  char fname[20];
  clrscr();
  printf("\n\n");
  printf("     *******************  Welcome!  *******************\n\n");
  printf("     Please Input the file name then Press 'Enter':\n\n");
  printf("     ");
  scanf("%s",fname);
  if((fp=fopen(fname,"rb"))==NULL)  /*出错判断:无法读取文件*/
  {
    printf("\n\n     Can't open the file,Please press any key to exit!\n");
    printf("      ");
    getch();
    return;
  }
  while(fname[i]!='\0')    /*发送文件名*/
  {
    state=inportb(0x3fd); /*取得发送状态寄存器状态*/
    if((state&0x20)!=0)   /*测试发送保持寄存器是否为空*/
    {
      outportb(0x3f8,fname[i]);
      i++;
    }
  }
  outportb(0x3f8,'#');   /*文件名发送结束符号*/
  do
  {
    state=inportb(0x3fd)&0x01;
  }while(state==0);
  string=inportb(0x3f8);
  if(string!='#')
    printf("\n\n   #ERROR! the file you sent name is wrong!\n");
  printf("\n\n   ......Sending......\n");
  ch=fgetc(fp);   /*取字符*/
  while(ch!=EOF)  /*若不是文件尾则继续*/
  {
    do
    {
      state=inportb(0x3fd)&0x20;
    }while(state==0);
    outportb(0x3f8,ch);
    ch=fgetc(fp);
  }
  outportb(0x3f8,'\x1B');  /*文件发送完毕符号*/
  fclose(fp);     /*关闭文件*/
  printf("\n   Send file finish!...Press any key to exit!\n\n");
  getch();
  return;
}

Receive_File()      /*接收文件函数*/
{
  int j=0;
  char  fname[20];
  clrscr();
  printf("\n\n");
  printf("     *******************  Welcome!  *******************\n\n");
  printf("     The file name received is: \n\n");
  printf("      ");
  do
  {
    state=inportb(0x3fd)&0x01;
  }while(state==0);
  string=inportb(0x3f8);
  while(string!='#')       /*接受文件名*/
  {

    printf("%c",string);
    fname[j]=string;
    j++;
    do
    {
      state=inportb(0x3fd)&0x01;
    }while(state==0);
    string=inportb(0x3f8);
  }
  fname[j]='\0';
  remove(fname);    /*替换相同文件名文件*/
  fp=fopen(fname,"wb");
  if(fp==NULL)       /*文件建立失败*/
  {
    printf("\n\n   Can't save the file,Please Press any key to exit!\n");
    printf("      ");
    getch();
    return;
  }
  outportb(0x3f8,'#');
  printf("\n\n\n   ......Receiving......\n");
  do
  {
    state=inportb(0x3fd)&0x01;
  }while(state==0);
  ch=inportb(0x3f8);
  while(ch!='\x1B')
  {
    fputc(ch,fp);
    do
    {
      state=inportb(0x3fd)&0x01;
    }while(state==0);
    ch=inportb(0x3f8);
  }
  fclose(fp);     /*关闭文件*/
  printf("\n   Receive file finish!...Press any key to exit!\n\n");
  getch();
  return;
}

void Initial() /*8250初始化*/
{
  outportb(0x3fb,0x80); /*将线路控制寄存器的DLAB位设为1,设置分频值*/
  outportb(0x3f8,0x0c); /*将波特率设为9600 */
  outportb(0x3f9,0x00);
  outportb(0x3fb,0x0f); /*奇校验,2位停止位,8位数据位*/
  outportb(0x3f9,0x0f); /*允许所有中断*/
  outportb(0x3fc,0x0f); /*设置零MODEN衡有效状态*/
}

void Mainface()  /*菜单*/
{
  clrscr();
  printf("\n\n");
  printf("     *******************  Welcome!  *******************\n\n");
  printf("   * Operation\n\n");
  printf("   1) Send the keyboard characters     ,Please press '1': \n\n");
  printf("   2) Receive the keyboard characters  ,Please press '2': \n\n");
  printf("   3) Send the file                    ,Please press '3': \n\n");
  printf("   4) Receive the file                 ,Please press '4': \n\n");
  printf("   5} Exit                             ,Please press '5': \n\n");
  printf("   ======================================================\n\n");
  printf("   Please choose the operation:  ");
}

int main(void)
{
  int operate;
  Initial();
  while(1)
  {
    Mainface();
    scanf("%d",&operate);
    switch(operate)
    {
      case 1: Send_km();break;         /*传送字符*/
      case 2: Receive_km();break;      /*接收字符*/
      case 3: Send_File();break;       /*传送文件*/
      case 4: Receive_File();break;    /*接收文件*/
      case 5: exit(0);break;
      default: break;
    }
    /*break;*/
  }
  getch();
  return 0;
}

⌨️ 快捷键说明

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