LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Pesistent SubVI

Solved!
Go to solution
I want to make my own sub-vi that has the persistent properties of the "Elapsed Time" vi that is in the functions=>programming=>timing palette.  This sub-vi has an infinite loop, so the expected behavior is that once you call the vi, the execution of your program should halt and hang.  But somehow, a snapshot of the outputs of the vi are captured and the main program continues, while the subvi continues to run in the background until the next time you call it.  Is this something that is only possible because it was created by NI, or can I make my own sub-VI that has this behavior?
Download All
0 Kudos
Message 1 of 5
(3,128 Views)
Solution
Accepted by topic author j_osh_o

No, you don't have an infinite loop in your subVI.  It is a 1 iteration loop because you have a True constant wired to the Stop terminal.  So it runs only once each time it is called.

 

Why do that?  It uses uninitialized shift registers on the loop.  It gives the ability to store state in the subVI between each time it is called.

Message 2 of 5
(3,124 Views)
For further information: The use of shift registers like that is known as a functional global, which is what we old folks used before LabVIEW 3 introduced global variables. They actually are better than global variables in many ways. They are also the basis for Action Engines.
0 Kudos
Message 3 of 5
(3,103 Views)
Fifteen minutes after I posted this question I read the "weekly nugget" about action engines so now I see what is happening.  Part of my problem was that I thought about sub-vis as subroutines (a la Perl) where all the local variables go out of scope and are destroyed when the function returns.  Sub-vis are not really functions, they are more like objects that are created when the parent VI runs.  I suppose you could make an indicator on the front panel of a sub-vi that was not connected tot he connector pane, and use it for data storage just like an un-initialized shift register, although I suspect the style police would not approve.
0 Kudos
Message 4 of 5
(3,091 Views)
While creating an indicator for that is possible, it's not so much style as functionality. In LabVIEW there are no variables in the same sense as text-based programming languages. Data is carried on wires, so wires are the closest that you have to variables. That's why you don't need to create a front panel object just to "store" data. Front panel object serve to provide inputs to the VI (controls), and to provide outputs (indicators). Indicators are also used to show the values of certain points in the code, in other words the data that exists on a wire. So if you wanted to know, for example, what iteration you were on in a for-loop you could create an indicator and wire it to the iteration terminal. The data is actually on the wire. The indicator is merely displaying the data for your consumption.
0 Kudos
Message 5 of 5
(3,076 Views)