Set Up Communication With USB Instrument

Updated Jul 26, 2023

Environment

Software

  • LabVIEW
  • Measurement & Automation Explorer (MAX)

Driver

  • NI-VISA

Operating System

  • Windows

This tutorial shows you how to set up NI software to communicate with a USB instrument in LabVIEW.

The NI-VISA driver supports two classes of USB instruments: USB Test and Measurement Class (USBTMC) and USB RAW instruments. USBTMC instrument conform to the USBTMC protocol, while USB RAW instruments use their own specific communication protocol. This tutorial will cover both types of instruments.

Prerequisites

Make sure you have a USB A to USB B cable to connect your instrument to the computer.  


This tutorial assumes familiarity with your instrument’s communication protocol and focuses on the set-up workflow in NI software. The set-up steps are different depending on the protocol used. Consult the documentation for your USB instrument or contact the instrument manufacturer for this information.

 

Install NI Software and Drivers

Install the latest compatible version of LabVIEW  and the NI-VISA  driver. Refer to the following for compatibility information. 

 

Set Up and Configure Hardware

After installing NI software, connect and configure your USB instrument.


USBTMC Instruments

NI-VISA will detect USBTMC instruments automatically, so no additional configuration is necessary after installing the NI-VISA driver. The instrument will be installed as a USB Test & Measurement Class Device. 

  1. Securely connect the USB instrument to the computer via a USB cable.
  2. Open Measurement and Automation Explorer (MAX) from the Start menu. 
  3. Expand Devices and Interfaces >> USB Devices. You should see the VISA resource name of the USBTMC instrument listed as USB[board]:: manufacturer ID:: model code:: serial number:: [USB interface number]::INSTR. 
  4. Continue to the Test Communication section.

 

USB RAW Instruments

For NI-VISA to recognize USB RAW instruments, you need to create a set-up information file (INF file) with the NI-VISA Driver Wizard and install it onto your computer.


Create INF File (USB driver)

  1. Launch NI-VISA Driver Wizard from the Start menu.
  2. On the Hardware Bus Selection page, select USB. Click Next.

  1. On the Device Selection page, do one of the following:
    • If your device is already connected to the computer, select it from the device list and click Next. If you do not see your device on the list, make sure it is securely connected and select Refresh. Continue to Step 5.
    • If your device is not connected/unavailable, select Other… at the bottom of the list. Click Next.


 

  1. If you selected Other…, manually input information about your device on the USB – Device Information page.


Click the question mark on the upper right of the window. This enables the Context Help and provides descriptions for each field.


To get the USB Manufacturer ID (Vendor ID) and USB Model Code (Product ID), refer to Find Vendor and Product ID for USB Device


Once you have the fields filled out, click Next. The image below shows an example for a National Instruments GPIB-USB-HS device.


 

  1. On the Output Files Generation page, name your INF file in the Instrument Prefix field and specify where it should be saved. Click Next.
  2. On the Installation Options page, specify your installation option.
    • If you would like to have the  NI-VISA Driver Wizard automatically install the INF file for you, select Install the generated files on this computer and click Finish. This is the default and recommended option. Continue to the Install USB Instrument section.

Note: Choosing the Install the generated file(s) on this computer option requires administrator/root privilege.

  • If you would like to install the INF files yourself, select Do nothing and exit the wizard and click Finish. Continue to the section below.

 

Install INF File

If you are manually installing the INF file, navigate to the location the INF file was created and open the INF file in any text editor (e.g., Notepad). The installation instructions are included in the header at the top of INF file.


For more detailed instructions, follow the steps below:

  1. If you’re using a Windows 8 machine or higher, you must sign the INF file first to install it on your computer. Windows 7 and earlier machines can skip to the next step.
  2. Make sure you are logged into an Administrator user account.
  3. Disconnect the USB instrument at this time so the driver will be properly associated with the device upon re-install.
  4. Copy the INF file to C:\\Windows\INF. If the INF folder is hidden, navigate to Tools >> Folder Options >> View >> Advanced Settings >> Show hidden files, folders, and drives to unhide it.
  5. Right-click on the INF file and click Install. This creates a PNF file for your device. You are now ready to install your USB instrument.


Install USB Instrument

  1. Securely connect the USB instrument to the computer via a USB cable.
  2. Add New Hardware Wizard should automatically open after you connect your device. Select Next. On the Add Hardware page, select Search for and install the hardware automatically (Recommended).  

  1. After an installation period, Windows should indicate that it has successfully installed your device.
  2. Open Measurement and Automation Explorer (MAX) from the Start Menu and expand Devices and Interfaces. You should see the instrument’s VISA resource name in the format USB[board]:: manufacturer ID:: model code:: serial number::[USB interface number]::RAW.


Some USB RAW instruments may not have serial numbers. If this is the case, NI-VISA automatically assigns a VISA specific serial number in the format NI-VISA-[number].


If the instrument only supports 1 interface, it will not be included in the VISA resource name.


In some cases, Windows may already have a default driver associated with your USB instrument and will install that driver first. To re-associate the driver to the INF file, follow the steps below:

  1. Open Device Manager.
  2. Find your instrument on the list and make sure its Vendor ID (VID) and Product ID (PID) match the ones used to create the INF file.
  3. Right click on the instrument and select Update Driver Software.
  4. Select Browse my computer for driver software.
  5. Select Let me pick from a list of drivers on my computer.
  6. Select Have disk.
  7. Navigate to C:\\Windows\INF and select the INF file.
  8. Click Next.
  9. Click Finished.  


Test Communication

After set-up and configuration, use VISA Test Panels to verify communication with the instrument and test different instrument commands.

  1. In MAX, select the Tools tab >> NI-VISA >> VISA Interactive Control.
  2. Select the VISA resource name of your USB instrument.
  3. In the Test Panel windows, select the Input/Output tab.
  4. In the Basic I/O tab, input a supported command and select Query. Refer to manufacturer documentation for a valid list of commands to use with your instrument. 
  5. The display window will outline the write and read operations.

 

Install Instrument Driver

After completing your configuration and verifying communication with the instrument, you need an easy way to transition from interactive mode to programming mode. The best way to achieve this is by using instrument drivers.

Refer to Choosing the Right Interface to Control Instruments in LabVIEW to determine which instrument driver type is best for your application. Follow the links in the document to install and use the instrument driver.

 

Run an Example

The instrument driver and NI-VISA driver will install examples and the API required to communicate with your instrument. Use these examples as programming references and an easy way to start using your instrument.


Note: Close MAX before you start running a LabVIEW example. Having MAX Test Panels open and running a LabVIEW example simultaneously will result in an access resource error. 


Instrument Driver Examples

If you installed an instrument driver, refer to IVI instrument driver examples.


NI-VISA Driver Examples

If you are using NI-VISA, refer to the following examples based on the USB instrument type.


USBTMC Instruments

For USBTMC instruments, you can use the NI-VISA API to communicate with the instrument. An example snippet is provided below. This example opens a session to the USB instrument, performs a write of *IDN?\n, reads the specified number of bytes back from the instrument, closes the session, and handles any errors.

  1. Right-click on the snippet and save it as an image.
  2. Drag the image directly onto an empty LabVIEW block diagram. 
  3. On the front panel, configure the VISA resource name to be the VISA resource of your instrument previously configured in this tutorial.
  4. Run the VI.

For more information on VISA functions, refer to LabVIEW Help: VISA VIs and Functions.



USB RAW Instruments

NI-VISA USB RAW mode supports Interrupt, Bulk and Control data transfers. Isochronous transfers are not supported. For more information about these communication types, refer to LabVIEW Help: Using NI-VISA to Communicate with Your USB Device.


Refer to the USB RAW shipping examples by navigating to the LabVIEW Help tab >> Find Examples…>>Hardware Input and Output >> VISA. You will find examples for bulk, control, and interrupt communication.


Note: Because NI-VISA USB RAW support is very device-specific, it requires detailed knowledge of how the device operates. Refer to manufacturer documentation for details on the communication protocol for your USB RAW instrument.