《c语言串口通信范例30964.pdf》由会员分享,可在线阅读,更多相关《c语言串口通信范例30964.pdf(16页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、一个 c 语言的串口通信程序范例 标签:c 语言 串口通信 通信程序 it 分类:技术笔记 最近接触一个项目,用 HL-C1C 激光位移传感器+易控组态软件完成生产线高度跳变检测,好久没有接触 c c#,一些资料,找来做个记录,也许大家用的着#include#include#include#include#define COM232 0 x2f8#define COMINT 0 x0b#define MaxBufLen 500#define Port8259 0 x20#define EofInt 0 x20 static int comportaddr;static char intvect
2、num;static unsigned char maskb;static unsigned char BufferMaxBufLen;static int CharsInBuf,CircIn,CircOut;static void(interrupt far*OldAsyncInt)();static void interrupt far AsyncInt(void);void Init_COM(int ComPortAddr,unsigned char IntVectNum,int Baud,unsigned char Data,unsigned char Stop,unsigned ch
3、ar Parity)unsigned char High,Low;int f;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,0 x80);outp(ComPortAddr,Low);outp(ComPortAddr+1,High);Data=(Data-5)|(Stop-1)*4);if(Parity=2)Data=Data|0 x18;else if(
4、Parity=1)Data=Data|0 x8;outp(ComPortAddr+3,Data);outp(ComPortAddr+4,0 x0a);outp(ComPortAddr+1,0 x01);disable();OldAsyncInt=getvect(IntVectNum);setvect(IntVectNum,AsyncInt);enable();maskb=inp(Port8259+1);if(IntVectNum=0 x0c)outp(Port8259+1,maskb&0 xef);else outp(Port8259+1,maskb&0 xf7);static void in
5、terrupt far AsyncInt(void)disable();if(CharsInBufMaxBufLen)BufferCircIn=inp(comportaddr);if(CircIn0)(*Char)=BufferCircOut;if(CircOutMaxBufLen-1)CircOut+;else CircOut=0;CharsInBuf-;Flag=0;return Flag;int SendChar(unsigned char Char)if(inp(comportaddr+5)&0 x20)=0)return-1;outp(comportaddr,Char);return
6、 0;main()int i,c;unsigned char InChar;Init_COM(COM232,COMINT,1200,8,1,0);while(1)if(kbhit()if(InChar=getch()=27)break;else while(SendChar(InChar);if(GetCharInBuf(&InChar)=0)printf(%c,InChar);Restore();接收程序:#include#include#include#include#include#include#define RXD 0.);void putb(unsigned char ch).)u
7、nsigned char ch;ch=inportb(portaddr+RXD);putb(ch);incount+;n);exit(1);else printf(The used port is:COM%dn,ComNum);Getportaddr(port);.#else#define _CPPARGS#endif#define SER_RBF 0#define SER_THR 0#define SER_IER 1#define SER_IIR 2#define SER_LCR 3#define SER_MCR 4#define SER_LSR 5#define SER_MSR 6#def
8、ine SER_DLL 0#define SER_DLH 1#define SER_BAUD_1200 96#define SER_BAUD_2400 48#define SER_BAUD_9600 12#define SER_BAUD_19200 6#define SER_GP02 8#define COM_1 0 x3F8#define COM_2 0 x2F8/*/base port address of port 1*/#define COM_3 0 x3E8#define COM_4 0 x2E8#define SER_STOP_1 0/*/1 stop bit per charac
9、ter*/#define SER_STOP_2 4/*/2 stop bits per character*/#define SER_BITS_5 0/*/send 5 bit characters*/#define SER_BITS_6 1/*/send 6 bit characters*/#define SER_BITS_7 2/*/send 7 bit characters*/#define SER_BITS_8 3/*/send 8 bit characters*/#define SER_PARITY_NONE 0/*/no parity*/#define SER_PARITY_ODD
10、 8/*/odd parity*/#define SER_PARITY_EVEN 24/*/even parity*/#define SER_DIV_LATCH_ON 128/*/used to turn reg 0,1 into divisor latch*/#define PIC_IMR 0 x21/*/pics interrupt mask reg.*/#define PIC_ICR 0 x20/*/pics interupt control reg.*/#define INT_SER_PORT_0 0 x0C/*/port 0 interrupt com 1&3*/#define IN
11、T_SER_PORT_1 0 x0B/*/port 0 interrupt com 2&4*/#define SERIAL_BUFF_SIZE 128/*/current size of circulating receive buffer*/void interrupt far(*Old_Isr)(_CPPARGS);/*/holds old com port interrupt handler*/char ser_bufferSERIAL_BUFF_SIZE;/*/the receive buffer*/int ser_end=-1,ser_start=-1;/*/indexes into
12、 receive buffer*/int ser_ch,char_ready=0;/*/current character and ready flag*/int old_int_mask;/*/the old interrupt mask on the PIC*/int open_port;/*/the currently open port*/int serial_lock=0;/*/serial ISR semaphore so the buffer*/*/isnt altered will it is being written*/*/to by the ISR*/*-写串口-*/vo
13、id interrupt far Serial_Isr(_CPPARGS)serial_lock=1;ser_ch=inp(open_port+SER_RBF);if(+ser_end SERIAL_BUFF_SIZE-1)ser_end=0;ser_bufferser_end=ser_ch;+char_ready;outp(PIC_ICR,0 x20);serial_lock=0;int Ready_Serial()return(char_ready);/*-读串口-*/int Serial_Read()int ch;while(serial_lock)if(ser_end!=ser_sta
14、rt)if(+ser_start SERIAL_BUFF_SIZE-1)ser_start=0;ch=ser_bufferser_start;printf(%x,ch);if(char_ready 0)-char_ready;return(ch);else return(0);/*-写串口-*/Serial_Write(char ch)while(!(inp(open_port+SER_LSR)&0 x20)asm cli outp(open_port+SER_THR,ch);asm sti /*-初始化串口-*/Open_Serial(int port_base,int baud,int c
15、onfiguration)open_port=port_base;disable();outp(port_base+SER_LCR,SER_DIV_LATCH_ON);outp(port_base+SER_DLL,baud);outp(port_base+SER_DLH,0);outp(port_base+SER_LCR,configuration);outp(port_base+SER_MCR,SER_GP02);outp(port_base+SER_IER,1);if(port_base=COM_1|port_base=COM_3)Old_Isr=_dos_getvect(INT_SER_
16、PORT_0);_dos_setvect(INT_SER_PORT_0,Serial_Isr);printf(nOpening Communications Channel Com Port#1/3.n);else Old_Isr=_dos_getvect(INT_SER_PORT_1);_dos_setvect(INT_SER_PORT_1,Serial_Isr);printf(nOpening Communications Channel Com Port#2/4.n);old_int_mask=inp(PIC_IMR);outp(PIC_IMR,(port_base=COM_1)(old
17、_int_mask&0 xEF):(old_int_mask&0 xF7);enable();/*-关闭串口-*/Close_Serial(int port_base)outp(port_base+SER_MCR,0);outp(port_base+SER_IER,0);outp(PIC_IMR,old_int_mask);if(port_base=COM_1)_dos_setvect(INT_SER_PORT_0,Old_Isr);printf(nClosing Communications Channel Com Port#1.n);else _dos_setvect(INT_SER_PO
18、RT_1,Old_Isr);printf(nClosing Communications Channel Com Port#2.n);/*-发送应用-*/void main(int argc,char*argv)char ch,press;int done=0;FILE*fp;argc=2;/argv1=c:;if(argc2)printf(nUsage:display!);/exit(1);if(fp=fopen(argv1,r+b)=NULL)printf(cannot open the filen);/exit(0);fseek(fp,0,SEEK_SET);Open_Serial(CO
19、M_1,SER_BAUD_9600,SER_PARITY_EVEN|SER_BITS_8|SER_STOP_1);printf(com:1;bps:9600;parity:even;bits:8;stop bit:1);printf(press any key to begin sending);getch();/Serial_Write();/该语句可用于发送单个字符 while(!done&ch!=EOF)/发送文件开始 ch=fgetc(fp);/if(ch=EOF)Serial_Write(27);Serial_Write(ch);delay(30);if(kbhit()press=g
20、etch();if(press=27)Serial_Write(27);done=1;Close_Serial(COM_1);fclose(fp);下面介绍最重要的 MFC:CWnd:窗口,它是大多数“看得见的东西”的父类(Windows 里几乎所有看得见的东西都是一个窗口,大窗口里有许多小窗口),比如视图 CView、框架窗口 CFrameWnd、工具条CToolBar、对话框 CDialog、按钮 CButton,etc;一个例外是菜单(CMenu)不是从窗口派生的。CDocument 文档,负责内存数据与磁盘的交互。最重要的是 OnOpenDocument(读入),OnSaveDocum
21、ent(写盘),Serialize(读写)CView 视图,负责内存数据与用户的交互。包括数据的显示、用户操作的响应(如菜单的选取、鼠标的响应)。最重要的是 OnDraw(重画窗口),通常用 CWnd:Invalidate()来启动它。另外,它通过消息映射表处理菜单、工具条、快捷键和其他用户消息。CDialog 对话框 CWinApp 应用程序类。似于 C 中的 main 函数,是程序执行的入口和管理者,负责程序建立、消灭,主窗口和文档模板的建立。最常用函数 InitInstance():初始化。CGdiObject 及子类,用于向设备文本画图。它们都需要在使用前选进 DC。CPen 笔,画线 CBrush 刷子,填充 CFont 字体,控制文字输出的字体 CBitmap 位图 CPalette 调色板 CRgn 区域,指定一块区域可以用于做特殊处理。CFile 文件。最重要的不外是 Open(打开),Read(读入),Write(写)CString 字符串。封装了 C 中的字符数组,非常实用。CPoint 点,就是(x,y)对 CRect 矩形,就是(left,top,right,bottom)CSize 大小,就是(cx,cy)对(宽、高)
限制150内