LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

using multiple timers

Hi Guys,
 
I need some help. I am trying to use multiple timer in my program. I have a timer for each panel.....in my main panel i have a timer, n a callback for it.....in this callback i hv the code to update the main panel.......now i have a added another panel and another timer......on my main panel when i press the button it goes to the callback, i hide the main panel.....disable main_timer....and enable sub_timer......but the problem with is that the main timer hasnt stopped... below is the callback for the button....
 
int CVICALLBACK buttonCallback (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 switch (event)
 {
  case EVENT_COMMIT:
   SetCtrlAttribute (panelhandle, MAIN_TIMER, ATTR_ENABLED, 0); //disables main panel timer
   HidePanel(panelhandle);          //mainpanel
   DisplayPanel (sub_handle);
   SetCtrlAttribute(video_handle, SUB_TIMER_2, ATTR_ENABLED, 1);  //enables video timer
   GetCtrlVal(panelhandle,MAIN_HEAD_SEL,&head_sel);    //sets the active head sel
   FlushInQ(comport);
   break;
 }
 return 0;
}
int CVICALLBACK SubTimerCallback (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 switch (event)
  {
  case EVENT_TIMER_TICK:
        break;
  }
}
 
Is there any way i can stop the main timer, so stop doing the code in the main timer callback and only do whats in sub_timer...
 
looking forward for a reply.
 
Thanks
 
k1_ke
0 Kudos
Message 1 of 6
(3,843 Views)
If I understand you correctly you have executed buttonCallback, and turned off MAIN_TIMER, but the MAIN_TIMER callback is still executing. 
If that is the case, I would assume that the main timer callback has something in it that calls ProcessSystemEvents().  This allows the buttonCallback to execute, and when finished, it returns to complete the main timer callback.
 
If that is the case, you will want to set a flag when the main timer callback starts to execute and clear it when the main timer callback exits.
Inside the buttonCallback you will want to turn off the main timer then check the flag and if it is set, if it is set, post a deferred event to call your buttonCallback processing routine and return immediately.  This way the already executing main timer callback can complete before swap the panels around.
0 Kudos
Message 2 of 6
(3,834 Views)
k1_ke, couldn't you have one timer only? Since you are not discarding the main panel but simply hiding it, and since apparently the timers are not running concurrently, you could use only the timer in the main panel and a global flag that states the operating condition in the program. The timer callback could then have at the beginning an if statement to decide which code to execute based on the value of the global flag: this guarantees that the timer always executes one and on,ly one section of code depending on the operating condition.
 
If you don't want to use global variable for the panel handles you could use the callbackData for timer control to retain the value of the active panel.
 
The skeleton of such an application could be something like this:
 
Global definitions:
#define    MAIN_P     0
#define    VIDEO_P    1
 
static int   PgmMode;
 
In the main callback set the flag to 0:
PgmMode = MAIN_P;
 
When calling the second panel:
HidePanel (theMainPanel);
// Pass the handle of the second panel to the timer in callbackData variable
SetCtrlAttribute (theMainPanel, PANEL_TIMER, ATTR_CALLBACKDATA, (void *)theVideoPanel);
PgmMode = VIDEO_P;
DisplayPanel (theVideoPanel);
 
When closing the second panel:
PgmMode = MAIN_P;
DiscardPanel (theVideoPanel);
DisplayPanel (theMainPanel);
 
The timer callback:
int   pnl;
if (mod == MAIN_P) {
  // Here the code to update the main panel
  // 'panel' variable holds the handle of the main panel
  // Update the GUI
  SetCtrlVal (panel, PANEL_NUMERIC, .......);
}
else {
  // Here the code for the second panel
  // Obtain the handle of the second panel
  pnl = (int)callbackData:
  // Update the GUI
  SetCtrlVal (pnl, PANEL_2_NUMERIC, .......);
}
return 0;


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 3 of 6
(3,829 Views)

Hi Roberto & mvr,

Thanks for reply. I really appreciate it. Well i looked at mvr's suggestion.....well i wasnt using ProcessSystemEvents so i dont know what was happening....but i tried using Roberto's method.

Roberto, i tried using the flag and having everythign done under one timer control.....well see i hv different .c files for each panel....and variable...moving everything in main.c......then i hv also got array for the labels in my video.uir bt when i compile..it say cannot find VIDEO_LED and so on......

Well basically what i want to do is this.....when i go to the video panel.....i want to stop the timer control for main and edge panel......then here i want to get date from serial using a modbus command...so i send a command once...i get a string of data....(its large...abt 32Kbtyes) and save it to an array.......do u think i can use the InstallComCallback for this too?? or what should i do?? also currently i already use that for getting data for the main panel......pls let me know if u hv suggestion go around this problem.

Thanks for help guys.

k1_ke

0 Kudos
Message 4 of 6
(3,807 Views)

>>well see i hv different .c files for each panel....and variable...moving everything in main.c......then i hv also got array for the labels in my video.uir bt when i compile..it say cannot find VIDEO_LED and so on......

If the compiler can not find VIDEO_LED it may be because you need to add the header file for the uir that has VIDEO_LED defined in it to the file in which you are trying to use it.  If you have panels defined in multiple uirs, you must include the header for each uir in any file that tries to use those panels.

0 Kudos
Message 5 of 6
(3,795 Views)
>>do u think i can use the InstallComCallback for this too??
InstallComCallback will work particularly well if you can send a command to the modbus to get your data, and then use the ComCallback to store that data and set a flag when all the data has arrived.  This requires that you have a way to determine when the entire data block has been received. 
Why this works so well is that it allows your program to continue processing other things, like the user interface, while you are receiving data over the serial port.  It also is very useful if you ever need to move the serial communication into another thread (which is probably a subject better left for another time).
0 Kudos
Message 6 of 6
(3,794 Views)