资源描述:
《c语言串口通信范例》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、一个c语言的串口通信程序范例标签:c语言串口通信通信程序it分类:技术笔记最近接触一个项目,用HL-C1C激光位移传感器+易控组态软件完成生产线高度跳变检测,好久没有接触cc#,一些资料,找来做个记录,也许大家用的着#include#include#include#include#defineCOM232 0x2f8#defineCOMINT 0x0b#defineMaxBufLen 500#definePort8259 0x20#defineEofInt 0x20staticint
2、 comportaddr;staticcharintvectnum;staticunsignedcharmaskb;staticunsignedcharBuffer[MaxBufLen];staticint CharsInBuf,CircIn,CircOut;staticvoid(interruptfar*OldAsyncInt)();staticvoidinterruptfarAsyncInt(void);voidInit_COM(intComPortAddr,unsignedcharIntVectNum,intBaud, unsignedcharData,unsign
3、edcharStop,unsignedcharParity){ unsignedcharHigh,Low; intf; comportaddr=ComPortAddr; 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
5、); if(Parity==2)Data=Data
6、0x18; elseif(Parity==1)Data=Data
7、0x8; outp(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
8、&0xef); elseoutp(Port8259+1,maskb&0xf7); }staticvoidinterruptfarAsyncInt(void){ disable(); if(CharsInBuf9、estore(void){ setvect(intvectnum,OldAsyncInt); outp(Port8259+1,maskb); }intGetCharInBuf(unsignedchar*Char){ intFlag; Flag=-1; if(CharsInBuf>0) { (*Char)=Buffer[CircOut]; if(CircOut10、((inp(comportaddr+5)&0x20)==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));