Tutorial: Subvis: Understanding Modularity
Tutorial: Subvis: Understanding Modularity
This video describes how to develop modular applications. The power of NI LabVIEW software lies in the hierarchical nature of the
VI. After you create a VI, you can use it on the Block Diagram of another VI. There is no limit to the number of layers in the
hierarchy. Using modular programming helps you manage changes and debug the Block Diagram quickly.
Table of Contents
Understanding Modularity
Modularity, by definition, means to use modules or smaller parts for the overall objective. Within LabVIEW, program modularity
means creating smaller sections of code known as subVIs. SubVIs are the same as VIs. They contain front panels and block
diagrams, but you call them from within a VI. A subVI is similar to a subroutine in text-based programming languages.
When you create a subVI and use it, you see an icon within your block diagram that represents the subVI. You can customize this
icon, which is the same icon in the upper right corner of the subVI front panel and block diagram. Learn how to customize icons in
a later section of this tutorial.
The following figures demonstrate the difference when replacing a section of code with a subVI. You can see the simplicity in the
bottom figure.
You can create a subVI just like a VI and then use it as a subVI, or you can create a subVI from code already within another VI.
Once you create a VI, you can:
Customize the icon for the subVI
Configure the terminals
Use it within other VIs repeatedly
Creating a SubVI from an Existing VI
You can simplify the block diagram of a VI by converting sections of the block diagram into subVIs.
1. Create a new VI and construct the following block diagram.
1/6
www.ni.com
The selected section of the block diagram is replaced with an icon for the subVI. LabVIEW creates controls and indicators for the
new subVI, automatically configures the connector pane based on the number of control and indicator terminals you selected, and
wires the subVI to the existing wires.
The new subVI uses a default pattern for the connector pane and a default icon.
Creating an Icon
Create custom icons to replace the default icon by right-clicking the icon in the upper right corner of the front panel or block
diagram and selecting Edit Icon. You can also do this by double-clicking the icon in the upper right corner of the front panel.
Once you open the Icon Editor, you have many tools for creating a custom icon or importing an image.
You also can drag a graphic from anywhere in your file system and drop it in the upper right corner of the front panel or block
diagram. LabVIEW converts the graphic to a 32x32 pixel icon.
You can find a standard set of graphics to use as a VI icon at ni.com/info by entering the info code expnr7.
Use the tools on the left side of the Icon Editor dialog box to create the icon design in the editing area. The normal size image of
2/6
www.ni.com
Use the tools on the left side of the Icon Editor dialog box to create the icon design in the editing area. The normal size image of
the icon appears in the appropriate box to the right of the editing area, as shown in image of the front panel above.
Use the Edit menu to cut, copy, and paste images from and to the icon. When you select a portion of the icon and paste an
image, LabVIEW resizes the image to fit into the selected area. You also can drag a graphic from anywhere in your file system
and drop it in the upper right corner of the front panel window or block diagram window. LabVIEW converts the graphic to an icon.
Use the Copy from option on the right side of the Icon Editor dialog box to copy from a color icon to a black-and-white icon and
vice versa. After you select the Copy from option, click the OK button to complete the change.
Use the Icon Editor tools to perform the following tasks:
The menu bar in the Icon Editor dialog box contains more editing options under the Edit menu such as Undo, Redo, Cut, Copy,
Paste, and Clear.
Develop a simple icon for the subVI created:
1. If the subVI is not open, double-click the placed icon on the block diagram.
2. From the front panel or block diagram of the subVI, double-click the icon in the upper right-hand corner.
4. Draw the icon you want to represent your subVI using the tools on the left.
5. Select the 16 colors icon on the right and click the 256 Colors button under Copy from. Repeat for the B & W icon.
You need to build a connector pane, shown as follows, to use the VI as a subVI. The connector pane is a visual representation of
3/6
www.ni.com
You need to build a connector pane, shown as follows, to use the VI as a subVI. The connector pane is a visual representation of
how inputs and outputs are connected to the subVI from the calling VI.
Each rectangle on the connector pane represents a terminal. Use the rectangles to assign inputs and outputs.
The following front panel has four controls and one indicator, so LabVIEW displays four input terminals and one output terminal on
the connector pane.
2.
Click the front panel control or indicator you want to assign to that terminal.
4/6
www.ni.com
Note that the terminal color changes to that of the data type to which you have connected it. You also can select the control or
indicator first and then select the terminal.
3.
4.
Repeat these steps for all the controls and indicators that you will use to pass data to and from the calling VI.
Using SubVIs
To open the front panel of a subVI from the calling VI, double-click the subVI on the block diagram. To display the block diagram of
a subVI from the calling VI, press the <Ctrl>key and double-click the subVI on the block diagram.
You can edit and save a subVI, and the changes affect all calls to the subVI, not just the current instance.
Setting Required, Recommended, and Optional Inputs and Outputs
In the Context Help window, the labels of required terminals appear bold, recommended terminals appear as plain text, and
optional terminals appear dimmed. The labels of optional terminals do not appear if you click the Hide Optional Terminals and
Full Path button in the Context Help window. Output terminals cannot be set as Required.
To designate which inputs and outputs are required, recommended, and optional:
1. Right-click a terminal in the connector pane.
5/6
www.ni.com
You also can select ToolsOptionsFront Panel and put a checkmark in the Connector pane terminals default to required
checkbox. This option sets terminals on the connector pane to Required instead of Recommended. This applies to connections
made using the wiring tool and to subVIs created using Create SubVI.
Inputs and outputs of VIs in vi.lib are already marked as Required, Recommended, or Optional. LabVIEW sets the inputs and
outputs of the VIs you create to Recommended by default. Set a terminal setting to Required only if the VI must have the input or
output to run properly.
Video
Exercise
Modules Home
6/6
FIRST Community
www.ni.com