您的位置:首页 > 其它

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

2008-12-06 11:27 274 查看
一个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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐