c语言串口通信范例

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

一个c语言的串口通信程序范例标签:c语言串口通信通信程序it分类:技术笔记最近接触一个项目,用HL-C1C激光位移传感器+易控组态软件完成生产线高度跳变检测,好久没有接触cc#,一些资料,找来做个记录,也许大家用的着#includestdio.h#includedos.h#includeconio.h#includestring.h#defineCOM2320x2f8#defineCOMINT0x0b#defineMaxBufLen500#definePort82590x20#defineEofInt0x20staticintcomportaddr;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(CharsInBufMaxBufLen)Buffer[CircIn]=inp(comportaddr);if(CircInMaxBufLen-1)CircIn++;elseCircIn=0;if(CircIn==CircOut)CircOut++;elseCharsInBuf++;enable();outp(Port8259,EofInt);}voidRestore(void){setvect(intvectnum,OldAsyncInt);outp(Port8259+1,maskb);}intGetCharInBuf(unsignedchar*Char){intFlag;Flag=-1;if(CharsInBuf0){(*Char)=Buffer[CircOut];if(CircOutMaxBufLen-1)CircOut++;elseCircOut=0;CharsInBuf--;Flag=0;}returnFlag;}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()){if((InChar=getch())==27)break;elsewhile(SendChar(InChar));}if(GetCharInBuf(&InChar)==0)printf(%c,InChar);}Restore();}接收程序:#includedos.h#includefstream.h#includeconio.h#includestdio.h#includestdlib.h#includemath.h#defineRXD0//接收#defineTXD0//发送#defineLSB0//波特率调节低8位#defineMSB1//波特率调节高8位#defineIER1//中断起用寄存器#defineIIR2//中断标识寄存器#defineLCR3//线路控制寄存器#defineMCR4//调制解调器控制寄存器#defineLSR5//线路状态寄存器#defineMSR6//调制解调器状态寄存器#defineIERV1#defineOUT20x08#defineERTS2#defineEDTR1#defineEMPTY0X20#defineREADY0X30#defineICREG0X20#defineIMASKREG0X21#defineEOI0X20#defineWAITCOUNT5000#defineBUFFLEN2048//用于存储字符的数组的界#defineALTE0X12#defineALTQ0X10#defineSER_BAUD_120096#defineSER_BAUD_240048#defineSER_BAUD_96000x0C#defineSER_BAUD_192006#defineSER_STOP_10/*/1stopbitpercharacter*/#defineSER_STOP_24/*/2stopbitspercharacter*/#defineSER_BITS_50/*/send5bitcharacters*/#defineSER_BITS_61/*/send6bitcharacters*/#defineSER_BITS_72/*/send7bitcharacters*/#defineSER_BITS_83/*/send8bitcharacters*/#defineSER_PARITY_NONE0/*/noparity*/#defineSER_PARITY_ODD8/*/oddparity*/#defineSER_PARITY_EVEN24/*/evenparity*/intport;intComNum;unsignedportaddr;unsignedportf;unsignedintbaudtable[]={0x180,0xc0,0x60,0x30,0x18,0x0c,0x06};unsignedcharparitytable[]={0x08,0x18,0x00,0x28,0x38};unsignedcharbuffer[BUFFLEN];//recvbufintbuffin=0;intbuffout=0;intincount=0;intoutcount=0;voidinterrupt(*vect_com)(...);voidputb(unsignedcharch)//writeachartotherecvbuf将中断得到的数据写到缓冲区{inttemp;temp=buffin;if(++buffin==BUFFLEN)buffin=0;if(buffin!=buffout){buffer[buffin]=ch;//printf(bufferin[%d]=%c,buffer[buffin]);//getch();}elsebuffin=temp;};unsignedchargetb()//readacharfromtherecvbuf{if(buffout!=buffin){if(++buffout==BUFFLEN)buffout=0;//printf(bufferout[%d]=%c,buffout,buffer[buffout]);return(buffer[buffout]);}elsereturn(0xff);};/*unsignedcharsender(unsignedcharch){outportb(portaddr2+TXD,ch);printf(\nsenderoutportdata=%c\n,ch);outcount++;return(1);};*/voidinterruptreceiver(...){unsignedcharch;ch=inportb(portaddr+RXD);putb(ch);incount++;//记录接收了多少个数据。outportb(ICREG,EOI);};voidSerInit(intbaud,intconfiguration){disable();outportb(portaddr+LCR,0x80);outportb(portaddr+LSB,baud);outportb(portaddr+MSB,0x00);outportb(portaddr+LCR,configuration);enable();};voidSerOpen(void){vect_com=getvect(portf+8);disable();inportb(portaddr+RXD);inportb(portaddr+MSR);inportb(portaddr+LSR);inportb(portaddr+IIR);outportb(portaddr+IER,IERV);outportb(portaddr+MCR,OUT2|ERTS|EDTR);outportb(IMASKREG,inportb(IMASKREG)&(~(1portf)));setvect(portf+8,receiver);enable();};voidSerClose(void){disable();outportb(portaddr+IER,0);outportb(portaddr+MCR,0);outportb(IMASKREG,inportb(IMASKREG)|(1portf));enable();setvect(portf+8,vect_com);};voidGetportaddr(intport){switch(port){case0:portaddr=0x3F8;break;case1:portaddr=0x2F8;break;case2:portaddr=0x3E8;break;case3:portaddr=0x2E8;break;}portf=(port==0)?4:3;};voidInt(){unsignedcharkey,key2;port=ComNum-1;if(peek(0x40,port*2)==0){printf(havenospecialcom.\n);exit(1);}else{printf(Theusedportis:COM%d\n,ComNum);};Getportaddr(port);//得到串口地址SerInit(SER_BAUD_9600,SER_PARITY_EVEN|SER_BITS_8|SER_STOP_1);//初始化串口,设置波特率等SerOpen();do{if(kbhit()){key2=getch();if(key2==27){break;}};key=getb();if(key!=0xff){printf

1 / 13
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功