Qia's LabVIEW Station Virry Test & Control

We talk about LabVIEW and HVAC

Using NI-VISA 3.0 to Control Your USB Device

This tutorial is meant as a starting point for using NI-VISA to communicate with a USB device. It is not intended as a starting point for learning about USB architecture or the various protocols used in USB communication. After reading this tutorial, you should be able to install a USB device and use NI-VISA to communicate with that device, as long as you understand the device communication protocol.

Table of Contents:

        1. USB and VISA Background

      VISA is a high-level application programming interface (API) used to communicate with instrumentation buses. VISA is platform independent, bus independent, and environment independent. In other words, the same API is used regardless of whether a program is created to communicate with a USB device with LabVIEW on a machine running Windows 2000 or with a GPIB device with C on a machine running Mac OS X.

      Universal Serial Bus (USB) is a message-based communication bus. This means a PC and a USB device communicate by sending commands and data over the bus as text or binary data. Each USB device has its own command set. You can use NI-VISA Read and Write functions to send these commands to an instrument and read the response from an instrument. Check with your instrument manufacturer for a list of valid commands for your instrument.

      Starting with version 3.0, NI-VISA supports USB communication. Two classes of VISA resources are supported: USB INSTR and USB RAW.

      The USB INSTR resource class is used by USB devices that conform to the USB Test and Measurement Class (USBTMC) protocol. USBTMC devices conform to a protocol that the VISA USB INSTR resource class can understand. No configuration is necessary to communicate with a USBTMC device. To communicate with a USBTMC instrument, refer to section 3. For more information about the USBTMC specification, refer to the USB Implementers Forum Web page linked below.

      USB RAW instruments are any USB instrument other than those instruments that specifically conform to the USBTMC specification. If you are using a USB RAW device, follow the instructions in section 2 to configure NI-VISA to control your device. Contact your instrument manufacturer for details about the communication protocol and the command set your instrument uses.

      For specific information about the NI-VISA API, refer to the NI-VISA User Manual and NI-VISA Programmer Reference Manual. Both documents are included with NI-VISA and are available through the links at the end of this tutorial.

            2. Configuring NI-VISA to Control Your USB Device

          This section walks through the steps for configuring a USB RAW device to be controlled by NI-VISA 3.0 on a Windows-based computer. If you are using a USBTMC-compatible device, connect your device and skip to section 2.3.

          At this point, NI-VISA already should be installed on your computer, and your USB device should not be connected. Furthermore, you should not have a driver for your USB device installed. There are three steps to configure your USB device to use NI-VISA:


          1. Create the INF file using the Driver Development Wizard.
          2. Install the INF file and the USB device using the INF file.
          3. Test the device with NI-VISA Interactive Control.



          For the purposes of this tutorial, a National Instruments DAQPad-6020E is used as an example USB device and is installed on a Windows XP system. Because this tutorial is intended to explain the configuration of a generic USB device, details specific to the DAQPad-6020E are not discussed. Remember that NI-DAQ is the only supported driver for a DAQPad-6020E.

          2.1. Create the INF File Using the Driver Development Wizard

          To use NI-VISA, you must first tell Windows to use NI-VISA as default driver for the device. In the Windows environment, you can do this with an INF file. NI-VISA 3.0 includes the VISA Driver Development Wizard (DDW) to create an INF file for your USB device.


          1. To open the DDW, select Start»Programs»National Instruments»VISA»VISA Driver Developer Wizard. The window shown in Figure 1 opens.

            Figure 1. VISA DDW Hardware Bus Window


            You can use this wizard to create an INF file for a PXI/PCI or USB device. Because you are creating the driver for a USB device, click USB and Next. The VISA DDW Basic Device Information window opens as shown in Figure 2.

            Figure 2. VISA DDW Basic Device Information Window


          2. For this step, you must know the USB vendor ID and product ID for your USB instrument. These numbers identify your USB device when you install it and address your device when you want to communicate with it. According to the USB specification, both numbers are 16-bit hexadecimal numbers and should be provided by the device manufacturer.

            Enter the vendor ID, product ID, manufacturer name, and model name for your device in their respective fields.

            For the DAQPad-6020E, the vendor ID and product ID are 0x3923 and 0x12C0, respectively. If your device is not a National Instruments DAQPad-6020E, the vendor ID and product ID are different for your device. Contact your device vendor to obtain this information.

            Click Next. The Output Files Properties window is displayed as shown in Figure 3.

            Figure 3. VISA DDW Output Files Properties Window


          3. The USB Instrument Prefix is simply a descriptor you use to identify the files used for this device. Enter a USB instrument prefix, select the desired directory in which to place these files, and click Finish. The INF file is created in the directory specified by the output file directory.



          2.2. Install the INF files and the USB device.

          The installation of the INF files is different for each version of Windows. When the DDW creates an INF file, installation instructions are included in a header at the top of the INF file. Because INF files are ASCII text files, they can be read in any text editor such as Notepad. For detailed information about installing your INF file, open your INF file in a text editor and follow the instructions at the top of the file. This tutorial assumes you are using Windows XP.


          1. Copy the INF file to the INF folder. On Windows XP, this folder is usually at C:\WINDOWS\INF. This folder may be hidden, so you may need to change your folder options to view hidden files.

          2. Right-click on the INF file in C:\WINDOWS\INF and click Install. This process creates a PNF file for your device. You are now ready to install your USB device.

          3. Connect your USB device. Because USB is hot pluggable, Windows should be able to detect your USB device, and the Add New Hardware Wizard should open automatically as soon as you connect it to the USB port. Follow the onscreen instructions for the wizard. When you are prompted to select a driver for this device, browse to the INF folder and select the INF file you created using the DDW.



          2.3. Test Communication with VISA Interactive Control.


          1. Open Measurement & Automation Explorer. Select Tools»Refresh to refresh the view. Your USB device should be listed as a USB Device under Devices and Interfaces as shown in Figure 4. Your USB device is now installed and configured to use NI-VISA.

            If you select your USB device, the device information is displayed in the Attributes window. Using this window, you can access information such as the manufacturer ID, model code, and serial number for your device.



            Figure 4. USB Device Shown in Measurement & Automation Explorer


          2. To communicate with your device using VISA, use the VISA instrument descriptor for your device. The instrument descriptor format for a USB INSTR device is USB[board]:: manufacturer ID:: model code:: serial number[:: USB interface number]::INSTR. The instrument descriptor format for a USB RAW device is USB[board]:: manufacturer ID:: model code:: serial number[:: USB interface number]::RAW.

            According to the USBTMC specification, all USBTMC devices must have a serial number. Some USB RAW devices may not have serial numbers. If your device does not have a serial number, NI-VISA automatically assigns a VISA specific serial number for that device. The format for the serial number is NI-VISA-#, where # is an automatically generated number.

            Some USB devices have multiple interfaces. This is similar to the way a PCI device can have multiple functions. If your device only supports one interface, you do not need to include the USB interface number.

            The DAQPad-6020E uses the RAW class, and the manufacturer code and model code are 0x3923 and 0x12C0, respectively. For the DAQPad-6020E, the instrument descriptor is USB0::0x3923::0x12C0::00B50DAE::RAW.

            To test communication with this device, open Measurement & Automation Explorer. Select Tools»NI-VISA»VISA Interactive Control. A window similar to that shown in Figure 5 should open.

            Figure 5. VISA Interactive Control


          3. The VISA Interactive Control (VISAIC) is a utility program used to communicate easily with any VISA resource. After your USB device is configured to use VISA, it should be listed in the USB branch. Double-click on your device to open a VISA session to the device. The window shown in Figure 6 should open.

            Figure 6. VISA Interactive Control Open VISA Session


            When you open a VISA Session with VISAIC, the Template tab and the Property Node (Read) tab are automatically selected. To read a property, select the desired property and click Execute. The current value of the property specified in Attribute Name is displayed in the Current Value indicator. In Figure 6, the Resource Name property was read.

            For more information about using VISAIC, refer to Developer Zone Tutorial: VISA Interactive Control (VISAIC), linked below. For information about the NI-VISA API, review the NI-VISA Programmer Reference Manual and the NI-VISA User Manual. Both are available through the links at the end of this tutorial. For a list of valid commands for your USB instrument, contact your instrument’s manufacturer.

                3. Using NI-VISA to Communicate with Your USB Device

              This section explains how to communicate with your USB device using NI-VISA 3.0 and above. Recall that there are two classes of USB devices. The method of communication depends on the class of your device.

              3.1 USB INSTR Class (USBTMC)

              Devices that conform to the USB Test and Measurement Class (USBTMC) use the NI-VISA USB INSTR class. These devices use 488.2 style communication. For these devices, you can simply use the VISA Open, VISA Close, VISA Read, and VISA Write functions in the same way you would if you were communicating with GPIB instruments.

              Figure 7 illustrates a LabVIEW VI that communicates with a USBTMC device. In this example, a VISA session is opened to a USB device. A command is written to the device, and the response is read back. In this example, the specific command being sent is the ID query for the device. Check with your device manufacturer for your device command set. After all communication is complete, the VISA session is closed.

              Figure 7. USBTMC LabVIEW Example Block Diagram


              3.2 USB RAW Class

              Communicating with the USB RAW class is more complicated because each device may use its own communication protocol. Contact your device vendor for details about the communication protocol for your device.

              USB communicates using four types of pipes or endpoints: control, bulk, interrupt, and isochronous. Each type of pipe transfers a different type of information. Furthermore, any number of endpoints can be of any endpoint type. Think of an endpoint as a communication socket. For specific details about USB architecture, review the USB specification linked below.

              NI-VISA supports three types of USB pipes: control, bulk, and interrupt. When NI-VISA detects your USB instrument, it automatically scans your instrument for the lowest available endpoint for each type.

              When VISA detects the lowest available endpoint, it assigns that value to the appropriate VISA attribute. The bulk in endpoint and bulk out endpoint are stored in the VI_ATTR_USB_BULK_IN_PIPE attribute and the VI_ATTR_USB_BULK_OUT_PIPE attribute, respectively. The interrupt in endpoint is stored in the VI_ATTR_USB_INTR_IN_PIPE attribute. A value of -1 indicates that a USB device does not support that type of pipe. For the control pipe, only endpoint zero is supported. If you are using the C API, use the viSetAttribute function to change endpoints. In LabVIEW, use a Write VISA Property node.

              NI-VISA includes four functions to transfer data through USB pipes. Before you can communicate with your device using these functions, you need to set up the communication protocol using the VISA USB attributes. The following list describes the available functions.

              • Use VISA USB Control In and VISA USB Control Out to transfer data using the control pipe.
              • To transfer data using a bulk pipe, use VISA Read and VISA Write.
              • If you are using LabVIEW, VISA includes an additional function to use the interrupt pipe: VISA Get USB Interrupt Data. In the C API, you can do this by accessing the VI_ATTR_USB_RECV_INTR_SIZE and VI_ATTR_USB_RECV_INTR_DATA attributes of the VI_EVENT_USB_INTR event object. See the NI-VISA Programmer Reference Manual for more information about VISA Events.

                  Related Links:
                  Using the PXI Driver Development Wizard and NI-VISA to Register-Level Program a PXI/PCI Device under Windows
                  USB Implementers Forum: USB 2.0 Specification: http://www.usb.org/developers/docs
                  USB Implementers Forum: Approved Class Specification Documents: http://www.usb.org/developers/devclass_docs
                  VISA Interactive Control (VISAIC)
                  Product Manuals: NI-VISA Programmer Reference Manual
                  Product Manuals: NI-VISA User Manual

                  posted on 2005-05-22 15:42  LabVIEW开发者  阅读(4306)  评论(1编辑  收藏  举报

                  导航