利用中断方式通讯的测试程序

一个COM1与COM2利用中断方式通讯的测试程序:  
    
  #include   <stdio.h>  
  #include   <stdlib.h>  
  #include   <dos.h>  
  #include   <bios.h>  
  #include   <conio.h>  
   
  #define   BASE1   0x3f8  
  #define   BASE2   0x2f8  
  #define   DATA   0  
  #define   IER     1  
  #define   IIR     2  
  #define   LCR     3  
  #define   MCR     4  
  #define   LSR     5  
  #define   MSR     6  
  #define   INTNO1   0x0c  
  #define   INTNO2   0x0b  
  #define   INTON1   0xef  
  #define   INTON2   0xf7  
  #define   BUFSIZE   512  
  unsigned   char   inbuf[BUFSIZE],outbuf[BUFSIZE];  
  unsigned   int   incount=0,inrear=0,outcount=0,outrear=0,newcount=0,inflag=0;  
  unsigned   char   *news="                                                                                                     ";  
   
  void   interrupt   (*oldch1)();  
  void   interrupt   far   ch1();  
  void   interrupt   (*oldch2)();  
  void   interrupt   far   ch2();  
   
  void   interrupt   far   ch1()  
            {  
            register   int   id;  
            enable();  
            while   (1)  
                  {  
  id=inportb(BASE1+IIR);  
  if   (id==1)  
        {  
        outportb(0x20,0x20);  
  /*       news="com1   no   interrupt   !!!";  
        newcount=1;*/  
        return;  
        }  
                  switch   (id)  
                        {  
                        case   2:  
                                  if   (outcount!=outrear)  
                                        {  
        outportb(BASE1+DATA,outbuf[outrear]);  
                                        outrear++;  
                                        if   (outcount==outrear)  
                                              {  
                                              outcount=0;  
                                              outrear=0;  
                                              }  
        news="data   com1   out   !!!";  
                                        newcount=1;  
        }  
                                  break;  
                        case   4:  
  inbuf[incount]=inportb(BASE1+DATA);  
  incount++;  
  if   (inbuf[incount-1]==0x0d)  
                                        {  
                                        inbuf[incount-1]='/0';  
        news="data   com1   in   !!!";  
                                        newcount=1;  
                                        inflag=1;  
        }  
                                  break;  
                        case   6:  
  news="com1   error   !!!";  
  newcount=1;  
  break;  
        defiult:  
  news="com1   no   data   !!!";  
  newcount=1;  
  break;  
                        }  
                  }  
  /*           (*oldch1)();*/  
            }  
   
  void   interrupt   far   ch2()  
            {  
            register   int   id;  
            enable();  
            while   (1)  
                  {  
                  id=inportb(BASE2+IIR);  
                  if   (id==1)  
                        {  
        outportb(0x20,0x20);  
        news="no   interrupt   !!!";  
        newcount=1;  
                        return;  
                        }  
                  switch   (id)  
                        {  
                        case   2:  
                                  if   (outcount!=outrear)  
                                        {  
                                        outportb(BASE2+DATA,outbuf[outrear]);  
                                        outrear++;  
                                        if   (outcount==outrear)  
                                              {  
                                              outcount=0;  
                                              outrear=0;  
                                              }  
        news="data   com2   out   !!!";  
                                        newcount=1;  
                                        }  
                                  break;  
                        case   4:  
                                  inbuf[incount]=inportb(BASE2+DATA);  
  incount++;  
  if   (inbuf[incount-1]==0x0d)  
                                        {  
                                        inbuf[incount-1]='/0';  
        news="data   com2   in   !!!";  
                                        newcount=1;  
                                        inflag=1;  
                                        }  
                                  break;  
                        case   6:  
                                  news="com   error   !!!";  
                                  newcount=1;  
  break;  
        defiult:  
  news="com2   no   data   !!!";  
  newcount=1;  
  break;  
                        }  
                  }  
  /*           (*oldch2)();*/  
            }  
   
  main   ()  
            {  
            unsigned   char   ch,p,i;  
            clrscr();  
            disable();  
            outportb(BASE1+LCR,0x80);  
            outportb(BASE1+DATA,0x02);  
            outportb(BASE1+IER,0x00);  
            outportb(BASE1+LCR,0x03);  
            inportb(BASE1+IIR);  
            inportb(BASE1);  
            outportb(BASE2+LCR,0x80);  
            outportb(BASE2+DATA,0x02);  
            outportb(BASE2+IER,0x00);  
            outportb(BASE2+LCR,0x03);  
            inportb(BASE2+IIR);  
            inportb(BASE2);  
            oldch1=getvect(INTNO1);  
            setvect(INTNO1,ch1);  
            oldch2=getvect(INTNO2);  
            setvect(INTNO2,ch2);  
            outportb(BASE1+IER,0x03);  
            outportb(BASE1+MCR,0X08);  
            outportb(BASE2+IER,0x03);  
            outportb(BASE2+MCR,0X08);  
            outportb(0x21,inportb(0x21)&INTON1);  
            outportb(0x21,inportb(0x21)&INTON2);  
            enable();  
            p=1;  
            while   (p)  
                  {  
                  if   (kbhit()!=0)  
                        {  
                        ch=getch();  
                        if   (ch==0)   ch=getch();  
                        if   (ch==27)   p=0;  
                        else  
                              {  
                              outbuf[outcount]=ch;  
                              if   (ch==0x0d)   outbuf[outcount+1]='/0';  
                              outcount++;  
                              }  
        if   ((ch==0x0d)&&(outcount!=0))  
                              {  
              outportb(BASE1+DATA,outbuf[outrear]);  
                              outrear++;  
              printf("/n%02d   :   ",outcount);  
                              for   (i=0;i<outcount;i++)   printf("%c",outbuf[i]);  
                              }  
                        }  
                  if   (newcount==1)  
                        {  
        printf("/n%s",news);  
                        newcount=0;  
                        }  
  if   (inflag==1)  
                        {  
        printf("/n%02d   :   ",incount);  
        for   (i=0;i<incount;i++)   printf("%c",inbuf[i]);  
        inflag=0;incount=0;inrear=0;  
                        }  
                  }  
            setvect(INTNO1,oldch1);  
            setvect(INTNO2,oldch2);  
            outportb(0x21,inportb(0x21)|~INTON1);  
            outportb(0x21,inportb(0x21)|~INTON2);  
            outportb(BASE1+MCR,0);  
            outportb(BASE1+IER,0);  
            outportb(BASE2+MCR,0);  
            outportb(BASE2+IER,0);  
            }   
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值