LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

stopping while loop in subvi

I have a subvi that takes in the VISA resource name and error line. There is a while loop that continuously reads from the serial port and queues the data from the serial port. There is another while loop that dequeues the data continuously and processes the data. There is a third while loop that stops when the Stop button is pressed or if the Obtain Queue function has an error. When this third loop finshes, it will then call Release Queue. This will stop the other two loops. This subvi is similar to the Queue Basics vi from the NI examples.

In the main vi, I want to be able to stop the subvi when the main vi' Stop button is pressed. What is the best way to proceed?

I have attached an image on what I tried to do with the main vi. It does not work since the subvi has the three while loops that run continuously until the Stop button in the subvi is pressed.
0 Kudos
Message 1 of 16
(4,184 Views)
Without knowing more about the application the simplest solution is to use a global variable which is a "stop" boolean. If you open up the example finder and search for "globals" you will find two VIs "Display Numbers" and "Generate Numbers" that show how to use a global variable to stop another VI that has a loop running.

Other solutions are possible and I'm sure others will chime in.
0 Kudos
Message 2 of 16
(4,161 Views)
I have fixed the main vi (see mainVI.PNG). I also have attached an image of the third while loop in the subVI (subVIthirdwhileloop.PNG).

I still have not been able to get the subVI to stop when I pressed the stop button on the main vi.
Download All
0 Kudos
Message 3 of 16
(4,153 Views)
You used a shared variable as opposed to a global variable. You don't need to use a shared variable (especially a networked shared variable, which is what you used) - a global variable is sufficient. Replace the shared variable with a global variable.

If it still doesn't work with the global variable then please post your code so that all the loops can be seen.
Message 4 of 16
(4,133 Views)
Thanks for your help! That worked.

Generally, when do you want to use a global variable and when do you want to use the network shared variable?
0 Kudos
Message 5 of 16
(4,113 Views)
A global variable is just a simple data storage mechanism that can be visible to any VI. A shared variable is basically an uber-global variable. It can be single process, networked, or time-triggered. A single process shared variable is basically like a global variable.

There's a ton of stuff that a shared variable can do that a global variable can't. You can read all the gory details here. The shared variable does have the nice advantage of having error in and error out terminals to allow you to control when you read/write the global variable, but if global variables are used for more complicated things typically one would write wrapper VIs to control access to it.

If you were to change the shared variable that you had created to be a single process shared variable then it probably would have worked, but for what you were doing a shared variable was overkill.
0 Kudos
Message 6 of 16
(4,104 Views)
I just wanted to add something here, even though you got this to work with a global variable. You could also just create the queue in the main VI and pass it into the sub VI. Then when you press stop in the main VI, release the queue and respond to the destruction of the queue in the sub VI so that each loop will stop. Personally, I avoid global variables whenever possible and I would use this method.
0 Kudos
Message 7 of 16
(4,100 Views)
Marc A,
I second you,"Always Stay away from Global".Me too...I think the queue solution will take care of this Problem.

CLAD
Certified LabVIEW Architect
Certified TestStand Architect
0 Kudos
Message 8 of 16
(4,084 Views)
I've said this before, and I'll say it again: GLOBAL VARIABLES ARE NOT EVIL. There's nothing wrong with global variables, so long as they're used properly. ANY language element can be misused, be it a global variable, a for-loop, or a break statement. Yes, even "GoTo". To completely disregard global variables because of some generalization is ridiculous.
0 Kudos
Message 9 of 16
(4,081 Views)
Yes Sir Smiley Very HappySemercurio,

You are right but as for me NO NO NO Global I use FG,AE,LV2Global for Global.

Greetings from Germany.Smiley Tongue

CLAD
Certified LabVIEW Architect
Certified TestStand Architect
0 Kudos
Message 10 of 16
(4,070 Views)