Programming LabView - Structures Part 1 (Intro and While-loop)

Without structures, LabView would be just a mesh of wires with no way to control the order and timing of the things that occur. However, structures make LabView a true programming language. For those with experience programming in languages such as C, LabView structures are essentially identical to flow control or conditional statements.

All LabView structures are placed on the block diagram by dragging a box. You left click at one corner, drag the mouse, and release at the opposite corner. The area in the box is now partitioned from the area outside of the box. In a sense there are multiple block diagrams (at least from a dataflow standpoint) in the one piece of code.

The menu to access LabView structures is shown below:

Let's just dig right in to an example:

This code shows a block diagram, which just like previous examples has one input and one output. In between is a while structure. This structure contains just the grey rectangle, and the "i" and stop terminals. The portion of the block diagram inside a while loop will run until TRUE is sent to the stop terminal. It will simply start over within the confines of that rectangle, if FALSE is sent to the stop terminal. The 'i' terminal always puts out how many times the while loop has repeated starting with 0. If FALSE is received on the first iteration of the while loop, then 'i' will send out 1 as its value for the second iteration. On the third iteration, if it is reached, the 'i' terminal will send 2. The squares on the periphery of the while loop where values come in and leave the structure are called "tunnels". The leftmost tunnel happens to be an input tunnel and the rightmost tunnel happens to be an output tunnel. The value coming into the while loop on an input tunnel will stay the same for every iteration of the while loop. The value going out of the while loop on the output tunnel will only be sent once after the last iteration (when TRUE is received by the stop terminal).

So what does this code do? It iterates multiple times in the while loop, until the value from the index 'i' terminal is greater than the value on the front panel control wired to the input tunnel. The last value of 'i' on the final iteration is sent out of the while structure to the indicator on the front panel.

With a while loop it is up to the developer to get that TRUE/FALSE (also known as boolean) value to go to TRUE eventually. If this does not happen, then the VI could get stuck in an infinite loop.

With structures, the tunnels are a key entry and exit point. All dataflow is conducted into and out of structures looking at the tunnels as nodes that tie the structures to the rest of the block diagram. Once all the input nodes have their values, the structure will begin to execute the portion of the block diagram within the structure to completion. Once it completes, any output tunnels will contain the results of the processing within the structure.


mekkis said...

I was doing well until this point.

I have some mild background in programming, and am familiar with basic conditional loops. However, I am not able to logically connect the function of the while loop in your example with the diagram provided.

I'll netsearch a little on flowcharting to see if the > inside the logical OR box has some meaning I've not encountered with my small experience, but if this is a true introductory for beginners you might want to touch on this aspect.

Tony Young said...

The triangle in the middle of the while loop is actually a comparison between the two values wired in. If the top value is greater than the bottom value, TRUE is output. The diagram would be similar to the C code:

i = 0;
while(!(i > num))
num2 = i;

There was an error in this gadget