C语言实现串行通信接口程序(2)
作者:佚名; 更新时间:2014-12-05
ize 4096
#define SEND 2
#define RECEIVE 1
#define COM1 0
#define COM2 1
static unsigned char Hardinterrupt=0;
struct ComInterrupt
{int portadd;
int intbit;
char buf[maxsize],*comm;
int bufh,recount,sendcount;
}com[2]={{0x3f8,0x0c,,,0,0,0},
{0x2f8,0x0b,,,0,0,0} };
void static interrupt (*old_com[2])(void);
vold interrupt coml(vold);
void interrupt com2(void);
void fax2(int comnum);
void setinterrupt(int comnum);
void clearinterrupt(int comnum);
void cominit(int comnum, int para, int interruptmark);
void sendcomdata (int comnum,char *command);
int getcomdata (int comnum, char *buf);
void interrupt com1(void)
{fax2(0);}
void interrupt com2(void)
{fax2(1);}
// set cominterrupt, comnum 0=com1, 1=com2
void setinterrupt (int comnum)
{
old_com[comnum]=getvect(com[comnum].intbit);
if (!oomnum)
setvect(com[comnum].intbit,coml); //com1
else
setvect(com[comnum].intbit,com2); //com2
//set hard int
Hardinterrupt = inportb(0x21);
if(comnum)
outportb(0x21,Hardinterrupt&0xf7); //com2 ,0
else
outportb(0x21,Hardinterrupt&0xef); //com1 0,
}
void clear interrupt(int comnum)
{
if(comnum)
outportb(0x21,Hardinterrupt | 0x08); //COM2
else
outportb(0x21,Hardinterrupt|0x10); //COM1
setvect(com[comnum].intbit,old_com[comnum]);
for( i=0;i<maxsize;i++) com[comnum].buf[i]=\0;
com[comnum].sendcount=com[comnum].recount=com[comnum].bufh=0;
outportb(com[comnum].portadd+1,0);
outportb(com[comnum].por tadd+4,0x0);
}
void fax2(int i)//i=o,com1; i=1, com2
{ unsigned char mark;
mark=inport(com[i].portadd+2);
do
{
if(mark&0x4)// receive data
{ if (com[i].bufh==maxsize)
com[i].bufh=0; com[i].buf[com[i].bufh++]=inportb(com[i].portadd); com[
i].recount++;}
else if(mark&0x2)// send command
{ if(*com[i].comm)
outportb(com[i].p
ortadd,*com[i].comm++);
com[i],sendcount++;}
else
outportb(com[i].portadd+1,1);
}
}while ((mark=inport([1]. portadd+2))!=1);
outportb(ox20,0x20); //hard int return
}
// interruptmark 1= reoeive, 2=send, 3=rec&send
void comint(int com, char para, int interruptmark)
{
bioscom(0, par, com);
//open com interrupt
outportbv (com[comnum]. portadd+4,0x8;
outportb (com[comnum].portadd+1,interruptmark);
}
void sendcomdata(int comnum,char * command)
{ unsigned char interruptmark;
com[comnum],comm=command;
com[comnum],sendcount=0;
//set send interrupt
interruptmark=inportb (com[comnum].portadd_1);
outportb (com[comnum].portadd+1.(interruptmark|2));
}
//get com_receivedate and clear com_receivebuf,
int getcomdata (int comnum, char * buf)
{ int result=com[comnum]. recount,i:
if(buf)
strncpy(buf,com[comnum].buf,com
[comnum].bufh);
buf[com[comnum].bufh]=\0;
com[comnum].recount=com [comnum].bufh=0;
retun(result);
}

 

核心期刊快速发表
Copyright@2000-2030 论文期刊网 Corporation All Rights Reserved.
《中华人民共和国信息产业部》备案号:ICP备07016076号;《公安部》备案号:33010402003207
本网站专业、正规提供职称论文发表和写作指导服务,并收录了海量免费论文和数百个经国家新闻出版总署审批过的具有国内统一CN刊号与国际标准ISSN刊号的合作期刊,供诸位正确选择和阅读参考,免费论文版权归原作者所有,谨防侵权。联系邮箱:256081@163.com