Automatically Creating a SubVI from a Section of the Block Diagram


One key to writing your program right the first time with the least amount of bugs is modularity. The modules in LabVIEW are, of course, subVIs. When you're writing a large application, it's essential to break up the tasks and assign them to subVIs. If you didn't plan ahead (which is by far the best approach) or if the scope of the project increases unexpectedly (never happens), LabVIEW provides a nice recovery mechanism. The Edit>>Create SubVI menu option makes converting sections of your block diagram into subVIs much simpler. If you're working on some code and decide you should make part of it a subVI, no problem! Select the section of a VI's block diagram, as shown in Figure 15.77, and choose Edit>>Create SubVI. LabVIEW converts your selection into a subVI and automatically creates controls and indicators for the new subVI. The subVI replaces the selected portion of the block diagram in your existing VI, and LabVIEW automatically wires the subVI to the existing wires, as shown in Figure 15.78. You'll probably want to add a few features and move a few terminals around to match the wiring elsewhere in your project and, of course, there are terminal descriptions to add, but LabVIEW has given you a good start. This feature is also an excellent option to use, for example, if you need to repeat part of your block diagram in another VI. If two places need the same code, shouldn't it be in a subVI?

Figure 15.77. Selecting a block of code, which you wish to convert into a subVI


Figure 15.78. The subVI that replaced your selected block of code after selecting Edit>>Create SubVI from the menu


As with all menu options, you can create a custom shortcut key for the Create SubVI command in the Menu Shortcuts section of the Tools>>Options . . . dialog, as we discussed earlier.


Despite how easy it is to use, let's look at some rules for using the Create SubVI option.

Because converting some block diagrams into subVIs would change the behavior of your program, there are some instances where you cannot use this feature. These potential problems are listed next.


  • You cannot convert selections that would create a subVI with more than 28 inputs and outputs, because that is the maximum number of inputs and outputs on a connector pane. We'd feel sorry for the user who would have to wire even 15 inputs! In a case like this, select a smaller section or group data into arrays and clusters before selecting a region of the block diagram to convert.

  • You cannot convert selections in which items inside and outside a structure are selected, yet the structure itself has not been selected.

  • Linked Property nodes (those that are implicitly linked to controls on the front panel and do not have a reference input or output) that are contained within the selection of code, will be replaced by an unlinked Property Node. To maintain the connection between the control and the Property node, a VI Server Reference to the control will appear on the block diagram and the reference will be passed into the new subVI (and subsequently passed to the Property node) via a control reference data type. Figures 15.79 and 15.80 show the before and after state of this operation.

    Figure 15.79. Before: select a linked Property Node

    Figure 15.80. After: selected property converted into subVI

  • When your selection includes local variables but does not include the corresponding control, one local variable for the control remains in the calling VI and passes the data to or from the subVI. Within the subVI, the first local variable for a control becomes a read or write for the control and subsequent local variables refer to the control in the subVI. Yikes! What does that mean? Bottom line: Be very careful when you create a subVI from the selection if it includes locals.

  • When your selection includes local variables or front panel terminals inside a loop, the value that they are measuring may be changed elsewhere on the block diagram as the loop runs. Thus, when you convert the loop into a subVI, there is a possibility that the functionality of the selected code changes. If you have selected some but not all of the local variables or front panel terminals, LabVIEW displays a warning that allows you to choose between continuing and canceling the operation.

The Create SubVI option can be a really handy tool in cases where you start with what you thought was a small project (hah!), and you soon realize your program is going to be more complex than you originally thought. However, don't just squeeze a bunch of diagram mess into a subVI to increase your workspacesubVIs should always have a clear, well-defined task. When considering whether to make a part of your diagram a subVI, you might ask yourself, "Could I ever use this piece of code or function somewhere else?" We'll come back to subVIs and modular programming again in Chapter 17, where we look at good programming techniques.




LabVIEW for Everyone. Graphical Programming Made Easy and Fun
LabVIEW for Everyone: Graphical Programming Made Easy and Fun (3rd Edition)
ISBN: 0131856723
EAN: 2147483647
Year: 2006
Pages: 294

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net