Table of Contents
Introduction
Using C++ for instrument control can be a challenge because of the complexity converting string results to C++ data types and communicating with a large number of hardware and bus protocols. Measurement Studio facilitates parsing and converting the string format results from your instrument into C++ data types by using Instrument I/O Assistant, an interactive tool to query, read, and parse results from your instrument. Furthermore, the Virtual Instrument Software Architecture (VISA) provides a single application programming interface (API) to communicate with any instrument regardless of the instrument interface. VISA is a high-level driver that calls lower-level drivers for each instrument hardware bus type. This application note introduces VISA and explains how you can take advantage of VISA using the Measurement Studio Instrument I/O Assistant and Microsoft Visual C++ 2003 to communicate with GPIB, serial, Ethernet/LAN, IEEE 1394, and USB instruments.
What Is the Measurement Studio Instrument I/O Assistant?
Measurement Studio is an integrated suite of classes and controls for test, measurement, and automation applications in Visual Studio 2005, Visual Studio .NET 2003, and Visual Studio 6.0. The Measurement Studio Instrument I/O Assistant is an interactive tool that uses VISA to facilitate interfacing with interface with GPIB, serial, Ethernet/LAN, IEEE 1394, and USB instruments. Instrument I/O Assistant automatically creates code to send commands to an instrument and parses the result strings that are returned.
What Is VISA?
VISA is a driver software architecture developed by National Instruments to unify communication with GPIB, serial, Ethernet/LAN, IEEE 1394, and USB instruments and simplify your instrument control applications. The VISA API accesses GPIB, serial, Ethernet/LAN, IEEE 1394, and USB instruments through OS calls and each particular hardware bus driver.
VISA offers the following benefits:
- Interface independence - VISA uses the same methods to communicate with instruments, regardless of the interface type. For example, the VISA command to write an ASCII string to a message-based instrument is the same for GPIB, serial, Ethernet/LAN, IEEE 1394, and USB. With this feature, you can use a single API to work with different instruments.
- An object-oriented architecture that can easily adapt to new instrumentation interfaces as they are developed in the future. You can easily include new interfaces.
- Built-in functionality for instrumentation programming in a very compact command set.
How Can I Use the Measurement Studio Instrument I/O Assistant with C++?
Instrument I/O Assistant is part of the Measurement Studio Professional and Enterprise packages. Once you install Measurement Studio you can start to use Instrument I/O Assistant to generate instrument control code and communicate with GPIB, serial, Ethernet/LAN, IEEE 1394, and USB instruments. The first step in creating your C++ instrument control application is to create a Measurement Studio Visual C++ Project in Visual Studio.Creating Measurement Studio Visual C++ Project
To create a Measurement Studio Visual C++ project you must open Visual Studio and click on File >> New >> Project. The New Project window will open and display the different types of project you can create. Select the Measurement Studio Projects >> Visual C++ Projects category on the left pane. This will display the different types of Templates that can be created in the right pane. Select MFC Application as shown in Figure 1.
Once you have selected the type of project, type the name of the new project and click the OK button. This action will open the MFC Application Wizard. Click on the Finish button in the MFC Application Wizard window to create your project.
Creating a VISA Task to Use Instrument I/O Assistant
After creating a new project, the Measurement Studio MFC Application Wizard should appear. The Measurement Studio MFC Application Wizard lets you specify which class libraries and instrument drivers you want to include in your project. Click the Next >> button to move to the Add Tasks slide. The Add Tasks slide lets you create a task to interactively configure a measurement or automation operation and generate code for the operation. Select the Launch Instrument I/O Assistant to create a Visa task checkbox and click on the Finish button to use Instrument I/O Assistant to create a task to perform instrument control operations.
Using Instrument I/O Assistant to Communicate and Control Your Instrument
The new VISA Task will be created as a new tab in Visual Studio. The Task will display the interface for Instrument I/O Assistant, as seen in Figure 2.
The first step in using Instrument I/O Assistant to generate code to control our instrument is to specify the resource name for our instrument. A resource is the GPIB, serial, Ethernet/LAN, IEEE 1394, and USB instrument or controller with which you want to communicate. The resource name, also know as instrument descriptor, specifies the exact name and location of the VISA resource. For example, the resource name ASRL1::INSTR describes the instrument on COM port 1 of your computer, and GPIB0::13::INSTR describes a GPIB instrument at address 13. To use VISA to communicate with an instrument, we must determine its address and instrument descriptor. The following link will take you to the Hardware section of the Instrument Control Fundamentals site. This site contains articles that will show you how to find the resource name for all instrument hardware buses.
Instrument Control Fundamentals: Hardware and Bus Technologies
Once you have determined the resource name for your instrument type it into the Select and instrument combo box. Once you have specified the resource name for your instrument you can start adding steps in Instrument I/O Assistant to perform different operations. To add a step, click on the + Add Step button. Select the Query and Parse option to create a step that will send a command to the instrument, read its response, and parse the result. You can also create steps to Write or Read and Parse a response from the instrument. The Query and Parse step performs both of the operations implemented by the Write and Read and Parse steps.
To demonstrate the use of Instrument I/O Assistant, we will send our instrument a command, MEAS:DC?, that will elicit a single measurement. The command is entered in Command combo box. To execute the command, click on Run this step button, which will automatically send the command to the instrument, read back the response, and parse the string to convert it into different data types as seen in Figure 3.
Instrument I/O Assistant will automatically parse the result string and try to determine the data type of the results. In this case it has correctly recognized that the result string contains a single numeric value. In case the automatic parsing did not recognize the information correctly, you can specify the data manually. A token will be created for each of the pieces of data recognized from the response string. A token will be used in your C++ code to store the results from the instrument query.
Using the Code Generated by Instrument I/O Assistant in Your C++ Application
To generate code to implement the functionality described in Instrument I/O Assistant, you must save the VISA task you generated. You can save the task by selecting the tab containing the task and typing CTRL-S or by going to File >> Save All. This action will create a file in your project under the Header Files called <Task Name>.h where <Task Name> is the name of the task you created. This header contains a function called Run, which you can use to run the functionality described by Instrument I/O Assistant. This function is public, which means that it can be used by any source file that includes the <Task Name>.h file. The function will contain as many parameters as token created in Instrument I/O Assistant. For example, in this document we created one step with one token, which contained the numeric vale we read from our instrument. Therefore, our Run function will have one parameter. This is an example of how the function would look in the header file:
void Run (double & token);
The token parameter is passed by reference so it can be used to output information into the application that is calling it. In this way you can use the functionality you implemented in Instrument I/O Assistant in your C++ code.
Conclusion
Measurement Studio provides a fast and easy solution for your C++ instrument control needs. Measurement Studio takes advantage of the ease of use of Instrument I/O Assistant and the power of C++.
Reader Comments | Submit a comment »
Legal
This tutorial (this "tutorial") was developed by National Instruments ("NI"). Although technical support of this tutorial may be made available by National Instruments, the content in this tutorial may not be completely tested and verified, and NI does not guarantee its quality in any way or that NI will continue to support this content with each new revision of related products and drivers. THIS TUTORIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND AND SUBJECT TO CERTAIN RESTRICTIONS AS MORE SPECIFICALLY SET FORTH IN NI.COM'S TERMS OF USE (http://ni.com/legal/termsofuse/unitedstates/us/).



