NI does not actively maintain this document.
This content provides support for older products and technology, so you may notice outdated links or obsolete information about operating systems or other relevant products.
VISA is a driver software architecture developed to unify communication with GPIB, serial, Ethernet/LAN, IEEE 1394, and USB instruments and simplify your instrument control applications. With the VISA API developers can use GPIB, serial, Ethernet/LAN, IEEE 1394, and USB instruments.
VISA offers the following benefits:
The VISA .Net API is an object-oriented interface made up of a set of .Net classes to communicate with instruments with VISA. You can use Net compliant languages such as C# and Visual Basic .Net.
The VISA .Net API is part of the free NI-VISA driver software. NI VISA can be downloaded free of charge from National Instrument VISA for use with NI products. To install .Net support for NI VISA include .Net support by selecting .Net Framework 1.1 Languages Support under the Development Support category as seen in Figure 1.
Adding References to the VisaNS Class Libraries
To create your C# or Visual Basic .Net instrument control application, begin with a Solution in Visual Studio. In the Solution Explorer you will find that each project has a category called References under which references are organized. In order to use the VISA .Net API class libraries add the NationalInstruments.Common and NationalInstruments.VisaNS references to your project. The NationalInstruments.Common namespace includes common methods and properties used by many of the National Instruments drivers. The NationalInstruments.VisaNS namespace includes the methods and properties for the VISA .Net API. To add a new reference, right click on References category and select Add Reference.
Importing the VisaNS Namespace into Your Application
When you reference the VisaNS classes, you can use them in your project. By importing the VisaNS namespace, you can more directly access objects by reducing typing. Import the VisaNS namespace by adding the following line of code to the beginning of your application:
[C#]
using NationalInstruments.VisaNS;
[VB .Net]
Imports NationalInstruments.VisaNS
Determining the Instrument Resource Name
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 communicate with an instrument using VISA. we must determine its address and instrument descriptor.
We will use this resource name later on in the tutorial to specify which instrument we want to communicate with.
Opening a VISA Session
A session is a connection or link from the VISA .Net API to a resource. The VISA .Net API contains different Session classes designed for particular applications. For example, the MessageBasedSession class is used for instruments that communicate by sending and receiving messages in the form of text strings. In contrast, the RegisterBasedSession class is used to communicate with instruments that communicate by writing and reading from registers. In this tutorial we will discuss the MessageBasedSession class. If you want more information on the other types of sessions, please refer to the NI-VISA .Net Framework help document that is installed with NI VISA.
The first step in creating a new MessageBasedSession is to declare it as a variable in our application. The following code demonstrates how to declare a new MessageBasedSession:
[C#]
private MessageBasedSession mbSession;
[VB .Net]
Private mbSession As MessageBasedSession
It is important to declare this MessageBasedSession variable as a global variable in the form or class. Any function that communicates with the instrument will have to access this object.
After declaring the MessageBasedSession variable, we need to instantiate a MessageBasedSession object. To do so, we will use the static (or shared as it is called in Visual Basic) method, GetLocalManager of the ResourceManager class. This method instantiates a new ResourceManager object. This new ResourceManager object contains a function called Open, which then instantiates a new Session object. Finally, we will cast the newly created Session object to a MessageBasedSession. The ResourceManager and Session classes contain much more functionality than we discuss in this tutorial. If you want more information on these classes, please refer to the NI-VISA .Net Framework help document that is installed with NI VISA. The following code shows how to perform these three operations in one line of code:
[C#]
mbSession = (MessageBasedSession)ResourceManager.GetLocalManager().
Open(resourceString.Text);
[VB .Net]
mbSession = CType(ResourceManager.GetLocalManager().
Open(resourceString.Text), MessageBasedSession)
The Open method of the ResourceManager objects accepts as a parameter the resource name of the instrument as a string. We discussed how to find the resource name for our instrument earlier in this tutorial.
In order to make our application more robust, we will add try and catch statements. Try and catch statements let us respond to any errors that occur during the execution of certain statements. In particular we will catch exceptions of the type InvalidCastException and all other exceptions by using the Exception type. The following code demonstrates how to implement try and catch statements:
[C#]
try
{
mbSession = (MessageBasedSession)ResourceManager.GetLocalManager().
Open(resourceString.Text);
}
catch(InvalidCastException)
{
MessageBox.Show("Resource selected must be a message-based session");
}
catch(Exception exp)
{
MessageBox.Show(exp.Message);
}
[VB .Net]
Try
mbSession = CType(ResourceManager.GetLocalManager().
Open(resourceString.Text), MessageBasedSession)
Catch exp As InvalidCastException
MessageBox.Show("Resource selected must be a message-based session")
Catch exp As Exception
MessageBox.Show(exp.Message)
End Try
Transferring Data
After creating a Session to communicate with our instrument, we can start transferring data to the instrument and reading its response. The three most common operations for communicating with a message-based instrument are query, write, and read. The query operation writes a command to an instrument and reads back the response. On the other hand, the write command only sends a command to the instrument, and the read command reads information from the instrument. The following piece of code demonstrates how to use the query operation:
[C#]
try
{
string responseString = mbSession.Query(stringToWrite.Text);
}
catch(Exception exp)
{
MessageBox.Show(exp.Message);
}
[VB .Net]
Try
Dim responseString As String = mbSession.Query(stringToWrite.Text)
Catch exp As Exception
MessageBox.Show(exp.Message
End Try
The read and write operations, because they are methods of the MessageBasedSession class, are implemented very similarly to the query operation. If you want more information on these operations, please refer to the NI-VISA .Net Framework help document that is installed with NI VISA.
Closing the VISA Session
To close the VISA session we created to communicate with our instrument, we must use the Dispose method of the MessageBasedSession class. This method releases any resources allocated to the session. The following code demonstrates how to dispose of a session:
[C#]
mbSession.Dispose();
[VB .Net]
mbSession.Dispose()
National Instruments VISA is a fast-and-easy solution for diverse instrument control. The VISA .Net API offer an object-oriented interface for easily communicating with your instrument.