?? result.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 + -