LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

how to pass a value from one loop to another

I have two separate while loops in my Labview vi. Is there a way to pass vale within one while loop to the other one? Both run parallely. It is not a loop within a loop, but two separate loops.
 
Thanks and regards,
Ravi.
0 Kudos
Message 1 of 18
(6,132 Views)

Hi,

You can use, for instance queues to do that.

Look at attachment,
Hope this helps,
Paulo
0 Kudos
Message 2 of 18
(6,122 Views)
Hi
 
Using queues is a good way to exchange data between two different loops. I think you could also use notifiers, but I'm not so used to them.
 
Thomas
Using LV8.0
--------------------------------------------------------------------
Don't be afraid to rate a good answer... 😉
--------------------------------------------------------------------
0 Kudos
Message 3 of 18
(6,123 Views)

I like using queues, notifiers have a few limitations, the do not work remotely (probably not a problem) and notifiers do not buffer sent messages, acting like a lossy queue.  I would avoid the temptation to use local or global variables since race conditions occur in concurrent loops.  Queues are surprisingly easy to use in labview and have saved me many times.  I am not sure about overhead associated with queued communication but I have never had a problem using them even in DAQ or processor intensive application.  In addition you can name the queue which allows easier debugging of error conditions.

-Paul

Paul Falkenstein
Coleman Technologies Inc.
CLA, CPI, AIA-Vision
Labview 4.0- 2013, RT, Vision, FPGA
Message 4 of 18
(6,114 Views)
If you have version 7.0+ you can look at the producer-consumer design pattern template, most of the work for your design will be done for you.
 
 
Paul Falkenstein
Coleman Technologies Inc.
CLA, CPI, AIA-Vision
Labview 4.0- 2013, RT, Vision, FPGA
0 Kudos
Message 5 of 18
(6,112 Views)
Just a note...  Notifiers would generally be a poor choice for two parallel running loops, as whatever is "Waiting on Notification" will cause the whole while loop to hang on that part of the code until it receives said notification.  This consequence can however be very useful if you only want one of the loops running at some time you specify.
0 Kudos
Message 6 of 18
(6,102 Views)
You could run into the same problem using queues - depending on the timeout you set.
 
Using LV8.0
--------------------------------------------------------------------
Don't be afraid to rate a good answer... 😉
--------------------------------------------------------------------
0 Kudos
Message 7 of 18
(6,100 Views)
I believe you could also just use local variables depending on the use?
Ryan

LV 7.1
0 Kudos
Message 8 of 18
(6,099 Views)

Hi esa_paranoid,

 

I agree with falkpl who wrote

"I would avoid the temptation to use local or global variables since race conditions occur in concurrent loops".

 

1) Queues are fast and efficient. Good if your data starts in one or more places and end ups in one place. Example: Commands to a "robot controller" that has to respond to requests from more than one client. These do NOT work across networks with multiple machines.

2) LV2 Globals, Not as fast as queues but handle multiple readers. Example: Multiple tasks need to know the status of safety interlocks. Can be shared across multiple machines.

 

Ben

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
Message 9 of 18
(6,090 Views)
Sorry, I disagree with the statement

"I would avoid the temptation to use local or global variables since race conditions occur in concurrent loops".

Race conditions occur when using locals within one loop.  When using parallel loops, local variables are an excellent and very easy way to send data from one loop to another.  Of course, improper programming could always cause problems no matter what you use, queues or locals.  Here is an example of using a local properly between two loops.  If you want both loops to stop when the user presses the stop button, one loop contains the stop button and the other loop contains a local of the stop button.  Nothing wrong with that.  Using queues instead of locals to send data to another loop is more complex, harder to code, yet it performs the same function and the same risks are involved with timing.  Both loops are running independently.  There is no way to guarantee that one loop will write to the queue before the other loop reads the queue.  So what is the difference if you are writing to a queue in one loop and reading the queue in another or if you are writing to a local in one loop and reading the local in another?  Also, if you use queues, once you read the queue in the second loop, it is empty.  You have to do special coding to retain the last value dequeued.  With locals, there is always a value defined.  I have attached an example of using locals with two parallel loops and I doubt that anyone can find anything wrong with this example.

I am not trying to encourage the use of locals everywhere, but they do have a use if used properly.  Locals offer an easy exchange of data between parallel loops.  If the programmer knows what he is doing, use them.  Don't be scared to try them.  I wish the experts here would stop scaring newcomers away from locals and globals like they are the plague.  Instead, educate newcomers on how to avoid race conditions, and how to properly use locals and globals.  Teach good programming, don't hide your head in the sand to avoid a potential problem.  If newcomers are taught how to avoid the problems that may come with locals and globals, they will have become better programmers and will have more tools to work with.

- tbob

Inventor of the WORM Global
Message 10 of 18
(6,071 Views)