资源描述:
《一个c语言的串口通信程序范例.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、#include#include#include#include#defineCOM232 0x2f8#defineCOMINT 0x0b#defineMaxBufLen 500#definePort8259 0x20#defineEofInt 0x20staticint comportaddr;staticcharintvectnum;staticunsignedcharmaskb;staticunsignedcharBuffer[MaxBufLen];
2、staticint CharsInBuf,CircIn,CircOut;staticvoid(interruptfar*OldAsyncInt)();staticvoidinterruptfarAsyncInt(void);voidInit_COM(intComPortAddr,unsignedcharIntVectNum,intBaud, unsignedcharData,unsignedcharStop,unsignedcharParity){ unsignedcharHigh,Low; intf; comportaddr=ComPortA
3、ddr; intvectnum=IntVectNum; CharsInBuf=0;CircIn=0;CircOut=0; f=(Baud/100); f=1152/f;High=f/256; Low=f-High*256; outp(ComPortAddr+3,0x80); outp(ComPortAddr,Low); outp(ComPortAddr+1,High); Data=(Data-5)
4、((Stop-1)*4); if(Parity==2)Data=Data
5、0x18; elseif(Parity==1)Data=Data
6、0x8; ou
7、tp(ComPortAddr+3,Data); outp(ComPortAddr+4,0x0a); outp(ComPortAddr+1,0x01); disable(); OldAsyncInt=getvect(IntVectNum); setvect(IntVectNum,AsyncInt); enable(); maskb=inp(Port8259+1); if(IntVectNum==0x0c)outp(Port8259+1,maskb&0xef); elseoutp(Port8259+1,maskb&0xf7);
8、 }staticvoidinterruptfarAsyncInt(void){ disable(); if(CharsInBuf9、yncInt); outp(Port8259+1,maskb); }intGetCharInBuf(unsignedchar*Char){ intFlag; Flag=-1; if(CharsInBuf>0) { (*Char)=Buffer[CircOut]; if(CircOut10、0)==0)return-1; outp(comportaddr,Char); return0;}main(){ inti,c; unsignedcharInChar; Init_COM(COM232,COMINT,1200,8,1,0); while(1) { if(kbhit()) {if((InChar=getch())==27)break; elsewhile(SendChar(InChar)); } if(GetCharInBuf(&InChar)==0) printf("%c",InChar); }Resto
11、re();}