LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How to get an event signal triggerd by TIMER (under user inferface) by using GetUserEvent()

Solved!
Go to solution

Hi,

 

I want to use GetUserEvent() inside a loop for polling TIMER event, instead of using RunUserInterface() and TIMER calllback function.

 

but it seems GetUserEvent() can't get TIMER event. Are there any thing that I should notice before using GetUserEvent()?

 

 

I use a command button(PANEL_Add) to switch TIMER ON/OFF, the time interval is 1 second and TIMER is initially OFF.

 

PANEL_LED shows the status of TIMER ON/OFF flag.

 

PANEL_LED did turn on/off after I click the command button, but the counter value didn't change anyway while TIMER is enabled.

 

Thanks for answering~ 

 

 

Here is my code

================================================================== 

int main (int argc, char *argv[])
{
    int error = 0;
    int done  = FALSE;
    int eventPanel;
    int eventCtrl;
    int panelHandle;
    unsigned int Count=0;
    int flag=0;
 
    /* initialize and load resources */
    if (InitCVIRTE (0, argv, 0) == 0)
        return -1;
    if ((panelHandle = LoadPanel (0, "Timer.uir", PANEL)) <= 0)
        return -1;
 
    /* display the panel and run the user interface */
   DisplayPanel (panelHandle);
   while (done == FALSE){
        
         GetUserEvent (1, &eventPanel, &eventCtrl);

        

         switch (eventCtrl){
                case PANEL_Add : flag = (flag==TRUE)? FALSE:TRUE;
                                            SetCtrlAttribute (panelHandle, PANEL_TIMER, ATTR_ENABLED, flag);
                                            SetCtrlVal(panelHandle, PANEL_LED, flag);
                                            break;


                case PANEL_TIMER : SetCtrlVal(panelHandle, PANEL_Counter, Count++);
                                                if(Count > 19)
                                                       Count = 0; 
                                                break;         
          }
     }


     /* clean up */
    DiscardPanel (panelHandle);
    CloseCVIRTE ();
    return 0;
}

 

 

and here is the .h file

==================================

#include <userint.h>

 

#ifdef __cplusplus
    extern "C" {
#endif

 

#define  PANEL                            1
#define  PANEL_Counter              2     
#define  PANEL_Add                    3     
#define  PANEL_LED                    4     
#define  PANEL_TIMER                 5

 

#ifdef __cplusplus
    }
#endif

0 Kudos
Message 1 of 7
(3,915 Views)
Solution
Accepted by topic author morphous

Hi,

 

according to the help GetUserEvent obtains the next commit event or programmer-defined event from the GetUserEvent queue. A commit event occurs when the user changes the state of a hot or validate control or selects a menu item.

Place programmer-defined events in the GetUserEvent queue by calling QueueUserEvent.

 

Because the timer does not generate a commit event, you need to call QueueUserEvent; for this you will need to use the TimerCallback function that you didn't like... I see no other way than using the callback function of the timer

 

Message Edited by Wolfgang on 03-19-2010 09:40 AM
0 Kudos
Message 2 of 7
(3,911 Views)

Thanks for answering~~

 

But this lead me to another question.

  

 

 

For testing QueueUserEvent(), I have modified my code and now it look likes the following

=========================================================

#define MyEvent1 1001

 

 

inside TIMER callback function

==========================

switch (event)
 {
    case EVENT_TIMER_TICK:  QueueUserEvent(MyEvent1, panelHandle, PANEL_TIMER);

                                             break;
 }
 return 0;

 

 

and now the polling loop becomes

 ==============================================

GetUserEvent (1, &eventPanel, &eventCtrl);

       

switch (eventCtrl){
                            case PANEL_Add : flag = (flag==TRUE)? FALSE:TRUE;
                                                        SetCtrlAttribute (panelHandle, PANEL_TIMER, ATTR_ENABLED, flag);
                                                        SetCtrlVal(panelHandle, PANEL_LED, flag);
                                                        break;
                            case MyEvent1 :   SetCtrlVal(panelHandle, PANEL_Counter, Count++);
                                                        if(Count > 19)
                                                               Count = 0;
                                                        break;          
}

 

 

what I thought is this should insert a new event into queue everytime after TIMER callback been executed,

 

and GetUserEvent() shold be able to get the event in polling loop.

 

But it didn't.

 

Did I miss anything again?

0 Kudos
Message 3 of 7
(3,902 Views)

I don't think that this is the most straightforward solution.... Why don't you put your statement into the timer callback, i.e.

 

==========================

switch (event)
{
    case EVENT_TIMER_TICK: 

        SetCtrlVal(panelHandle, PANEL_Counter, Count++);
        if(Count > 19) Count = 0;
    break;
}

 

 

This makes life much more simple 🙂

Message Edited by Wolfgang on 03-19-2010 11:01 AM
0 Kudos
Message 4 of 7
(3,896 Views)

yes, you are right

 

the code you post is the exact code I wrote at the first place~

 

 

To be curious, I just want to test how to use QueueUserEvent()  but nothing else. Smiley Tongue

0 Kudos
Message 5 of 7
(3,891 Views)

ok, in this case you need tp check the event status by using event_status = GetUserEvent (1,.. ,.. ) ...

 

from the help:

 

The event, if any, that occurred.

0No event.
1Commit event occurred.
1,000-10,000Event queued by QueueUserEvent.

0 Kudos
Message 6 of 7
(3,884 Views)

Be warned: if your panel has a menu bar your program will stay inside GetUserEvent() if the mouse cursor is sitting on a menu or menu item until the mouse is moved away or a menu item has been selected

(stupid tracking loop) .

-----------------------
/* Nothing past this point should fail if the code is working as intended */
0 Kudos
Message 7 of 7
(3,874 Views)