利用中断方式通讯的测试程序
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);
}
#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);
}
相关文章推荐
- 串口通讯程序(中断方式)
- 用node.js建博客(五) - 用vows以BDD方式测试程序
- dsp控制DM9000实现802.3数据收发第三篇,调试过程程序第三版;采用外部引脚中断方式获取中断,优化掉帧现象
- 利用delphi编写Socket通讯程序
- 【程序27】利用递归函数调用方式,将所输入的5个字符,以相反顺序打印出来
- 微信小程序通讯地址授权拒绝后的处理方式
- java application 应用程序 使用JDBC proxool 两种方式连接数据库 测试程序代码
- paip.程序模块间的通讯方式大总结
- asp.net使用jbox插件,利用ajax(使用一般处理程序方式)执行撤销操作
- idea利用sbt打包scala程序为jar包 并发布到集群中测试
- 利用socket编写windows和linux通讯程序
- 怎么利用微信小程序来盈利?总有一种方式适合你的
- Process Monitor监测记录表明,QQ不仅会自动访问许多与聊天无关的程序和文档,例如“我的文档”等敏感位置,测试当天的上网记录也没能幸免。随后,QQ还会产生大量网络通讯,很可能是将数据上传到腾讯服务器。短短10分钟内,它访问的无关
- 利用socket编写windows和linux通讯程序
- TestNG参数化测试-数据提供程序 @DataProvider方式
- main app方式启动servlet容器(Jetty) 测试web程序
- unity3D中Socket链接与服务之间的通讯测试程序
- 利用数学证明方式验证程序的可靠性(一)
- 如何写安全的网络通讯服务器程序----避免程序崩溃的几种方式
- Android实例-为程序创建快捷方式(未测试)