📄 commss.c
字号:
#include"stdio.h"
#include"conio.h"
#include"dos.h"
#define SPACE 1024
/*--------------------三个必用函数--------------------*/
void initial_com1();
void open_com1();
void close_com1();
void error_file();
/*--------------------中断处理函数---------------------*/
void interrupt far asyncint();
void interrupt(*asyncoldvect)();
/*---------------------几个全局变量---------------------*/
unsigned int buffer[SPACE];
int buffin=0;
int buffout=0;
/*---------------------发送数据-----------------------*/
void my_send(int send_number)
{
outportb(0x3f8,send_number);
enable();
}
/*---------------------获得数据-------------------------*/
int my_receive(void)
{
int receive_number;
if(buffout==buffin)
return(-1);
else
{
receive_number=buffer[buffout];
buffout=buffout+1;
if(buffout>=SPACE)
buffout=0;
return receive_number;
}
}
/*-----------------------主程序--------------------------*/
void main()
{
int press_number;
int sends;
int if_back;
char con2;
int wrong_flag=1;
FILE *fp;
char ch,filename[10];
if_back=0;
printf("\n\nplease input the name of your receiving file[the format of the file is **.txt]\n\n ");
/*----------------------文件错误处理--------------------------*/
scanf("%s",filename);
if((fp=fopen(filename,"w"))==NULL)
{
if(!wrong_flag)
{
printf("can not open the file");
con2=getch();
error_file(con2,wrong_flag);
}
}
/*---------------------------打开断口---------------------------*/
open_com1();
fprintf(stdout,"\n\n1.Ready to Receive DATA\n");
printf("2.press any number key to send number\n");
printf("3.press [n] to quit\n\n\n");
printf("<---the data you receive have been saved in the file you have just created--->\n\n");
do
{
if(kbhit())
{
press_number=getch();
if(press_number=='n')
{
if_back=1;
break;
}
else
{
/*由于此处,我在FPGA中设计串口通信时,对于ASCII码正好多一,所以这里press_number均-1*/
if(press_number==0x30){clrscr();printf("\n\n");printf(" 1 2 3 4 5 6 7 8 9 [0]\n\n");printf(" press [n] to exit");}
else if(press_number==0x31){clrscr();printf("\n\n");printf(" [1] 2 3 4 5 6 7 8 9 0\n\n"); printf(" press [n] to exit");}
else if(press_number==0x32){clrscr();printf("\n\n");printf(" 1 [2] 3 4 5 6 7 8 9 0\n\n"); printf(" press [n] to exit");}
else if(press_number==0x33){clrscr();printf("\n\n");printf(" 1 2 [3] 4 5 6 7 8 9 0\n\n"); printf(" press [n] to exit");}
else if(press_number==0x34){clrscr();printf("\n\n");printf(" 1 2 3 [4] 5 6 7 8 9 0\n\n"); printf(" press [n] to exit");}
else if(press_number==0x35){clrscr();printf("\n\n");printf(" 1 2 3 4 [5] 6 7 8 9 0\n\n"); printf(" press [n] to exit");}
else if(press_number==0x36){clrscr();printf("\n\n");printf(" 1 2 3 4 5 [6] 7 8 9 0\n\n"); printf(" press [n] to exit");}
else if(press_number==0x37){clrscr();printf("\n\n");printf(" 1 2 3 4 5 6 [7] 8 9 0\n\n"); printf(" press [n] to exit");}
else if(press_number==0x38){clrscr();printf("\n\n");printf(" 1 2 3 4 5 6 7 [8] 9 0\n\n"); printf(" press [n] to exit");}
else if(press_number==0x39){clrscr();printf("\n\n");printf(" 1 2 3 4 5 6 7 8 [9] 0\n\n"); printf(" press [n] to exit");}
}
if(if_back==0) my_send(press_number+1);
}
press_number=my_receive();
if(press_number!=-1)
{
printf("%x",press_number);fputc(press_number,fp);
}
} while(!if_back);
fclose(fp);
close_com1();
}
/*===================文件错误处理=====================*/
void error_file(char con,int n)
{
printf("would like try again y/n");
con=getch();
if(con=='y') n++;
if(con=='n') {n=0;exit(0);}
}
/*----------------四个功能模块------------------------------*/
/*===================打开端口=====================*/
void open_com1()
{
unsigned char temp;
disable();
asyncoldvect=getvect(0x0c);
initial_com1();
inportb(0x3f8);
inportb(0x3fe);
inportb(0x3fb);
inportb(0x3fa);
temp=inportb(0x21)&0xef;
outportb(0x21,temp);
setvect(0x0c,asyncint);
enable();
}
/*===================中断端口=====================*/
void interrupt far asyncint()
{
buffer[buffin++]=inportb(0x3f8);
if(buffin>=SPACE)
buffin=0;
outportb(0x20,0x20);
}
/*===================关闭端口=====================*/
void close_com1(void)
{
disable();
outportb(0x3f9,0x00);
outportb(0x3fc,0x00);
outportb(0x21,inportb(0x21)&0x10);
enable();
setvect(0x0c,asyncoldvect);
}
/*===================初试化端口=====================*/
void initial_com1()
{
outportb(0x3FB,0x80); /****线路控制寄存器LCR***/
outportb(0x3F8,0x0c); /*******baud 9600********/
outportb(0x3F9,0x00);
outportb(0x3FB,0x03); /****帧格式:1位停止位,8位数据位,无校验位****/
outportb(0x3FC,0x08);
outportb(0x3F9,0x03);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -