Creating
Sub-vi's in LabVIEW
In any programming environment, if you do enough programming you will eventually
want to put together code for common tasks and use it as a subroutine or
a function (or whatever other name is used for that in the particular programming
environment). You can do that in LabVIEW. Here's how.
-
Choose the code that you
want to "encapsulate", and select it. (It has to be the active selection.)
-
With the block diagram
selected, on the menu choose Edit - Creat
Sub vi.
-
The code you selected
will be replaced by a sub-vi.
-
You will probably need
to edit the connections to the sub-vi - i.e. the inputs and the outputs.
-
The sub-vi comes with
an icon - shown in the top-right hand corner of both the front panel and
the block diagram for the sub-vi.
-
You can edit the icon
by double-clicking it - which brings up an icon editor.
-
The icon also contains
information about connectors (terminals for information flowing into the
sub-vi and out of the sub-vi, i.e. inputs and outputs). The icon
may come with a single connector terminal, but you can right-mouse-click
the icon to put it into a connector-editing mode to add connectors.
With a little experimentation, you can learn to adjust the configuration.
-
To wire a connector, you
will need either a control (for an input) on the front panel, or an indicator
(for an output) on the front panel. You can create those from the
block diagram.
-
To wire an output, for
example, click on the terminal location on the icon (which will then turn
black) and then click on the indicator (which will turn the terminal location
the proper color for the kind of variable you have in the indicator).
Wire all of the inputs and outputs.
-
Save your sub-vi.
-
To use a sub-vi that you
have created, click on the Select a vi...
icon,
then find your sub-vi file and select it. You will get an insertion
icon that you can use to place your sub-vi whereever you want it in your
new program.