Mindmajix

Local and Global Variables in LabVIEW

LOCAL VARIABLES

Local variables transfer data within a single VI and allow data to be passed between parallel loops as shown in Figure 4.22. They also break the dataflow programming paradigm. Two ways to create a local variable are right-click on an object’s terminal and select Create»Local Variable. A local variable icon for the object appears on the block diagram as shown in Figure 4.23.

Local variable

Creating a local variable from object terminal

Another way is to select the Local Variable from the Structures palette. Create the front panel and select a local variable from the Functions palette and place it on the block diagram. The local variable node, shown as follows, is not yet associated with a control or indicator. To associate a local variable with a control or indicator, right-click the local variable node and select Select Item from the shortcut menu. The expanded shortcut menu lists all the front panel objects that have owned labels as shown in Figure 4.24. LabVIEW uses owned labels to associate local variables with front panel objects, so label the front panel controls and indicators with descriptive owned labels. Owned label becomes variable name and select whether you want to read or write to the local variable.

Creating a local variable from Structures palette

After you create a local or global variable, you can read data from a variable or write data to it. By default, a new variable receives data. This kind of variable works as an indicator and is a write local or global. When you write new data to the local or global variable, the associated front panel control or indicator updates to the new data. You also can configure a variable to behave as a data source, or a read local or global. Right-click the variable and select Change To Read from the shortcut menu to configure the variable to behave as a control. When this node executes, the VI reads the data in the associated front panel control or indicator. To change the variable to receive data from the block diagram rather than provide data, right-click the variable and select Change To Write from the shortcut menu. On the block diagram, you can distinguish read locals or globals from write locals or globals the same way you distinguish controls from indicators. A read local or global has a thick border similar to a control. A write local or global has a thin border similar to an indicator. When you write to a local variable, you update its corresponding front panel object and when you read from a local variable, you read the current value of its corresponding front panel object. Initialize local and global variables before reading them.

GLOBAL VARIABLES

Global variables are built-in LabVIEW objects. You can use variables to access and pass data among several VIs that run simultaneously. A local variable shares data within a VI; a global variable also shares data, but it shares data with multiple VIs. For example, suppose you have two VIs running simultaneously. Each VI contains a While Loop and writes data points to a waveform chart. The first VI contains a Boolean control to terminate both VIs. You can use a global variable to terminate both loops with a single Boolean control as shown in Figure 4.25. If both loops were on a single block diagram within the same VI, you could use a local variable to terminate the loops.

When you create a global variable, LabVIEW automatically creates a special global VI, which has a front panel but no block diagram. Add controls and indicators to the front panel of the global VI to define the data types of the global variables. Select a global variable as shown in Figure 4.25 from the Functions palette and place it on the block diagram. Double-click the global variable node to display the front panel of the global VI. Place controls and indicators on this front panel the same way you do on a standard front panel. LabVIEW uses owned labels to identify global variables, so label the front panel controls and indicators with descriptive owned labels.

Global variable

Figure 4.26 shows a global variable front panel with a numeric, a string, and a cluster containing a digital and a Boolean control. The toolbar does not show the Run, Stop or related buttons as a normal front panel.

Global variable front panel

After you finish placing objects on the global VI front panel, save it and return to the block diagram of the original VI. You then must select which object in the global VI that you want to access. Right-click the global variable node and select a front panel object from the Select Item shortcut menu. The shortcut menu lists all the front panel objects that have owned labels. You also can use the operating tool or labeling tool to click the local variable node and select the front panel object from the shortcut menu. If you want to use this global variable in other VIs, select Functions»All Functions»Select a VI. By default, the global variable is associated with the first front panel object with an owned label that you placed in the global VI. Right-click the global variable node you placed on the block diagram and select a front panel object from the Select Item shortcut menu to associate the global variable with the data from another front panel object.

The two common problems that globals will cause in an application are race conditions and performance problems. These problems are pretty easy to diagnose and relatively easy to fix once you understand them. A race condition occurs when more than one piece of code needs to update a global and there is no way of ensuring that they take turns and leave the global in a valid state. The solution is to control the access and making the code take turns. The performance problem is that it is also possible to use globals in ways that greatly slow down your VI’s execution. This is very different from a race condition in that the program executes correctly, but it takes longer than it should to complete. There is a very good solution that will actually make the code easier to read at the same time it restores the performance. To speed this up, make a subVI that takes the parameters for indexing the global and returns the data. Local and global variables are advanced LabVIEW concepts. They are inherently not part of the LabVIEW dataflow execution model. Block diagrams can become difficult to read when you use local and global variables, so you should use them carefully.


 

0 Responses on Local and Global Variables in LabVIEW"

Leave a Message

Your email address will not be published. Required fields are marked *

Copy Rights Reserved © Mindmajix.com All rights reserved. Disclaimer.
Course Adviser

Fill your details, course adviser will reach you.