用两个多媒体定时器,一个定时0.1秒计时,另一个定时0.01秒用来对模拟量信号采集每秒100次。
1、在SEAL_PRESSUREDlg.h中添加代码
class CSEAL_PRESSUREDlg : public CDialo
{
public:CSEAL_PRESSUREDlg(CWnd* pParent = NULL); // standard constructor...UINT timerID;//自己添加的定时器ID变量 void DestroyTimer();//自己声明的销毁定时器函数UINT CreateTimer();//自己声明的创建定时器函数void OnTimer_1(UINT nIDEvent_1);static void CALLBACK TimeProc(UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2);//定时器回调函数int t_time;UINT timerID_2;//自己添加的定时器ID变量 void DestroyTimer_2();//自己声明的销毁定时器函数UINT CreateTimer_2();//自己声明的创建定时器函数void OnTimer_2(UINT nIDEvent_2);static void CALLBACK TimeProc_2(UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2);//定时器回调函数double t_time_2;...}
2、在SEAL_PRESSUREDlg.cpp中添加代码
void CSEAL_PRESSUREDlg::OnTesStart()
{if (m_isStop){m_TES_START.SetIcon(IDI_TES_START); m_TES_START.SetAlign(CButtonST::ST_ALIGN_VERT);((CButton *)GetDlgItem(IDC_TES_START))->SetWindowText(m_isStop ? "结束测试" : "开始测试"); DestroyTimer();DestroyTimer_2();m_isStop=0;}else{ t_time=0;t_time_2=0.0;m_TES_START.SetIcon(IDI_TES_STOP); m_TES_START.SetAlign(CButtonST::ST_ALIGN_VERT);((CButton *)GetDlgItem(IDC_TES_START))->SetWindowText(m_isStop ? "结束测试" : "开始测试");// CTime m_time; // m_time=CTime::GetCurrentTime(); //获取当前时间日期// CString m_strDateTime=m_time.Format(_T("%Y-%m-%d %H:%M:%S %A")); //格式化日期时间// CString str=m_time.Format("%Y_%m_%d "); //格式化日期时间// theApp.time_name=theApp.typ_name+"\\"+str+"\\"; SYSTEMTIME SystemTime;::GetLocalTime(&SystemTime);CString str1="",Date="";str1.Format("%02d",SystemTime.wYear);Date = Date + str1;str1.Format("_%02d",SystemTime.wMonth);Date = Date + str1;str1.Format("_%02d",SystemTime.wDay);Date = Date + str1;
// str1.Format("_%02d",SystemTime.wHour);
// Date = Date + str1;
// str.Format("_%02d",SystemTime.wMinute);
// Date = Date + str1;
// str.Format("_%02d",SystemTime.wSecond);
// Date = Date + str1; theApp.time_name=theApp.typ_name+Date+"\\";if (!PathIsDirectory(theApp.time_name)) //目录不存在{ // MessageBox("文件不存在");CreateDirectory(theApp.time_name, FALSE);//不存在则创建cre_data();}else //目录存在{// MessageBox("文件存在");cre_data();}SetDlgItemText(IDC_STATIC99,theApp.time_name);CreateTimer(); CreateTimer_2(); m_isStop=1;}}UINT CSEAL_PRESSUREDlg::CreateTimer()
{timeBeginPeriod(1);//设置定时器设备的最小时间分辨率timerID = timeSetEvent(100, 1, TimeProc, (DWORD)this, TIME_PERIODIC);return timerID;//返回定时器ID// timeSetEvent(UINT uDelay, UINT uResolution, LPTIMECALLBACK IP TimeProc, DWORD_PTR dwUser, UINT fuEvent);
//创建并初始化定时器事件,定时器回调函数入口地址
//uDelay:定时器触发时间间隔,以毫秒为单位
//uResolution:定时器设备精度,以毫秒为单位,,默认为1ms
//LpTimeProc:定时器出发时间的回调函数的地址
//dwUser:传递给回调函数的数据
//fuEvent:定时类型,TIME_ONSHOT表示uDelay毫秒后只产生一次事件,TIME_PERIOFIC表示每隔uDelay毫秒周期性的产生事件}void CALLBACK CSEAL_PRESSUREDlg::TimeProc(UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2)
{CSEAL_PRESSUREDlg* time_dlg = (CSEAL_PRESSUREDlg*)dwUser;time_dlg->OnTimer_1(1);//uID:多媒体定时器的ID,ID值由timeSetEvent创建定时器事件时返回//uMsg:保留 不使用//dwUser:由timeSetEvent传递的用户数据//dw1,dw2:保留 不使用
}void CSEAL_PRESSUREDlg::OnTimer_1(UINT nIDEvent_1)
{if (nIDEvent_1==1 && t_time<=theApp.m_sys_data.m_fMaxTime*10){t_time++;CString str_t2_6,str_t2_5,str_t2_4,str_t2_3,str_t2_2,str_t2_1;int t2=t_time; str_t2_5.Format("%d",t2%10);str_t2_4.Format("%d",(t2/10)%10);str_t2_3.Format("%d",(t2/100)%10);str_t2_2.Format("%d",(t2/1000)%10);str_t2_1.Format("%d",(t2/10000)%10); SetDlgItemText(IDC_STATIC4,str_t2_5);SetDlgItemText(IDC_STATIC3,".");SetDlgItemText(IDC_STATIC2,str_t2_4); SetDlgItemText(IDC_STATIC1,str_t2_3);SetDlgItemText(IDC_STATIC0,str_t2_2);}}void CSEAL_PRESSUREDlg::DestroyTimer()
{timeKillEvent(timerID);//销毁定时器timeEndPeriod(1);//清除上次调用 timeBeginPeriod 函数时指定的最小计时器分辨率
}UINT CSEAL_PRESSUREDlg::CreateTimer_2()
{timeBeginPeriod(1);//设置定时器设备的最小时间分辨率timerID_2 = timeSetEvent(10, 1, TimeProc_2, (DWORD)this, TIME_PERIODIC);return timerID_2;//返回定时器ID}void CALLBACK CSEAL_PRESSUREDlg::TimeProc_2(UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2)
{CSEAL_PRESSUREDlg* time_dlg_2 = (CSEAL_PRESSUREDlg*)dwUser;time_dlg_2->OnTimer_2(2);}void CSEAL_PRESSUREDlg::OnTimer_2(UINT nIDEvent_2)
{ if (nIDEvent_2==2 && t_time<=theApp.m_sys_data.m_fMaxTime*10){t_time_2++;double YRandom;//随机数YRandom = rand()%5; // Generate Random data m_ctrlPlot_1.GetChannel(0).AddXY(t_time_2/100, YRandom );} }void CSEAL_PRESSUREDlg::DestroyTimer_2()
{timeKillEvent(timerID_2);//销毁定时器timeEndPeriod(1);//清除上次调用 timeBeginPeriod 函数时指定的最小计时器分辨率
}
运行程序点击开始试验