LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Canceling a subvi from the main vi

Solved!
Go to solution

I have a program that has a subvi which takes a while to execute as some equipment needs to move.  How do I add in a cancel button that links to the main VI?  The machine is being operated by a conditional for loop in the sub vi but it seems to only be reading in the initial condition of the cancel button and not checking it during the process.

0 Kudos
Message 1 of 5
(835 Views)

Hi mhg,

 

you might use a reference to your cancel button to read from inside the loop of your subVI.

 

But that also sounds like a bad program design: when you want to communicate between several VIs running in parallel then there are bettwer ways like queues, notifiers or channels…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 2 of 5
(830 Views)

Here are a couple of ways.

The second one has the advantage that no matter what causes the SubVI to stop running, the main VI will continue to run.

0 Kudos
Message 3 of 5
(801 Views)

@mhg_v wrote:

 but it seems to only be reading in the initial condition of the cancel button and not checking it during the process.


Yes, that's data flow.

 

You'll (have to) get used to it 😎.

0 Kudos
Message 4 of 5
(737 Views)
Solution
Accepted by topic author mhg_v

Do you understand the concept of LabVIEW as a "Data Flow" language?  One aspect of this is that a Structure (such as a sub-VI) runs "until everything inside it has run", at which point, it exits.  If you call your sub-VI with, say, a "Stop" button, when you call it, you will pass the value of the Stop Button at the time you call it to the sub-VI, which will read it, and act on it (and, since you probably want the sub-VI to run, you do not call it with Stop = True).  Once the sub-VI is running, changing its value in the caller will have no effect.

 

What you need to do is to (temporarily) suspect this aspect of Data Flow by enabling "asynchronous communication".  In LabVIEW 2016, Asynchronous Channel Wires were introduced to do exactly this.  Instead of looking like "wires", Channel Wires look like "pipes", and encourage flow into and out of Structures (such as sub-VIs, While Loops, Case Statements, etc.).  You know how a "wire" gets into a For or While Loop through a "tunnel"?  Channel Wires lay their "pipes" on top of the loop boundaries, a metaphor for data flowing into and out of the Loop while it is running.

 

You can construct a "Tag" channel that can carry a Boolean (let's call the variable "Stop").  In your Main program, you create a "Stop" Button that you monitor (or put in an Event Structure).  You create a Channel Writer, choosing the Tag Channel (more about that in the next paragraph), and connect it to the Stop Button.

 

Tag Channels "instaneously" transfer data from their Writers to their Readers, "jumping over" exactly one "edge" (of a Structure, in this case, a sub-VI, but it could also be a While Loop or Case Statement).  For communication with a sub-VI, you would create a "Tag In" Control on the sub-VI Front Panel, and in the Block Diagram, you would right-click this Control, and create a Channel Reader (it will ask you "what kind of Channel", and you choose "Tag"). 

 

Now, here's the "Asynchronous Magic".  You need to configure your sub-VI loop "as though" it had an "Abort" button inside its innermost loop.  If you are processing a While Loop, you've got a "Stop" Control.  If you have a For Loop, you can add a Stop Control.  Bring your Tag Reader into the Loop and wire its output so that it will stop the Loop if the output is True (you may need to "or" the Tag "Abort" line with the regular "All Done" signal).

 

So the Tag Channel is special -- it holds the "latest" value.  When you created it, you initialized it with "False".  If you do nothing else, it will stay "False", and you won't "Abort/exit".  If you never press "Abort" on your Main routine, the Tag will just sit there.  But if you ever do press it, it will be "instantly" conveyed to, and "into", the sub-VI you "piped" it to, and the next time your sub-VI gets to its "Stop" condition, it will see the "Abort" signal and will exit.

 

You can learn more about the Tag Channel Wire (with examples and code) by using LabVIEW Help.  [Hmm -- I know I've seen examples and discussion in the Help, but now I can't find it ...  Maybe I'm using too "new" a version, after NI "improved" the documentation].  Ah, I just found a Tag Channel "Example" by looking for Examples and putting in "Channel" to the search.  It shows a Tag Channel conveying two pieces of information to three parallel loops -- one an "out-of-bounds" indicator, and a separate "Stop" signal (the Inventor of Channel Wires, Jeff K, threw in the additional "Stop" Boolean if you needed it).

 

Bob Schor

Message 5 of 5
(680 Views)