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.
2. Select the section of the block diagram you want to convert.
3. From the Tools menu, select Edit»Create SubVI.
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 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.
3. Double-click the filled rectangle tool, , to create a blank icon with a border.
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.
6. Click OK to save the icon.
Building the Connector Pane
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.
Setting Up the Connector Pane
Define connections by assigning a front panel control or indicator to each of the connector pane terminals.
1. Right-click the icon in the upper right corner of the front panel.
2. Select Show Connector.
3. You can select a different pattern by right-clicking the connector pane and selecting Patterns.
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.
Assigning Terminals to Controls and Indicators
After you select a pattern to use for the connector pane, you must assign a front panel control or indicator to each of the connector pane terminals. It is generally good programming practice to organize the inputs to a subVI on the left and the outputs on the right.
To assign a terminal to a front panel control or indicator:
1. Click a terminal of the connector pane.
2. Click the front panel control or indicator you want to assign to that terminal.
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. Click an open space on the front panel to deselect the control.
4. Repeat these steps for all the controls and indicators that you will use to pass data to and from the calling VI.
To place a subVI on the block diagram:
1. Click the Select a VI button on the Functions palette.
2. Navigate to the VI.
3. Double-click to place it on the block diagram.
Opening and Editing 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.
2. Select This Connection Is from the shortcut menu.
3. Select Required, Recommended, or Optional.
You also can select Tools»Options»Front 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.
Exercise Passing Data, Debug, and SubVIs Modules Home FIRST Community