Step 1—Initialize the Device
The first step in a VISA program is to open a VISA session to the device. This task is accomplished using either the viOpen() function in a text-based programming language or the VISA Open function in LabVIEW. The following figure shows the diagram of the LabVIEW VI created to initialize the PXI-6070E.
PXI-6070E Example eseriesInit.vi
This VI opens a VISA session to a PXI-6070E by calling VISA Open in LabVIEW. In the system displayed previously in MAX, the VISA resource name PXI2::13::INSTR would be supplied to the Instrument Handle input of VISA Open. The corresponding text-based function is viOpen(). The following figure shows the LabWindows/CVI code to toggle the digital lines of the PXI-6070E via the text-based version of the NI-VISA API. Refer to the following code for the details of calling the viOpen() function in LabWindows/CVI.
LabWindows/CVI Example
#include <ansi_c.h>
// To program PXI devices, you must define the following MACRO before
// you include the library "visa.h"
#if !defined NIVISA_PXI
#define NIVISA_PXI
#endif
// Include the VISA library
#include "visa.h"
int main (int argc, char *argv[])
{
// Variable declarations
ViUInt32 writeValue;
ViUInt32 bar1Base;
ViUInt16 modelCode;
ViUInt16 manufacturerID;
ViSession pxi6070E;
ViStatus status;
ViSession defaultRM;
ViAddr address;
ViAddr AddressOffset;
int offset;
// Open and Initialize the PXI-6070E
// Open the NI-VISA driver
status = viOpenDefaultRM (&defaultRM);
// Open a session to the PXI-6070E using its VISA resource
// name string "PXI1::10::INSTR"
status = viOpen (defaultRM, "PXI1::10::INSTR", VI_NULL, VI_NULL, &pxi6070E);
// Obtain the manufacturer's ID and models code
status = viGetAttribute (pxi6070E, VI_ATTR_MANF_ID, &manufacturerID);
status = viGetAttribute (pxi6070E, VI_ATTR_MODEL_CODE, &modelCode);
// Verify we have a PXI-6070E
if(manufacturerID != 0x1093)
return -1;
if(modelCode != 0x11B0)
return -1;
// Steps to initialize the MITE asic. These are specific
// to initializing the PXI-6070E; we will not go into
// detail about these steps.
status = viGetAttribute (pxi6070E, VI_ATTR_PXI_MEM_BASE_BAR1, &bar1Base);
writeValue = (bar1Base | 0x80);
status = viOut32 (pxi6070E, 11, 0xC0, writeValue);
// Map the block of memory we will write to using viPoke()
// and obtain a pointer to this memory
status = viMapAddress (pxi6070E, 12, 0, 0x1000, 0, VI_NULL, &address);
// Write the values to the registers
viPoke16 (pxi6070E, address, 0xB);
offset = (int)address;
offset = offset + 0x2;
status = viClose (pxi6070E);
status = viClose (defaultRM);
return 0;
}
There is one key difference between the LabVIEW example and the LabWindows/CVI example. The LabWindows/CVI example calls viOpenDefaultRM. You must call this function once at the beginning of every NI-VISA-based application to open and initialize the VISA driver. The handle to the driver session opened here is passed to subsequent calls to viOpen() to open sessions to specific devices. It is not necessary to explicitly call viOpenDefaultRM in LabVIEW because LabVIEW automatically calls it whenever a VI uses VISA. The error in input in the diagram of the figure above is an optional input in the example. This input would be supplied if you used eseriesInit.vi in sequence as a subVI with other subVIs that may need to pass in an error.
VISA attributes are a common way to access information about a device or its configuration. In the above examples, VISA attributes query the manufacturer ID and the model code of a PXI device to make sure the VISA session has been opened to the correct PXI device. In LabVIEW, a Property Node reads or writes VISA attributes for a device; in a text-based language, viGetAttribute() performs the same function.