Using the Measurement Studio NI-488.2 .NET Library

NI-488.2 .NET Class Library Help for Visual Studio 2010

Edition Date: August 2012

Part Number: 370628F-01

»View Product Info
Download Help (Windows Only)

See Also


The NationalInstruments.NI4882 namespace contains classes that provide a .NET interface to the NI-488.2 device driver. You can use the NI-488.2 device driver to communicate with devices, such as bench-top instruments, over the General Purpose Interface Bus (GPIB). You can use the NI-488.2 device driver also to control GPIB interfaces, such as GPIB cards.

Choosing an Instrument Control API

To communicate with an instrument, you can use instrument drivers, VISA, and bus-specific or interface-specific APIs such as NationalInstruments.NI4882. In a typical application, National Instruments recommends that you use an instrument driver if one is available for the instrument. If a suitable instrument driver is unavailable, use VISA or an interface-specific API. Use the NationalInstruments.VisaNS class library if you need to communicate over more than one bus or interface type or if a suitable .NET class library does not exist for the bus or interface type. If you are communicating with instruments solely over the GPIB bus, consider using the NationalInstruments.NI4882 class library.

Refer to the following sections for descriptions of NationalInstruments.NI4882 core concepts:

Using Devices and Boards

The NationalInstruments.NI4882 namespace includes .NET classes that provide a rich, object-oriented interface to the NI-488.2 C API. In a typical NationalInstruments.NI4882 application, instantiate Device objects to communicate with GPIB devices. In the Device constructor, specify the GPIB board number and the device address. Call methods to configure the device and to communicate with the device. You must call Dispose when you no longer need to access the device.

In an application that requires direct control of the GPIB bus or must grant talker/listener status explicitly, instantiate Board objects rather than Device objects. Call methods to configure the board and communicate with the devices connected to the board. You must call Dispose when you no longer need to access the board or devices connected to it.

Using I/O Operations

GPIB communication consists of transferring strings or byte arrays between devices and the host computer. The Device and Board classes contain member functions that read data from and write data to GPIB devices. Device and Board include synchronous and asynchronous read and write methods. Synchronous methods perform the entire operation and return when the operation completes. Asynchronous methods start the operation, return immediately, and call a delegate you provide or hold the data until you request it. If you use asynchronous methods, the application can perform non-NationalInstruments.NI4882 operations while the GPIB operation is in progress. You cannot perform NationalInstruments.NI4882 operations during an asynchronous GPIB operation. NationalInstruments.NI4882 operations throw GpibException if you attempt an NationalInstruments.NI4882 operation while an NationalInstruments.NI4882 asynchronous operation is in progress.

Board and Device contain the following commonly-used write methods:

Board and Device contain the following commonly-used read methods:

For more information on performing asynchronous I/O using the NI-4882 .NET library, refer to Using Asynchronous I/O with the NI-488.2 .NET Class Library.

Handling a Service Request

Some GPIB applications must detect and respond to service requests from devices on the bus. To request service, a device asserts the Service Request Line (SRQ) on the GPIB. The application must subsequently determine which device asserted the SRQ line and respond accordingly. Most applications use serial polling to determine which device asserted the SRQ line. Alternatively, applications can use parallel polling to determine which device asserted the SRQ line. Parallel polling is more complex to implement than serial polling but is typically more efficient than serial polling.

Serial Polling

In serial polling, the GPIB board independently queries each device attached to it to determine if it is the device that requested service. Call SerialPoll to query one or more devices connected to a GPIB board. Call SerialPoll to query a specific device. SerialPoll and SerialPoll return a status byte from the queried device or devices. The device that requested service sets bit 0x40 of the status byte. The devices that did not request service clear bit 0x40 of the status byte.

Parallel Polling

In parallel polling, the GPIB controller queries up to eight devices at a time to determine if any of them requested service. Before parallel polling, the controller must explicitly configure each device to respond to the parallel poll query. Call ParallelPollConfigure or ParallelPollConfigure to configure how a device responds to a parallel poll. Call ParallelPoll or ParallelPoll to query devices configured to respond to a parallel poll. ParallelPoll and ParallelPoll return the parallel poll response byte. Each bit in the response byte corresponds to a queried device. Which bit corresponds to which device is specified in the calls to ParallelPoll. The value of the bit specifies if the corresponding device requested service. For more information on parallel polling, refer to the NI-488.2 Online Help.

Using Error Handling

The NI-488.2 driver returns error codes that indicate the success or failure of an operation. When an NationalInstruments.NI4882 method call or property access results in an error in the NI-488.2 driver, the NationalInstruments.NI4882 class creates and throws a GpibException or a .NET Framework exception with InnerException set to an instance of GpibException. ErrorCode specifies the error code that the NI-488.2 driver operation returned. ErrorMessage specifies the error message for the corresponding error code.

Note: For a list of all NI-488.2 error codes, refer to the iberr Error Codes and Solutions topic in the NI-488.2 Help. To access the NI-488.2 Help from the Start menu, select Start»Programs»National Instruments»NI-488.2»NI-488.2 Help .

Differences Between .NET and NI-488.2 Locking

.NET locking and NI-488.2 locking synchronize access to two different things.

When writing .NET code, you can synchronize access to a particular section of code using the Monitor class or the lock statement in C#. Locking in .NET ensures that if a particular section of code is locked that only one thread can access it at a time. This method proves useful when you want to prevent other programs or threads from accessing a critical piece of code that should not be accessed simultaneously.

Alternatively, you can synchronize access to a particular NI-488.2 board by using the AcquireInterfaceLock and ReleaseInterfaceLock methods. When a board is locked, no other code can access that board while it is locked no matter in which thread or process the code resides. This method proves useful when you need exclusive access to a board.

See Also

WAS THIS ARTICLE HELPFUL?

Not Helpful