Example Code

VirtualBench: Digital Read and Write With the DIO

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Hardware

  • VirtualBench All-in-One Instrument

Code and Documents

Attachment

Description

Overview
This example automates the VirtualBench digital input and output (DIO) channels; users can select which channels should be inputs or outputs and specify output values.


[+] Enlarge Image

Steps to Run Program

    1. Download example program on the right under downloads.
    2. In LabVIEW software, open the program by going to File » Open… and navigating to the file.
    3. Connect the VirtualBench device.
    4. Specify the channel to set as outputs and inputs using their respective controls in the Channel Settings panel. 
    5. Specify output level (high or low) using the Output Array control. 
    6. Click the Run button to begin outputting values and reading inputs.
    7. The inputs will display in the Input Array indicator. 
    8. Change the outputs as desired.
    9. Click the Stop button to end the program.

 

Program Explained

 


[+] Enlarge Image

 

    • Initialize Session
      • A new session to the DIO output channels is initialized by passing the Output Line(s) information from the control into the Dig Initialize VI.
    • Configure Instrument and Perform Operation
      • Line and error information is passed into a While Loop. The While Loop indicates the program will read inputs and write outputs repeatedly. 
      • Output line and error information is passed into the Dig Write VI. The Output Array information is also passed into the VI. The program will now be outputting the specified array on the corresponding channels. 
      • Input line and error information is passed into the Dig Read VI, and the VI reads the values and displays them on the Input Array indicator. 
      • The error wire is then unbundled in order to determine the status. This information is then passed through an OR with the state of the Stop button into the conditional terminal of the While Loop. If either an error has occurred or if the user clicks the Stop button, the While Loop will finish executing.
      • A 100 ms Wait has been added and will be performed after every read/write. While Loops are designed to  execute as fast as possible, but this can take up a lot of processor resources. The Wait is added to prevent  unnecessary use of computing resources.
    • Close Session
      • The line and error information are passed out of the While Loop and into the Dig Close VI. This closes the session to the DIO output channels and deallocates any resources for the next time a session is created with the DIO.
    • Handle Errors
      • Finally, the error information is passed into the Simple Error Handler VI. If an error has occurred, a dialog box will open to notify the user.

 

Additional Resources

 

VirtualBench Examples

 

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.