积极答复者
如何定时启动消息响应函数

问题
-
串口已在connect.cpp中写好。
在vosdialog.h中声明connect m_com;
在vosdialog的对话框中有一“打开连接”按钮,判断是否打开串口。响应函数其代码为:
if(m_com.Isportopened())
{
m_com.CloseDevice();
if(!m_com.Isportopened())
{
this->SetDlgItemText(IDC_BUTTON_OPEN_PORT,"打开串口");
outstatus="串口状态:未连接";
m_StatusBar.SetText(outstatus, 0, 0);
m_com.Stop_listen();
}
}
else
{
CString out ,bb;
m_selcom.GetWindowText(out);
m_botelv.GetWindowText(bb);
DWORD btelv=atol(bb);
for(int i=0; m_asi.GetCount();i++)
{
if(out==m_asi[i].strFriendlyName)
{
out=m_asi[i].strDevPath;
break;
}
}
m_com.portconfig(out,false,false,btelv);
if(m_com.Isportopened())
{
this->SetDlgItemText(IDC_BUTTON_OPEN_PORT,"关闭串口");m_com.getportattribute(dcb);
for(int i=0; m_asi.GetCount();i++)
{
if(m_com.port_name==m_asi[i].strDevPath)
{
outstatus="串口状态:";
outstatus+=m_asi[i].strFriendlyName;
break;
}
}
outstatus.AppendFormat("; 波特率:%d 已连接",dcb.BaudRate);/////\r\n:
m_StatusBar.SetText(outstatus, 0, 0);
}
}在ControlDialog对话框的头文件中声明vosdialog *p_parent;
其中ControlDialog对话框中一个“连接按钮”的OnConnect()消息响应函数中先判断”串口是否打开,再进读写操作“代码为:
if(p_parent->m_com.Isportopened())
{
PurgeComm(p_parent->m_com.h_port,PURGE_TXCLEAR|PURGE_RXCLEAR);
cache[0]=0xeb;
cache[1]=0x90;
cache[2]=0x01;
cache[3]=0x1A;
cache[4]=cache[3];
wCount=5;
for(int i=0;i<7;i+=3)
{
SetTimer(20,3,NULL);
ClearCommError(p_parent->m_com.h_port,NULL,NULL);
p_parent->m_StatusBar.SetText("发连接FLASH命令...", 0, 0);
bWriteStat=WriteFile(p_parent->m_com.h_port,cache,wCount,&wCount,NULL);
theApp.m_sendcount=wCount;
if(!bWriteStat)
{
AfxMessageBox("写连接命令失败!");
}
else
{
memset(cache,0,5);
wCount=5;
p_parent->m_StatusBar.SetText("接受连接FLASH命令", 0, 0);
bReadStat=ReadFile(p_parent->m_com.h_port,cache,wCount,&wCount,NULL);
theApp.m_receivecount=wCount;
if(!bReadStat)
{
AfxMessageBox("读连接命令失败,请重新上电。");
}
else
{
if(cache[0]==0xeb && cache[1]==0x90 && cache[2]==0x01 && cache[3]==0x1a && cache[4]==0x1a)
{
AfxMessageBox("FLASH连接成功!"); }
}}
}
}
else AfxMessageBox("没有打开串口。");怎么实现在打开串口后10ms内启动ControlDialog对话框中的“连接按钮”的OnConnect()消息响应函数。超过10ms就停止。
答案
-
MFC里面的预定义的消息WM_TIMER,则只需启动和关闭定时器,并定义一个定时器处理函数
开启定时器 SetTimer(1,1000,NULL);//定时器号为1,每一秒钟产生一次(以毫秒为单位)
关闭定时器 KillTimer(1); //关闭ID的1的定时器
从"class wizard"里添加对wm_timer的响应函数,并区分不同的消息号即可。
UINT SetTimer(
HWND hWnd, // 接收定时器消息的窗口句柄
UINT nIDEvent, // 定时器的ID号
UINT uElapse, // 定时时间
TIMERPROC lpTimerFunc // 指向消息处理函数的指针
);在mfc中,由于是在cwnd或其子类中调用的且cwnd类中也有SetTimer函数的封装,定在mfc里调用SetTimer时不用传递窗口句柄,也就是有另一种SetTimer形式:
UINT SetTimer( UINT nIDEvent, UINT nElapse, void(CALLBACKEXPORT* lpfnTimer)(HWND, UINT, UINT, DWORD) );
关闭定时器也就对应有两种形式:
BOOL KillTimer(
HWND hWnd, // handle of window that installed timer
UINT uIDEvent // timer identifier
);BOOL KillTimer( int nIDEvent );
- 已标记为答案 Rob Pan 2011年3月28日 1:43
全部回复
-
MFC里面的预定义的消息WM_TIMER,则只需启动和关闭定时器,并定义一个定时器处理函数
开启定时器 SetTimer(1,1000,NULL);//定时器号为1,每一秒钟产生一次(以毫秒为单位)
关闭定时器 KillTimer(1); //关闭ID的1的定时器
从"class wizard"里添加对wm_timer的响应函数,并区分不同的消息号即可。
UINT SetTimer(
HWND hWnd, // 接收定时器消息的窗口句柄
UINT nIDEvent, // 定时器的ID号
UINT uElapse, // 定时时间
TIMERPROC lpTimerFunc // 指向消息处理函数的指针
);在mfc中,由于是在cwnd或其子类中调用的且cwnd类中也有SetTimer函数的封装,定在mfc里调用SetTimer时不用传递窗口句柄,也就是有另一种SetTimer形式:
UINT SetTimer( UINT nIDEvent, UINT nElapse, void(CALLBACKEXPORT* lpfnTimer)(HWND, UINT, UINT, DWORD) );
关闭定时器也就对应有两种形式:
BOOL KillTimer(
HWND hWnd, // handle of window that installed timer
UINT uIDEvent // timer identifier
);BOOL KillTimer( int nIDEvent );
- 已标记为答案 Rob Pan 2011年3月28日 1:43