09-07-2012 03:37 PM
Is there a better, cleaner way to accomplish the following task? I have a VI that reads and processes data from a device, and I want to turn it into a sub-VI that basically spits out the processed data everytime it is calculated. I can't post my real VI, but I have put together a small example that highlights the bulk of what I'm doing. See the attached files (created using LV 2011).
In my example, I first start the sub-VI and then control and monitor it inside a while loop using global variables. Is this a good way to do this? Are global variables discouraged? This is not exactly what I am shooting for, but it seems to work.
Ideally, I would like to have one icon I can put into a block diagram like the below image. As inputs, this VI (icon) would have the VISA res. name of the device, a Run/Stop switch, and a Kill switch. As outputs, it would have a bunch of numbers that are constantly updated. Is this possible?
09-08-2012 03:37 AM
Yes, there are definitely several better and cleaner ways.
First of all, your seem to have some misconceptions about dataflow.
@tlawren wrote:
In my example, I first start the sub-VI and then control and monitor it inside a while loop using global variables.
There is no "first". Your subVI call and the while loop have no data dependency, thus there is no way to tell which one starts first (... and it does not really matter.) Also your toplevel while loop is a CPU burner, because it polls and writes the globals as fast as the CPU allows, millions of times per second, pegging one CPU core at 100%. Not good. Since the subVI is paced at 500ms, you don't need to go that fast. Place a wait!
@tlawren wrote:
I have a VI that reads and processes data from a device, and I want to turn it into a sub-VI that basically spits out the processed data everytime it is calculated. I can't post my real VI, but I have put together a small example that highlights the bulk of what I'm doing. See the attached files (created using LV 2011).
How accurately does this example reflect your actual VI? Do you also have two loops linked by queues? Does all this need to be in the subVI, or could you fill the queue in the subVI while reading the queue in the calling VI?
If you want to use it the way you show in your picture, you cannot have any loops in the subVI so it can always return immediately.
Can you tell us a little more what else needs to be going on in your VI? Why do you need a subVI at all?
09-08-2012 08:35 AM - edited 09-08-2012 08:38 AM
"First of all, your seem to have some misconceptions about dataflow."
Yes, I'm text-based programmer that is still pretty new to LabVIEW, so dataflow often throws me.
"How accurately does this example reflect your actual VI?"
With respect to the movement of data, I think it is pretty close. In my real VI, I actually have three loops. The first is a producer loop that reads data from my device and enqueues it for a consumer loop, the second loop. The consumer processes and saves the data as well as enqueue data for the third loop that writes data back to my device. The writing occurs far less frequently than the reading. I'm not processing that much data (~100 bytes) and my devices operates at 1-10 Hz.
"Can you tell us a little more what else needs to be going on in your VI? Why do you need a subVI at all?"
I'm trying to develop some LV code (set of VIs and sub-VIs) that other people can use to interface with my device. I want to able to connect to my device and collect and process data from it. I'm not sure how it is going to be used, so I'm trying to develop something that is simple. That is why I'm thinking about a sub-VI, a VI or set of VIs that be used inside another VI. If I'm approaching this in a bad way, then please tell me.
09-10-2012 08:52 AM - edited 09-10-2012 08:54 AM
Are there any good tutorials or help sections that discuss what I'm trying to do?
09-11-2012 05:58 PM
If you have a service contract with National Instruments you have access to Self-Paced Online Training for LabVIEW. I would recommend looking into it. It will help you get started using LabVIEW and clear up confusion on how to set up the data flow structure. See http://sine.ni.com/myni/self-paced-training/app/main.xhtml.
More specifically to your post, you can get the same functionality of your examples using just the subVI if you put the booleans into it instead of referencing variables. To set the input and output of a subVI use the connector pane in the top right corner of your Front Panel.