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

📄 最终设计.c

📁 一、 设计目的及所需设备: 了解PC机串行口的使用;所需设备为:PC机
💻 C
字号:
#include<stdio.h>
#include<dos.h>
#include<process.h>
#include<conio.h>
#include<bios.h>
#include<ctype.h>
#include<stdlib.h>
#include<fcntl.h>
#include<time.h>

#define BUFFMAX 100

void Init(void);
void enableports(void);
void disableports(void);
void interrupt far receive(void);
void (interrupt *receive_mode)();


char Buffin[BUFFMAX];
char Buffout[BUFFMAX];
int start=0,fstart=0,comp=0,in=0,count=0;
int flag=0,key=0,ckey=0,fkey=0,end=0;
struct time t1,t2;
FILE *fin,*fout;

void enableports(void)
{
	unsigned char B;
	receive_mode=getvect(0x0c);
	disable();
	outportb(0x3fc,0x0b);
	outportb(0x3f9,0x01);
	B=inportb(0x21)&0xef;
	outportb(0x21,B);
	setvect(0x0c,receive);
	enable();
}

void interrupt far receive(void)
{
    char ch;
	ch = inportb(0x3f8);
	if(ckey==2){
		if(ch!='!'){Buffin[in++]=ch;}
		else {flag=2;ckey=0;start=0;}
	}
	if(fkey==2){
    	/*putchar(ch);*/
	    Buffin[in++] = ch;
    	if(in==Buffin[0]+1){
		   in = 0;
	    	fwrite(&Buffin[1],1,(int)Buffin[0],fin);
		    if((int)Buffin[0]<BUFFMAX-1){
			flag = 1;
			fkey=0;
			start=0;
			fclose(fin);
			}
		}
	}
	if(start==0){
	     if(ch=='#')ckey=1;
	     if(ch=='$')fkey=1;
		 start=1;
	}
	outportb(0x20,0x20);
}




void disableports(void)
{
	disable();
	outportb(0x3f9,0x00);
    outportb(0x3fc,0x00);
	outportb(0x21,inportb(0x21)&0x10);
	enable();
	setvect(0x0c,receive_mode);
	
}

void Init(void)
{
	outportb(0x3fb,0x80);
	outportb(0x3f8,0x01);
	outportb(0x3f9,0);
	outportb(0x3fb,0x07);
	outportb(0x3fc,0x0b);

}


void main()
{
	 char chr,choi;  
	int choise,i=1,length,delay=1;
	char lifename[20];
	do  {
	printf("*******************************************************\n");
	printf("          select the function for the com1:\n");
	printf("          com for receive:  Enter 1 \n");
	printf("          com for send:     Enter 2  \n");
	printf("          quit         :    Enter any other key!\n");
	printf("*******************************************************\n");
	scanf("%d",&choise);
	if(choise==1){
		Init();
		enableports();
		while(!kbhit()){
			if(fkey==1){
				printf("\n Do you want to save?\n");
				fkey=2;
				}
			if(ckey==1){
				ckey=2;
				}
			if(flag==2){
			        	printf("The string received is: \n ");
			         	for(i=0;i<in;i++)
			         		printf("%c",Buffin[i]);
			         		in=0;flag=0;
			}
		}
		chr=getch();
		if(chr=='y'||chr=='Y'){
				  printf("\nEnter name for the file you want to save!\n");
				  scanf("%s",lifename);
		          if((fin=fopen(lifename,"wb"))==NULL)printf("open error!");
				  outportb(0x3f8,'y');
				  printf("Now is conmunicating...\n");			
        	while(!kbhit()){
			
		        	if(flag==1){printf("\n File has received!\n");flag=0;}
		            
		
			}
	   	getch();
		}
	   printf("\n***********************************************\n");
	    printf(" Enter 'c' to continue conmunicate;\n");
		printf(" Enter 'q' to quit!;\n");
		printf("***********************************************\n"); 
		disableports();
		chr=getchar();
		while(chr!='q'&&chr!='c'){printf("Enter 'q' or 'c' !\n");chr=getchar();}
		if(chr=='c')clrscr();
		
	}
	if(choise==2){
		Init();
		clrscr();
		printf("==========================\n");
		printf("   c: to send charactar;\n");
		printf("   f: to send file;\n");
        printf("==========================\n");
		choi=getch();
		printf("%c",choi);
		if(choi=='c'||choi=='f'){
	    	if(choi=='c'){
			outportb(0x3f8,'#');
			   chr=getch();
		       while(chr!='e'){
			       if(inportb(0x3fd)&0x20){
		        	    outportb(0x3f8,chr);
				    	printf("%c",chr);
					    count++;
					   chr=getch();
				   }			 
			   }
			   outport(0x3f8,'!');
		        printf("\n %d bytes have been sended!\n",count);
			   count=0;
		        getch();
				
				
			}
		    if(choi=='f'){
				printf("\nEnter the name of the file you want to send!\n");
				scanf("%s",lifename);
					outportb(0x3f8,'$');
				while(!inportb(0x3f8)){}
				chr=inportb(0x3f8);
				if(chr=='y'){
				if((fout=fopen(lifename,"rb"))==NULL){
					printf("cannot open this file\n");
					exit(0);
				}
				printf("Now is conmunicating...\n");
				gettime(&t1);
				do{
             Buffout[0]=fread(&Buffout[1],1,99,fout);
				for(i=0;i<=Buffout[0];i++){
			            delay=1;
						while(delay==1){
					        if(inportb(0x3fd)&0x20){
							    outportb(0x3f8,Buffout[i]);
				    	        printf("%c",Buffout[i]);
					           	delay=0;		     
					  }
						}
				}
							
				}while(Buffout[0]==99);
				gettime(&t2);
				fclose(fout);
			    printf("\n  The file has been sended!\n");
				printf("    The time for transmiting is :  %d:\n",(t2.ti_min-t1.ti_min)*60+t2.ti_sec-t1.ti_sec);
				}
				else {printf("The computer you want to conmunicate refuses!\n");}
			}
                printf("***********************************************\n");
			    printf(" Enter 'c' to continue conmunicate;\n");
			    printf(" Enter 'q' to quit!;\n");
			    printf("***********************************************\n");
				chr=getchar();
				while(chr!='q'&&chr!='c'){printf("Enter 'q' or 'c' !\n");chr=getchar();}
				if(chr=='c')clrscr();
			   

		}
	}
	start=0,fstart=0,comp=0,in=0,count=0;
	flag=0,key=0,ckey=0,fkey=0,end=0;
	}while(chr=='c');
	
}

⌨️ 快捷键说明

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