Last updated on the afternoon of January 3, 2021
Last updated on the afternoon of January 3, 2021
c语言串口通信范例
一个c语言的串口通信程序范例
标签:
分类:
最近接触一个项目,用HL-C1C激光位移传感器+易控组态软件完成生产线高度跳变检测,好久没有接触cc#,一些资料,找来做个记录,也许大家用的着
#include<>#include<>#include<>#include<>
#defineCOM232?0x2f8#defineCOMINT?0x0b
#defineMaxBufLen500#definePort82590x20#defineEofInt0x20
staticintcomportaddr;staticcharintvectnum;staticunsignedcharmaskb;staticunsignedcharBuffer[MaxBufLen];staticintCharsInBuf,CircIn,CircOut;
staticvoid(interruptfar*OldAsyncInt)();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+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);}
staticvoidinterruptfarAsyncInt(void){disable();
if(CharsInBuf
voidRestore(void){setvect(intvectnum,OldAsyncInt);outp(Port8259+1,maskb);}intGetCharInBuf(unsignedchar*Char){intFlag;
Flag=-1;
if(CharsInBuf>0){(*Char)=Buffer[CircOut];if(CircOut
intSendChar(unsignedcharChar){if((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()){