Search code examples
labview

How to register a user event defined in a SUB VI(LabVIEW)


I created a SUB VI and defined a user event in it. Then I created another VI, let's call it MAIN VI. I put a SUB VI in the MAIN VI,and I want to register the user event created in the SUB VI to the Event Case in the MAIN VI, what should I do?


Solution

  • You can do this, but it takes effort. What you are attempting to do is generally a bad idea -- as you can see from how much fighting against LabVIEW you have to do in the steps below. But because it is sometimes (rarely) necessary, I'll tell you how to do it. In general, though, when you have to fight this much, you may want to reconsider and let the caller VI be the one that allocates the event refnum and then pass it into the subVI, and only let the subVI generate the events.

    Anyway... here's what you're asking for...

    Option 1

    Step one: In the caller VI, use "Obtain Queue" and create a queue of the user event type (that means you need to drop a constant of the desired event refnum type wired to the "Obtain Queue" element type terminal).

    Step two: Add the queue to the connector pane of the subVI and wire your created queue to the new terminal on the subVI.

    Step three: Modify the subVI so that after you create the user event, use "Enqueue Element" to enqueue that user event into the queue.

    Step four: Back in the caller VI, in parallel to the call to the subVI, do a "Dequeue Element". Now you have the user event in the caller.

    Step five: Use Release Queue in the caller VI to release the queue.

    Step six: In the caller VI, use "Register For Events" to register the user event. Now you are ready to receive the event in an Event structure.

    NOTE: The above process gives no guarantee that the caller VI will register the event before the subVI starts sending events. If the subVI starts sending events before the Register For Events node is called, you'll miss those initial events. You need a second signaling mechanism to tell the subVI "ok, it's safe to proceed".

    Option 2

    Step one: Put a giant case structure around all the code in your subVI. Wire the ? terminal of the case structure with a Boolean control. Add that Boolean control to the connector pane of the subVI. Now all the code for the subVI is in the TRUE case of the structure.

    Step two: In the FALSE case, put the code to create the user event. Create an indicator of the user event and put it on the connector pane.

    Step three: Use a Feedback Node outside of the case structure to store the user event. Do not provide any initialization of the Feedback Node.

    Step three: In the caller, call the subVI with a FALSE wired to its input. The subVI will allocate the event refnum and return it to you. It will also store the refnum in the Feedback Node.

    Step four: Use the Register For Events node to register the event in the caller VI.

    Step five: Call the subVI a second time, this time passing TRUE. It will get the value of the user event refnum from the Feedback Node.

    NOTE: This version DOES provide protection against missing the first events.