11-15-2012 10:53 AM
I have two while loops in my program; one reads data and the other does nothing until I hit stop on the first one. I want the first one to always run and the second to run off of the time constraint I have for it when I hit the start button. I am using a DAQ and transfering data from one while loop to the other. See attached file.
Main questions:
How to attach the start button?
How to get the second loop to work with the first?
Thank you
Solved! Go to Solution.
11-15-2012 10:58 AM
Your problem is that the loops have a data dependency. If you want to run both loops at the same time, they can't have any wires running from one to the other. Look into the Producer/Consumer architecture. It uses queues to send data from one loop to the other.
11-15-2012 12:39 PM
In your code one loop output feeds the next loop. This is a data dependency.
For small applications or simple constructs you could also cheat a bit and use local
variables to pass data from one loop to the next as they both operate simultaneously.
Just be aware that local variables can introduce race conditions if not handled with care.
You are responsible to insure this does not happen.
Just be very careful to
1)Assure proper initialization of the local first before using them anywhere in your VI
2) Insure no two code sections write to a local variable at the same time. This is your responsibility.
3) reset the locals before exiting the VI
A queue would handle most if not all of this responsibility for you but slightly more tedious to set up and master.
If you have little time you can always cheat to get what you need at the expense of
scale ability later.
Its your choice but. Thats why LabVIEW is so popular.
11-15-2012 12:52 PM - edited 11-15-2012 12:52 PM
Let me speak from experience. Locals are very rarely ever the way to go. And this is not one of them. You will run into race conditions and you will lose data. And queues are not difficult to use.
11-15-2012 12:57 PM
Thanks again, I got it to work easily by using the producer/consumer pattern.
11-19-2012 10:39 AM
I am learning labview as I go and little errors keep stumping me. I set up the producer/consumer design and it reads the two while loops like I want. However, when I stop the program to save it to the write to measure it gives me an error saying invalid input and highlights the dequeue. I have to queues running (one for a butotn and one for data) and they both do this. I tried connecting it different ways but its always the same error. Any ideas?
11-19-2012 12:43 PM
Since your lower loop has a dequeue with no timeout (-1), it is waiting for an element. If the queue is destroyed before it gets one, it throws an error. Since you don't have the error output of the dequeue wired to anything, automatic error handling in labview kicks in and pops up the error. You can actually use this to your advantage. Instead of the second queue to handle the button, place a case structure around all your code in the second loop that comes after the dequeue. Wire the error output of the dequeue to it so that code only fires when there is no error from the dequeue. Also branch the error wire straight to the while loop conditional terminal. This will automatically stop the lower loop when the queue is destroyed. This should eliminate the need for all the hidden stop buttons.
07-03-2019 08:18 AM
1. Create a stop Control on the Stop condition in the first loop.
2. Create a Local Variable of the Stop Button in the first loop
3. Put the local variable the second loop and wire it to the stop condition.
Voila. You fooled the system xD
I also struggled with this problem
07-03-2019 08:21 AM
Hi darkieboy,
congratulations on solving a 7 year old "problem"…
3. Put the local variable the second loop and wire it to the stop condition.
Voila. You fooled the system
Yes, sure you "fooled" around…
What happens when you want to refactor your VI and move that second loop into its own (sub)VI? Now you fooled yourself!
07-03-2019 08:27 AM
I started working in LabView 4 days ago. I simply need a solution for this. I read about problems with this thing, but for a simple application is enough. I did not studied the queues.
Thank you. It is also my first contact with this type of programming. It s so different from the traditional text based(C/C++)