Sie sind auf Seite 1von 14

Chapter 3 Creating a SubVI Chapter 3 Creating a SubVI This chapter introduces the icon/connector of a VI and explains how you

u can use a VI as a subVI in other VIs. You Will Learn: A. What a subVI is. B. How to create an icon and connector. C. How to use a VI as a subVI. D. How to use the Create SubVI menu option. A. Basic Ideas The key to creating LabVIEW applications is understanding and using the hierarchical nature of the VI. That is, after you create a VI, you can use it as a subVI in the block diagram of a higher-level VI. If a block diagram has a large number of icons, you can group them into a lower-level VI to maintain the simplicity of the block diagram. This modular approach makes applications easy to debug, understand, and maintain. SubVIs are similar to functions or subroutines in a conventional programming language. The following pseudo-code and block diagram demonstrate the analogy between subVIs and subroutines.

B. Creating the Icon and Connector A VI that you use as a subVI needs an icon to represent it in the block diagram of a calling VI. The subVI also must have a connector with terminals to pass data to and from the higher-level VI. ExampleSlope Function As an example, consider a VI that calculates the slope of two coordinates. The front panel and the block diagram for the VI are shown below. To use this VI as a subVI, you must create an icon and a connector for it.

Chapter 3 Creating a SubVI

Icon Every VI has a default icon displayed in the upper-right corner of the Panel and Diagram windows. For VIs, the default icon is a picture of the LabVIEW logo and a number indicating how many new VIs you have opened since launching LabVIEW. You use the Icon Editor to customize the icon by turning individual pixels on and off. To activate the Icon Editor, pop up on the default icon in the top right corner of the Panel or Diagram window and select Edit Icon as shown below.

The following window appears. You use the tools at left to create the icon design in the fat pixel editing area. An image of the icons actual size appears in one of the boxes to the right of the editing area.

Chapter 3 Creating a SubVI

Another method to create an icon is to drag any .BMP, .WMF, .EMF, or .PCT format graphic into the Front Panel icon pane. Depending on the type of monitor you are using, you can design a separateicon for monochrome, 16-color, and 256-color mode. You design and save each icon version separately. The editor defaults to Black & White, but you can click on one of the other color options to switch modes. The tools to the left of the editing area perform the following functions: Draws and erases pixel by pixel. Draws straight lines. Use <shift> to restrict drawing to horizontal, vertical, and diagonal lines. Selects the foreground color from an element in the icon. Fills an outlined area with the foreground color. Draws a rectangular border in the foreground. Double-click on this tool to frame the icon in the foreground color. Draws a rectangle bordered with the foreground color and filled with the background color. Double-click on this tool to frame the icon in the foreground color and fill it with the background color. Selects an area of the icon for moving, cloning, deleting, or performing other changes. Double-click on this tool and select Delete from the keyboard to delete the entire icon at once. Enters text into the icon. Double-click on this tool to select a different font. Note that in Windows, smaller fonts usually work better. Displays the current foreground and background background colors. Click on each to get a palettefrom which you can choose new colors.

Chapter 3 Creating a SubVI Show Terminal Click on this option to display the terminal pattern of the connector OK Click on this button to save your drawing as the VI icon and return to the Panel window. Cancel Click on this button to return to the Panel window without saving any changes. The menu bar contains more editing options such as Undo , Redo, Cut, Copy, Paste, and Clear. Connector The connector is the programmatic interface to a VI. If you use the panel controls or indicators to pass data to and from subVIs, these controls or indicators need terminals on the connector pane. You define connections by choosing the number of terminals you want for the VI and assigning a front panel control or indicator to each of those terminals. To define a connector, you select Show Connector from the icon pane pop-up menu on the Panel window, as the following illustration shows. The Diagram window does not have a connector pane.

The connector replaces the icon in the upper-right corner of the Panel window. LabVIEW selects a terminal pattern appropriate for your VI with controls on the left side of the connector pane, and indicators on the right. The number of terminals selected depends on the number of controls and indicators on your front panel.

Chapter 3 Creating a SubVI

Each rectangle on the connector represents a terminal area, and you can use the rectangles either for input to or output from the VI. If necessary, you can select a different terminal pattern for your VI. Selecting and Modifying Terminal Patterns To select a different terminal pattern for your VI, pop up on the connector and choose Patterns from the pop-up menu.

A boldfaced border highlights the pattern currently associated with your icon, as shown above. To change the pattern, click on a new pattern. If you choose a new pattern, you will lose any assignment of controls and indicators to the terminals on the old connector pane. The maximum number of terminals available for a subVI is 28. If you want to change the spatial arrangement of the connector terminal patterns, choose one of the following commands from the connector pane pop-up menu: Flip Horizontal, Flip Vertical, or Rotate 90 . Assigning Terminals to Controls and Indicators You assign front panel controls and indicators to the terminals using the Wiring tool. Use the following steps to associate the connector pane with the front panel controls and indicators.

Chapter 3 Creating a SubVI 1. Click on a connector terminal. The tool automatically changes to the Wiring tool. The terminal turns black.

2. Click on the front panel control or indicator you want to assign to the selected terminal. A dashed line frames the selected object.

If you position the cursor in free space and click, the dashed line disappears and the selected terminal turns the same color as the data type in the control or indicator you linked to it, indicating that the control or indicator you selected now corresponds to the colored terminal. Although you use the Wiring tool to assign terminals on the connector to front panel controls and indicators, no wires are drawn between the connector and these controls and indicators.

Chapter 3 Creating a SubVI 3. Repeat steps 1 and 2 for each control and indicator you want to connect. You also can select the control or indicator first and then select the terminal. You can choose a pattern with more terminals than you need. Unassigned terminals do not affect VI operation. You also can have more front panel controls or indicators than terminals. Exercise Use Convert C to F.vi Objective: To build an icon and a connector for this VI. C. Using a VI as a SubVI You may use any VI that has an icon and a connector as a subVI in the block diagram of another VI. You select VIs for use as subVIs from the Select a VI... option from the Functions palette. Choosing this option produces a file dialog box from which you can select any VI on your computer system.

You can also move an open VI into the diagram of another open VI by dragging the icon of the VI you want to be a subVI into the diagram of the other VI. A subVI is analogous to a subroutine. A subVI node (icon/connector) is analogous to a subroutine call. The subVI node is not the subVI itself, just as a subroutine call statement in a program is not the subroutine itself. A block diagram that contains several identical subVI nodes will call the same subVI several times. However, multiple copies of the subVI will not be stored in memory. Opening, Operating, and Changing SubVIs You may open a VI used as a subVI from the calling VIs block diagram. You open the Panel window of the subVI by double-clicking on the subVI icon. You then can open the Diagram window by selecting Show Diagram from the Windows menu. Any changes you make to a subVI alter only the version in memory until you save the subVI. Note that the changes affect all calls to the subVI and not just the node you used to open the VI. Online Help for SubVI Nodes With the Help Window enabled (Help menu Show Help), when you move an editing tool across a subVI node, the Help window displays the subVI icon with wires attached to each terminal. An example is shown below.

Chapter 3 Creating a SubVI

You can classify the requirements of the inputs/outputs of your subVI and represent the classification accordingly in the Help window. For example, by classifying that input as Required, you can automatically detect whether you have wired the input and prevent your VI from running if you have not.To classify input terminals, pop up on the icon pane and select Show Connector. Then, pop up on an input or output on the connector pane and choose This Connection Is Required, Recommended, or Optional.

Required You cannot run the VI without wiring it correctly. In the Help window, connections appear in bold text. Recommended You can run the VI, but the error list window will list a warning for the input. In the Help window, connections appear in plain text. Optional You can run the VI, but the potential for wiring error increases. In the Help window, connections are disabled. If the Help window is in simple view, the connections are hidden. By default, input and output terminals are recommended. See the following examples of a node with classified inputsthe Read File function from the File subpalette and the Print Panel VI from the Application Control subpalette.

Chapter 3 Creating a SubVI Exercise: Thermometer.vi Objective: To build a VI that you will use as a subVI. You will build a VI that measures temperature using the temperature sensor on the DAQ Signal Accessory. The sensor outputs a voltage proportional to temperature. For example, if the temperature is 23 C, the sensor output voltage is 0.23 V. The VI also will have the option to display the temperaturein degrees Fahrenheit rather than degrees Celsius. You measure the voltage using the plug-in DAQ board inside your computer. The sensor is hard-wired to Channel 0 of the DAQ board. You will use the Read Voltage VI to measure the voltage and then convert the voltage into a Fahrenheit or Celsius temperature reading. If a DAQ board and/or DAQ Signal Accessory is not available, use the (Demo) Read Voltage VI (User Libraries Basics Course subpalette) instead of the Read Voltage VI. You will use this VI later, so be sure to save it as the instructions below describe. Front Panel

1. Open a new panel by selecting New from the File menu.If you have closed all VIs, select New VI from the initial LabVIEW window.) 2. Place the thermometer indicator in the Panel window. a. Pop up in an open area of the Panel window and choose Thermometer from the popup Numeric subpalette. b. Type Temperature inside the highlighted text box and click the mouse button or the Enter button on the toolbar. 3. Rescale the thermometer control to display the temperature between 0.0 and 100.0. Using the Labeling tool, double-click on 10.0 in thermometer scale, type 100.0, and click the enter button on the toolbar. 4. Place the vertical switch control in the Panel window. a. Pop up in an open area of the Panel window and choose Vertical Switch from the popup Boolean subpalette. Type Temp Scale inside the text box and click the mouse button or the enter button on the toolbar. b. Using the Labeling tool, place a free label, deg C, next to the true condition of the switch. Place a free label, deg F, next to the false condition of the switch. Documenting the VI You can document the VI by choosing Show VI Info... from the Windows menu. Type the description of the VI in the dialog box. You can recall the description by again selecting Show VI Info... from the Windows menu.

Chapter 3 Creating a SubVI 5. Document the VI. Select Show VI Info... from the Windows menu. Type the description for the VI as shown and click on OK.

You can document the objects on the front panel (or their respective terminals on the block diagram) by popping up on the object and choosing Data Operations Description... from the object pop-up menu. Type the object description in the dialog box that appears.

You can recall the description by again selecting Description... from the object pop-up menu. An example pop-up menu that appears while the VI is in run mode is shown below. (You cannot edit the description while in the run mode.)

Chapter 3 Creating a SubVI

6. Document the thermometer indicator and switch control. a. Pop up on the thermometer indicator and choose Data Operations Description... from the pop-up menu. b. Type the description for the indicator as shown and click on OK.

c. Pop up on the vertical switch control and choose Data Operations Description... from the pop-up menu. d. Type the description for the control as shown and click on OK.

Chapter 3 Creating a SubVI 7. Show the descriptions you created by again selecting Data Operations Description... from the indicator and control pop-up menu. Block Diagram

1. Open the Diagram window by choosing Show Diagram from the Windows menu. 2. Select the block diagram objects. For each object, pop up in an open area of the Diagram window and choose the object from the pop-up menu. If your computer lacks a plug-in DAQ board or a DAQ Signal Accessory is not available, use the (Demo) Read Voltage VI. Read Voltage VI. In this exercise, this VI reads the voltage at Channel 0 or device 1. This VI simulates the Read Voltage VI operation. Numeric Constant (Numeric subpalette). (You need two of these constants.) To insert a new value, double-click inside the numeric with the Labeling tool and type the new value. String Constant (String subpalette). To insert a new value, double-click inside the string with the Labeling tool and type the new value. Multiply function (Numeric subpalette). In this exercise, this function multiplies the voltage that the Read Voltage VI returns by 100.0 to obtain the Celsius temperature. Convert C to F VI - This is the VI you built the icon and connector for in the previous exercise. You will use it here to convert the Celsius readings into Fahrenheit. Select function (Comparison subpalette). Depending on the value of the Temp Scale switch, the function outputs either the Fahrenheit (False) or Celsius (True) temperature value. 3. Using the Positioning tool, place the icons as illustrated on the diagram and wire them together with the Wiring tool. Remember, if you need to see icon terminals, pop up on the icon and choose Show Terminals from the pop-up menu. You also can show the Help window by choosing Show Help from the Help menu.The Read Voltage VI measures the voltage at Channel 0 of the plug-in board. The VI then multiplies the voltage by 100.0 to convert it to a temperature in C.

Chapter 3 Creating a SubVI 4. Make the Panel window the active window by selecting it and run the VI several times. Place the VI in the free-run mode by clicking on the Continuous Run button. Put your finger on the temperature sensor and notice the temperature increase. 5. Turn off the continuous-run mode by clicking on the Continuous Run button. 6. Create the icon.

a. Invoke the Icon Editor by popping up in the Icon Pane in the Panel window and choosing Edit Icon from the pop-up menu. b. Erase the default icon by double-clicking on the Select tool and pressing <Delete>. Redraw the frame by double-clicking on the Rectangle tool. c. Draw an icon that represents the thermometer. Draw the thermometer with the Pencil tool. Shift-dragging (holding down <shift> while dragging the mouse) with the Pencil tool draws horizontal or vertical straight lines. d. Create the text with the Text tool. Double-click on the Text tool to change the font to Small Font. e. Close the Icon Editor by clicking on OK when your icon is complete. The icon appears in the Icon Pane in the upper-right corner of the Panel window.

7. Create the Connector.

8. Save the VI by choosing Save from the File menu. Name the VI Thermometer.vi.

Chapter 3 Creating a SubVI This VI is now complete and ready for use as a subVI in other VIs. The icon represents the VI in the block diagram of the calling VI. The connector (with two terminals) outputs the temperature. 9. Close the VI by choosing Close from the File menu. D. The Create SubVI Option You can simplify the block diagram of your VI by converting sections of your diagram into subVIs. You can encapsulate subdiagrams into subVIs by selecting the section to convert and then choosing Create SubVI from the Edit Menu. LabVIEW converts the selection into a subVI and replaces the subdiagram with the new subVI. LabVIEW automatically creates the controls and indicators for the new subVI and wires the subVI to the existing wires. An example is shown below.

You cannot convert a section that creates a subVI with more than 28 inputs and outputs, because 28 is the maximum number of inputs and outputs allowed on a connector pane.

Das könnte Ihnen auch gefallen