среда, 23 июля 2008 г.

Using Visa Interop in .NET applications to control instruments

The Virtual Instrumentation Software Architecture (VISA) is a standard intended to communicate with instruments using the same API regardless of instrument vendor and what communications bus the instrument uses. VISA currently supports many interface buses (like GPIB, VXI, serial, PXI, USB, Ethernet) and will expand in the future. VISA was developed by the VXIplug&play Systems Alliance (http://www.vxipnp.org/) which is now part of the IVI Foundation. VISA has specifications for API versions in C and COM, so there are two ways to work with VISA in your .NET applications: you can write wrapper around C library (actualy it is already written by Agilent) or you can use Visa COM interop. The first approach is described in excellent David Gladfelter's article The VISA I/O API & .NET. In this article I describe the second aproach.
You need to download and to install VISA Library from a specific vendor. For example you can use National Instruments VISA or Agilent's implementation. Actualy these packages contain a lot of tools, different IO libraries, etc. But you really need only VISA COM library and its Primary Interop Assemblies (PIA). VISA PIAs are distributed by the IVI Foundation and are vendor-independent. Since your .NET application will have references only to the PIA it will work with any specific VISA-COM implementations.
Create a new project in Visual Studion and add reference to VISA COM Type Library
(In the Solution Explorer window, right-click References and click Add Reference. Alternately, you may use the Project > Add Reference menu. At the top of the Add Reference window, click the COM tab. Highlight VISA COM 3.0 Type Library. Click Select to select the VISA COM 3.0 libraries and click OK.)
Now we can write some code.
Using Ivi.Visa.Interop namespace:

using Ivi.Visa.Interop;

First of all we need an instance of ResourceManager. We also need

ResourceManager ioMgr = new ResourceManagerClass();

Let's get a list of available resources:

string[] resources = ioMgr.FindRsrc("?*");

Suppose our device has VISA address "ASRL1::INSTR". We should open VISA session:

FormattedIO488 instrument=new Ivi.Visa.Interop.FormattedIO488Class();
instrument.IO = (IMessage)ioMgr.Open("ASRL1::INSTR", AccessMode.NO_LOCK, 1000, "");

FormattedIO488 has methods WriteString and ReadString and we can use them to comunicate with the device. Let's send "*IDN?" to identify the device.

string cmd="*IDN?";
instrument.WriteString(cmd, true);
string responseString = instrument.ReadString();

When we finished work we have to close VISA session and release resources:

try
{
instrument.IO.Close();
}
catch {}
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(instrument);
}
catch {}
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(ioMgr);
}
catch {}


It will be better to surround ResourceManager.Open, FormattedIO488.ReadLine and FormattedIO488.WriteLine calls with apropriate try-catch block.

See also Agilent's article Using VISA COM I/O API in .NET.

Комментариев нет: