In this tutorial, I have explained the working of global variables. Global variables are variables or controls in LabView that can be used globally i.e. throughout the program including the main VI and all its respective subVIs. Firstly a brief introduction of global variables is given along with its comparison with text based programming language after that the use of global variables is explained with the help of a VI, and I tried to explain the working of global variables under different scenarios. At the end of the tutorial you are provided with an exercise to do it by yourself, and in the next tutorials I will assume that you have done those exercises and I will not explain the concept regarding them.
Introduction to global variables in labview
A variable with global scope, which means that it will be visible and accessible throughout the program included all its classes or subroutines unless shadowed is known as a global variable in computer programming. Global environment or global state is simply a set of global variables. Global variables are generally static variables, in compiler language; whose extent is the entire runtime of the program including all its subroutines, though in interpreted languages, global variables are generally dynamically allocated when declared, since they are not known ahead of time.
In some programming languages, by default all variables are global; however variables have limited scope in many of the modern programming languages, generally lexical scope, though declaring a variable at the top level of the program, is the requirement of in global variables. Global variable don’t exists in some of the programming languages. Modular programming language is the basic cause of the existence of global variables. It is the modules or a class or subroutine based object-oriented programming that enforces class structure.
In LabView global variables are same as that of other text based or pictorial programming language. This is the only factor so far that has the same name in LabView as other programming languages. It has the same working principle as a simple global variable possesses. Defining a global variable in the main VI, can results in its call and use in any of its subVIs. It is explained in depth in the program given below.
Example of using Global variables in labview
- Create a VI as discussed in tutorial 1 and save it for future use as we have done in all the previous tutorials.
- Right click on the block diagram and from the function palette select structures and then select global variables as shown in the figure below.
Figure 1: Placement of global variables
- Select this global variable and place it on the block diagram window. The global variable block is shown in the figure below,
Figure 2: Global variable block
Figure 3: Global variables front panel
- A global variable only have a front panel. It is similar to a subVI with only front panel and no block diagram.
- Save this global variable VI as a separate .vi file as you have learned to save a simple VI in previous tutorials. Right click inside the global variable and place a numeric constant, it has a same control palette as a simple VI front panel has. Refer to the figure below,
Figure 4: Global variable
- You can place as many numeric constants in one global variable as you want, and all these controls can be accessed from the block of the global variable in the block diagram of main VI. Refer to the figure below.
Figure 5: Multiple numeric controls in one global variable
- Now, go to the main block diagram of the VI and click on the global variable block, all the numeric controls will appear that you have placed inside the global variables’ front panel. Select the control you want to access, see the figure below,
Figure 6: Control selection.
- When you hover over the global variable, you can see that it only has a pin on the input side i.e. and no pin to read the data already stored in the global variable. What do to when one want to read the variables already stored in the global variable??. Right click on the global variable and select change to read as shown in the figure below,
Figure 7: Change to read
- This will also change the pin of the global variable from input to the output side, as shown in the figure below.
Figure 8: Pin at output
- Right click on the pin and create an indicator at the output of the variable, as shown in the figure below,
Figure 9: Indicator
- Copy this complete block diagram and paste twice in the same block diagram and change the variable of the global variable of other 2 global variables to Numeric 2 and Numeric 3 as shown in the figure below,
Figure 10: All variables with separate indicators
- Now if you see the main front panel of the main VI you can see all the indicators, these indicators are of the controls places inside the global variables as shown in the figure below,
Figure 11: Main front panel indicators
- Set the global controls to any desired values and run the main VI, the values you gave to the control inside global variables will appear on the main front panels’ indicator as shown in the figure below,
Figure 12: Output
- Lets’ try some other properties of the global variables open the global variable front panel, and delete any of the indicator let’s suppose I delete Numeric 2 from the global variable. The global variable block displaying the value of the numeric 2 control will change its color from orange (float control) to black as shown in the figure below.
Figure 13: Deleting a variable
- You can also place more than one data types in one global variable. Only the indicator type will change in this case all the operations will remain same. See the figure below,
Figure 14: Different data types in one global variable
- Place three global variables in the block diagram and set the control of remaining 2 to Boolean and string as shown in the figure below,
Figure 15: Changing control type
Figure 16: Indicator placement with different data types
- Now update the values in the global variables and run the main VI to see the results in the front panel of main VI. See the figure below, it displays the output of the global variable containing different data types.
Figure 17: Output with different data types
- Do the temperature conversion exercise by defining all the constants in subVI in the main VI i.e. using global variables. Constants are 5, 9 and 32.