labwindows 异步定时器能开多个吗
发布网友
发布时间:2022-05-16 08:06
我来回答
共1个回答
热心网友
时间:2023-10-16 04:21
//*********************************************************
// Labwindows/cvi8.5 同步/异步定时器的使用2
// szlihongtao
// 2010-06-16
// 参照 王建新的<Labwindows/cvi测试技术及工程应用> page312
// 稍有改写
//*********************************************************
#include <utility.h>
#include <cvirte.h>
#include <userint.h>
#include "toolbox.h"
#include "asynctmr.h"
#include "asy.h"
//*********************************************************
#define TB 0.02 // 定时器的间隔周期
//设置异步定时器的callbackData参数
static void *asyncCBData = "这是一个异步定时器示例!5555555555";
//设置同步定时器的callbackData参数
static void *syncCBData = "这是一个同步定时器示例!4444444444";
static int timerid; //异步定时器句柄
static int panelHandle;
int CVICALLBACK asynCB (int reserved, int timerId, int event, void *callbackData, int eventData1, int eventData2);
//*********************************************************
int main (int argc, char *argv[])
{
if (InitCVIRTE (0, argv, 0) == 0)
return -1; /* out of memory */
if ((panelHandle = LoadPanel (0, "asy.uir", PANEL)) < 0)
return -1;
timerid = NewAsyncTimer (TB, -1, 0, asynCB, asyncCBData); //创建一个异步定时器
DisplayPanel (panelHandle);
RunUserInterface ();
DiscardAsyncTimer (timerid); //释放异步定时器
DiscardPanel (panelHandle);
return 0;
}
//*********************************************************
int CVICALLBACK start (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
int mode;
double ddd;
switch (event)
{
case EVENT_COMMIT:
{
GetCtrlVal (panelHandle, PANEL_BINARYSWITCH, &mode);
if (mode) //设置异步定时器的ENABLED属性
{
SetAsyncTimerAttribute (timerid, ASYNC_ATTR_ENABLED, 1);
SetAsyncTimerAttribute (timerid, ASYNC_ATTR_INTERVAL, TB);
SetCtrlAttribute (panelHandle, PANEL_TIMER, ATTR_ENABLED, 0);
}
else //传递同步定时器的callbackData参数
{
SetAsyncTimerAttribute (timerid, ASYNC_ATTR_ENABLED, 0);
SetCtrlAttribute (panelHandle, PANEL_TIMER, ATTR_ENABLED, 1);
SetCtrlAttribute (panelHandle, PANEL_TIMER, ATTR_INTERVAL, TB);
}
//设置输入模式,与设置控件的dimmed属性类似
SetInputMode (panelHandle, PANEL_CMD_START, 0);
break;
}
}
return 0;
}
//*********************************************************
int CVICALLBACK QuitCallback (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
QuitUserInterface (0);
break;
}
return 0;
}
//*********************************************************
int CVICALLBACK stop (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
{
SetCtrlAttribute (panelHandle, PANEL_CMD_START, ATTR_DIMMED, 0);// 允许再次启动
SetAsyncTimerAttribute (timerid, ASYNC_ATTR_ENABLED, 0);
SetCtrlAttribute (panelHandle, PANEL_TIMER, ATTR_ENABLED, 0);
break;
}
}
return 0;
}
//*********************************************************
int CVICALLBACK timer (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
double yarray[2];
switch (event)
{
case EVENT_TIMER_TICK:
{
yarray[0] = Random (0, 100); //产生0-100之间的随机数 ;
PlotStripChart (panelHandle, PANEL_STRIPCHART, yarray, 1, 0, 0, VAL_DOUBLE);
SetCtrlVal (panelHandle, PANEL_TEXTMSG,syncCBData);
break;
}
}
return 0;
}
//*********************************************************
int CVICALLBACK asynCB (int reserved, int timerId, int event,
void *callbackData, int eventData1, int eventData2)
{
double yarray[2];
switch (event)
{
case EVENT_TIMER_TICK:
{
yarray[0] = Random (0, 100);
PlotStripChart (panelHandle, PANEL_STRIPCHART, yarray, 1, 0, 0, VAL_DOUBLE);
SetCtrlVal (panelHandle, PANEL_TEXTMSG, (char *)callbackData);
break;
}
}
return 0;
}
//*********************************************************
//*********************************************************