| Getting Started > Getting Started Sample Code |
This Getting Started sample is designed to provide a simple working VISA.NET program that will enable programmers who are new to VISA.NET to get started quickly. It illustrates the basics of instantiating VISA.NET, sending a *IDN? query to an instrument, and reading the response.
Before you begin, make sure you have:
MyInstr
These steps are described in Getting Started.
The Main routine in this code sample instantiates a GPIB session, then calls PrintfAndFlush to send the *IDN? string to the instrument, and Scanf to read the results. Finally, it displays the results in a console window and closes VISA.NET.
The sample illustrates several basic VISA.NET constructs, including resources, sessions, exception handling, and Formatted I/O.
The first line of code in the Main routine opens a session to an instrument with the alias MyInstr. The instrument conforms to the IEEE 488.2 standard, and so we know that it supports the I/O operations in the rest of the sample.
This code creates a connection to the instrument (an instrument session or session) by calling the GpibSession constructor with the alias. If this call succeeds, it returns an instance of GpibSession with an open connection to the instrument.
If the constructor fails, it throws an exception. If this happens, there is no way for the program to recover, so the exception is not handled and propagates up the stack. You can look at the exception details in the debugger. The constructor will fail, for example, if VISA.NET cannot find an instrument at the specified address.
| Initialize |
Copy Code |
|---|---|
|
GpibSession session = new GpibSession ( Ivi.Visa.AccessModes.None, 2000); | |
Note that you can easily change the sample to use an instrument at a different GPIB address by changing the alias to refer to a different instrument, or by using the instrument address directly in your code, in place of the alias. You can change the sample to use a USB, LAN, or Serial address by changing the instrument to which the alias refers, and by calling the constructor for UsbSession, TcpipSession, or SerialSession respectively. You can find the VISA address of a connected instrument in the Keysight Connection Expert utility.
The sample uses two Formatted I/O methods, PrintfAndFlush and Scanf, to perform a *IDN? query to the instrument. These methods are defined in a standard IVI implementation of the IMessageBasedFormattedIO interface. The sample uses a direct reference to this interface to access the implemented methods. The following code creates the reference.
| Formatted I/O |
Copy Code |
|---|---|
| IMessageBasedFormattedIO io = session.FormattedIO; | |
Now that we have an open connection (session) to the instrument, we can send commands and read results. In the sample we will send a *IDN? query to the instrument using the PrintfAndFlush method. PrintfAndFlush automatically appends a termination character (newline, in this case) and an END to the specified string, sends it to the instrument, and flushes the Formatted I/O write buffer.
To illustrate exception handling, the sample uses a try/catch block to catch any exceptions that PrintfAndFlush might throw. If the program catches an exception, it cleans up after itself by calling Dispose and closing the session, and the method returns.
| Send *IDN? |
Copy Code |
|---|---|
try { io.PrintfAndFlush("*IDN?"); } catch { Console.WriteLine("PrintfAndFlush failed"); session.Dispose(); session = null; return; } | |
In the sample we will use the Scanf method to read the result as a comma-separated array of strings where the size of the array is not known. Scanf reads the array into the response argument. Since the array size is not specified, the size is determined by the number of comma-separated elements returned by the instrument. A typical instrument returns two to four elements in response to a "*IDN?" query. Note that this program assumes that newline is the termination character (IEEE 488.2 compliant).
It is possible for response to contain 2, 3, or 4 strings, depending on the instrument. The code checks for a trailing '\n' (newline) character at the end of the last three strings and eliminates it. Since the call always returns at least two strings, we do not need to check the first string for a newline.
Again, we use a try/catch block to catch any exceptions that Scanf might throw. If an exception is caught, the program cleans up after itself by calling Dispose and closing the session, and the method returns.
| Read Result |
Copy Code |
|---|---|
string[] response = new string[]{ "", "", "", "" }; try { io.Scanf("%,s", out response); } catch { Console.WriteLine("Scanf failed"); session.Dispose(); session = null; return; } Console.WriteLine("Manufacturer: {0}", response[0]); Console.WriteLine("Instrument Model: {0}", response[1].TrimEnd(new char[] {'\n'})); if (response.Length > 2) { Console.WriteLine("Firmware Revision: {0}", response[2].TrimEnd(new char[] {'\n'})); } if (response.Length > 3) { Console.WriteLine("Serial Number: {0}", response[3].TrimEnd(new char[] {'\n'})); } | |
After performing the desired I/O, the program cleans up after itself by calling Dispose and closing the session. The two lines that call Console methods keep the console window from closing before the output can be read.
| Close Session |
Copy Code |
|---|---|
session.Dispose(); session = null; Console.WriteLine("Press any key to end..."); Console.ReadKey(); | |
The code for the complete sample is shown below. Note that, to clean up the code, we have combined the two try/catch blocks and the code to close the session.
| Getting Started |
Copy Code |
|---|---|
|
using System; using Ivi.Visa; using Keysight.Visa;
namespace IdnSample { class IdnSample { static void Main(string[] args) { GpibSession session = new GpibSession ("MyInstr", Ivi.Visa.AccessModes.None, 2000); try { IMessageBasedFormattedIO io = session.FormattedIO; io.PrintfAndFlush("*IDN?"); string[] response = new string[] { "", "", "", "" }; io.Scanf("%,s", out response); Console.WriteLine("Manufacturer: {0}", response[0]); Console.WriteLine("Instrument Model: {0}", response[1].TrimEnd(new char[] {'\n'})); if (response.Length > 2) { Console.WriteLine("Firmware Revision: {0}", response[2].TrimEnd(new char[] {'\n'})); } if (response.Length > 3) { Console.WriteLine("Serial Number: {0}", response[3].TrimEnd(new char[] {'\n'})); } } catch { Console.WriteLine("*IDN? query failed"); } finally { session.Dispose(); session = null; } Console.WriteLine("Press any key to end..."); Console.ReadKey(); } } } | |
When you build and run this program, you'll see a console window displaying the instrument's identifying information.