首 页 用户登录 | ![]() |
|||
|
|||
按字母检索 | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z |
按声母检索 | A | B | C | D | E | F | G | H | J | K | L | M | N | O | P | Q | R | S | T | W | X | Y | Z | 数字 | 符号 |
|
![]() |
您的位置: 5VAR论文频道 → 论文中心 → 计算机论文 → 计算机理论 |
|
|||||
深入Windows通信编程 | |||||
收集整理:佚名 来源:本站整理 时间:2009-01-10 12:07:59 点击数:[] ![]() |
|||||
口,加上随卡提供的Windows驱动程序,就可以进行多串口通信。具体用法请参阅扩展卡说明书。 三、Windows通信实例 #include<windows.h> #include<owl.h> #include<window.h> #include<string.h> int COM=1;//串口号 unsigned char ReceiveBuff〔11〕;//接收数据缓存 _CLASSDEF(TCommApp) class TCommApp: public Tapplication { public: TCommApp(LPSTR AName, HINSTANCE hInstance, HINSTANCE HPrevInstance, LPSTR 1p CmdLine, int nCmdshow) :TApplication(AName, hInstance, hPrevInstance, 1pCmd Line, nCmdShow){}; virtual void InitMain Window(); }; _CLASSDEF(TCommWin)//主窗口类 class TComm Win: public TWindow { public: TComm Win(PTWindowsObject AParent, LPSTR ATitle): TWindow(AParent, Atitle){} int InitCom(); void SetBaud();//设置Windows不支持的波特率 virtual BOOL WMCommNotify(TMessage & Mg)=〔WM_FIRST+ WM_COMMNOTIFY〕; virtual void Setup Window(); }; //该函数设置串口2的波特率为150bps,若用Windows提//供的波特率通信,则无须该函数 Void TCommWin::SetBaud() { asm cli; asm mov dx,2fbh; asm mov al,80h; asm out dx,al; asm mov dx,2f8h; asm mov al,00h; asm out dx,al; asm mov dx,2f9h; asm mov al,3; asm out dx,al; asm mov dx,2fbh; asm mov al,03; asm out dx,al; asm mov dx,2fch; asm mov al,0bh; asm out dx,al; asm mov dx,2f9h; asm mov al,0fh; asm out dx,al; asm mov al,20h; asm out 21h,al; asm sti; } int TComm Win::InitCom() { char str〔20〕,s〔2〕; int COMid,err; DCB dcb;//设备控制块 UINT Mask=EV_BREAKEV_ERREV_RXFLAG;//事件掩码 strcpy(str,"COM"); strcat(str,itoa(COM+1,s,10)); COMid=OpenComm(str,128,1); if(COMid<0) return COMid; strcat(str,":300,n,8,1"); err=BuildCommDCB(str,&dcb); dcb.EvtChar=-1;//事件字符0xff err=SetCommState(&dcb); SetBaud(); if(err>0) return err; FlushComm(COMid,1); if(!EnableComunNotification(COMid,HWindow,10,-1)) return -1; SetCommEventMask(COMid,Mask); return COMid; } void TCommWin::SetupWindow() { TWindow::SetupWindow(); InitCom(); } BOOL TCommWin::WMCommNotify(TMessage &Mg) { UINT flag=0; int id; COMSTAT stat; unsigned char SendChar; static unsigned char *p=ReceiveBuff; static num=0; int ret; id=Mg.WParam; switch(Mg.LP.Lo) { case com_EVENT://有事件掩码中定义的事件发生 flag=GetCommEventMask(id,EV_BREAK); if(flag & EV_BREAK) FlushComm(id,1); flag=GetCommEventMask(id,EV_RXFLAG); if(flag & EV_ERR) FlushComm(id,1); flag=GetCommEventMask(id,EV_RXFLAG); if(flag & EV_RXFLAG)//收到了事件字符0xff { SendChar=0x0f; WriteComm(id,& SendChar,1);//向对方回0x0f } break; case com_RECEIVE://接收到了规定个字符或超时 do { ret=ReadComm(id,p,1); if(ret>0) { p++; num++; } }while((ret>0)&(num<10)); if(num>=10)//接收完一条消息 { num=0; //此处处理接收到的消息 p=ReceiveBuff; SendChar=0x0f; WriteComm(id,& SendChar,1);//向对方回0x0f FlushComm(id,1); }break; } flag=GetCommError(id,&stat);//消除错误(若有) return 1; } void TCommApp::InitMainWindow() { MainWindow=new TCommWin(NULL, "Windows通信示例"); } int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevI nstance,LPSTR 1pCmdLine, int nCmdShow) { TCommApp CommApp("通信", hInstance,hPrevInstance,1pC mdLine, nCmdShow); CommApp.Run(); return CommApp.Status; } Tags: |
提供人:佚名 | |
【返回上一页】【打 印】【关闭窗口】 |
![]() |
5VAR论文频道 |
![]() |
5VAR论文频道 |
![]() |
关于本站 -
网站帮助 -
广告合作 -
下载声明 -
网站地图
Copyright © 2006-2033 5Var.Com. All Rights Reserved . |