一个 c 语言的串口通信程序范例标签:分类:技术笔记c 语言串口通信通信程序it最近接触一个项目,用 HL-C1C 激光位移传感器+易控组态软件完成生产线高度跳变检测,好久没有接触 cc#,一些资料,找来做个记录,也许大家用的着#include#include#include#include#defineCOM2320x2f8#defineCOMINT0x0b#defineMaxBufLen500#definePort82590x20#defineEofInt0x20staticintcomportaddr;staticcharintvectnum;staticunsignedcharmaskb;staticunsignedcharBuffer[MaxBufLen];staticintCharsInBuf,CircIn,CircOut;staticvoid(interruptfar*OldAsyncInt)();outp(ComPortAddr+4,0x0a);staticvoidinterruptfarAsyncInt(void);voidInit_COM(intComPortAddr,unsignedcharIntVectNum,intBaud,unsignedcharData,unsignedcharStop,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)|((Stop-1)*4);if(Parity==2)Data=Data|0x18;elseif(Parity==1)Data=Data|0x8;outp(ComPortAddr+3,Data);outp(ComPortAddr+1,0x01);outp(ComPortAddr+4,0x0a);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);}staticvoidinterruptfarAsyncInt(void){disable();if(CharsInBuf0){(*Char)=Buffer[CircOut];if(CircOut