Instrument Control in LabWindows/CVI Tutorial

Publish Date: Feb 27, 2019 | 4 Ratings | 4.25 out of 5 | Print | Submit your review

Overview

This tutorial will cover how to control your instrument in LabWindows/CVI using instrument drivers, the Instrument I/O Assistant, and the VISA Application Programming Interface (API).

Table of Contents

  1. Instrument Drivers
  2. Instrument I/O Assistant
  3. VISA API
  4. Creating an Instrument Driver
  5. Related Links:

1. Instrument Drivers

You can find instrument drivers for more than 5,000 instruments on the Instrument Driver Network . Look here to find an instrument driver for your particular instrument. If you find one, follow the procedure below to load it into LabWindows/CVI. If you cannot find one, refer to the section on Creating an Instrument Driver.

Back to Top

2. Instrument I/O Assistant


Use the Tools»Create Instrument I/O Task command to launch the NI Instrument I/O Assistant. You can use Instrument I/O Assistant to generate code to communicate with devices such as serial, Ethernet, and GPIB instruments without using an instrument driver. Instrument I/O Assistant generates the following three files:

· .c source file that contains the Run Task function and associated source code
· .h include file that contains the Run Task function declaration
· .mxb binary file that contains a description of the task that is used when you modify the task in Instrument I/O Assistant

These generated files are added to your project.

Using Instrument I/O Assistant

Instrument I/O Assistant organizes instrument communication into ordered steps. You must arrange the following steps into a sequence.

· Select Instrument – Specify the instrument with which the task will communicate. This action must be the first step in all sequences.
· Query and Parse – Send a command to the instrument, read a response from the instrument, and parse the returned data.
· Write – Write data or commands to the instrument.
· Read and Parse – Read a response from the instrument and parse the returned data.

As you specify and configure each step, click the Run this step button to execute the operations you configure, or click the Run button to perform the entire sequence. The following figure shows the Instrument I/O Assistant window.

Figure 1. Instrument I/O Assistant Window

For more information about configuring the steps, click Show Help to view the embedded help within the Instrument I/O Assistant.

You also must specify the name of the task, the name of the Run Task function to generate, and a Target Directory.

When you have created a sequence and configured each step, click OK in the Instrument I/O Assistant. LabWindows/CVI generates three files and adds the files to your project.

Once you create an Instrument I/O Assistant task, you can use it in your project.

To use your newly created task in a program, call the Run Task function declared in the header file as shown in the following code:

#include "MyIOTask.h"

double *data = 0;

RunMyIOTask (&data);

/* Analyze, log, or display your data. */

free (data);

Editing the Instrument I/O Task

You can double-click the .mxb file in the Project Tree to open the Instrument I/O Assistant and edit the task. If you make changes directly to the generated files, those changes are lost when you edit the task and regenerate the files.

Back to Top

3. VISA API


A powerful feature of VISA is the concept of a single interface for finding and accessing devices on various platforms. The VISA Resource Manager does this by exporting services for controlling and managing resources. These services include, but are not limited to, assigning unique resource addresses and unique resource IDs, locating resources, and creating sessions.

Each session contains all the information necessary to configure the communication channel with a device, as well as information about the device itself. This information is encapsulated inside a generic structure called an attribute. You can use the attributes to configure a session or to find a particular resource.

Opening a Session

When trying to access any of the VISA resources, the first step is to get a reference to the default Resource Manager by calling viOpenDefaultRM(). Your application can then use the session returned from this call to open sessions to resources controlled by that Resource Manager.

When referencing your instrument, you can use the canonical resource name that is assigned to your instrument or NI-VISA also supports the use of aliases to make opening devices easier. On Windows, run Measurement & Automation Explorer (MAX) and choose the menu option Tools»NI-VISA»Alias Editor to manage all your aliases. On UNIX, run visaconf and double-click any resource to bring up a dialog box for managing the alias for that resource. NI-VISA supports alias names that include letters, numbers, and underscores. To use an alias in your program, just call viOpen() with the alias name instead of the canonical resource name.

Finding Resources

As shown in the previous section, you can create a session to a resource using the viOpen() call. However, before you use this call you need to know the exact location (address) of the resource you want to open. To find out what resources are currently available at a given point in time, you can use the search services provided by the viFindRsrc() operation.

Configuring a Session

After the Resource Manager opens a session, communication with the device can usually begin using the default session settings. However, in some cases such as ASRL (serial) resources, you need to set some other parameters such as baud rate, parity, and flow control before proper communication can begin. GPIB and VXI sessions may have still other configuration parameters to set, such as timeouts and end-of-transmission modes, although in general the default settings should suffice.

Accessing Attributes

VISA uses two operations for obtaining and setting parameters – viGetAttribute() and viSetAttribute(). Attributes not only describe the state of the device, but also the method of communication with the device.

For example, you could use the following code to obtain the logical address of a VXI address:

status = viGetAttribute(instr, VI_ATTR_VXI_LA, &Laddr);

and the variable Laddr would contain the device's address. If you want to set an attribute, such as the baud rate of an ASRL session, you could use:

status = viSetAttribute(instr, VI_ATTR_ASRL_BAUD, 9600);

Notice that some attributes are read-only, such as logical address, while others are read/write attributes, such as the baud rate. Also, some attributes apply only to certain types of sessions; VI_ATTR_VXI_LA would not exist for an ASRL session. If you attempted to use it, the status parameter would return with the code VI_ERROR_NSUP_ATTR. Finally, the data types of some attribute values are different from each other. Using the above examples, the logical address is a 16-bit value, whereas the baud rate is a 32-bit value. It is particularly important to use variables of the correct data type in viGetAttribute().

Back to Top

4. Creating an Instrument Driver


Select Tools»Create IVI Specific Driver to open the Instrument Driver Development Wizard, with which you can create the source file, include file, and function panel file for controlling an instrument. Refer to the following figure to see the IVI Instrument Driver Wizard Window.

Figure 2. IVI Instrument Driver Wizard Window

You can base the new instrument driver on one of the following items:

· An existing driver for a similar instrument
· The core IVI driver template
· An IVI instrument class template

The Instrument Driver Development Wizard copies the template or existing driver files and replaces all instances of the original instrument prefix with the prefix you select for your new driver.

Refer to the LabWindows/CVI Instrument Driver Developers Guide for more information about the Instrument Driver Development Wizard.

Back to Top

5. Related Links:

LabWindows/CVI Instrument Driver Developers Guide
Instrument Driver Network
NI-VISA Programming Reference Manual
Getting Started Using National Instruments IVI with LabVIEW or LabWindows/CVI
Using IVI Drivers to Build Hardware-Independent Test Systems with LabVIEW and LabWindows/CVI
Using Instrument I/O Assistant to Create LabWindows/CVI Instrument Drivers
On-Demand Training: IVI Fundamentals I - What Are IVI Drivers? (SSP required)
On-Demand Training: IVI Fundamentals II - Why Use IVI Drivers? (SSP required)

Next Steps

 

Back to Top

Bookmark & Share


Ratings

Rate this document

Answered Your Question?
Yes No

Submit