- 中查找“深入Windows通信编程”更多相关内容
- 中查找“深入Windows通信编程”更多相关内容
- ·上一篇文章:中国电子商务差在哪儿
- ·下一篇文章:全面了解ODBC技术
深入Windows通信编程
三、Windows通信实例
实例的通信环境为:本方COMPAQ 4/50微机,安装中文Windows3.2;对方为8031单片机。通信参数设置:波特率150bps,数据位8,停止位1,无校验。通信协议是:对方发FF,本方收到后回0F,对方收到0F后发一条十字节的消息,本方回0F,结束一次通信。
编程环境为中文Windows3
#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 CN_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 CN_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:
作者:佚名评论内容只代表网友观点,与本站立场无关!
评论摘要(共 0 条,得分 0 分,平均 0 分)
查看完整评论