Finding Resources

NI-VISA

Finding Resources

As shown in the previous section, you can create a session to a resource using the viOpen() call. However, before you use this call you need to know the exact location (address) of the resource you want to open. To find out what resources are currently available at a given point in time, you can use the search services provided by the viFindRsrc() operation, as shown in the following example.

Example

Note  This example shows C source code. There is also an example in Visual Basic syntax.

#include "visa.h"

#define MANF_ID
#define MODEL_CODE
0xFF6
0x0FE
/* 12-bit VXI manufacturer ID of device */
/* 12-bit or 16-bit model code of device */

/* Find the first matching device and return a session to it */
ViStatus autoConnect(ViPSession instrSesn)
{

ViStatus
ViSession
ViFindList
ViChar
ViUInt32
ViUInt16
status;
defaultRM, instr;
fList;
desc[VI_FIND_BUFLEN];
numInstrs;
iManf, iModel;
status = viOpenDefaultRM(&defaultRM);
if (status < VI_SUCCESS) {

/* Error initializing VISA ... exiting */
return status;

}
/* Find all VXI instruments in the system */
status = viFindRsrc(defaultRM, "?*VXI?*INSTR", &fList, &numInstrs, desc);
if (status < VI_SUCCESS) {

/* Error finding resources ... exiting */
viClose(defaultRM);
return status;

}

/* Open a session to each and determine if it matches */
while (numInstrs--) {

status = viOpen(defaultRM, desc, VI_NULL, VI_NULL, &instr);
if (status < VI_SUCCESS) {

viFindNext(fList, desc);
continue;

}
status = viGetAttribute(instr, VI_ATTR_MANF_ID, &iManf);
if ((status < VI_SUCCESS) || (iManf != MANF_ID)) {

viClose(instr);
viFindNext(fList, desc);
continue;

}
status = viGetAttribute(instr, VI_ATTR_MODEL_CODE, &iModel);
if ((status < VI_SUCCESS) || (iModel != MODEL_CODE)) {

viClose(instr);
viFindNext(fList, desc);
continue;

}

/* We have a match, return the session without closing it */
*instrSesn = instr;
viClose(fList);
/* Do not close defaultRM, as that would close instr too */
return VI_SUCCESS;

}

/* No match was found, return an error */
viClose(fList);
viClose(defaultRM);
return VI_ERROR_RSRC_NFOUND;

}

As this example shows, you can use viFindRsrc() to get a list of matching resource names, which you can then further examine one at a time using viFindNext(). Remember to free the space allocated by the system by invoking viClose() on the list reference fList.

Notice that while this sample function returns a session, it does not return the reference to the resource manager session that was also opened within the same function. In other words, there is only one output parameter, the session to the instrument itself, instrSesn. When your program is done using this session, it also needs to close that corresponding resource manager session. Therefore, if you use this style of initialization routine, you should later get the reference to the resource manager session by querying the attribute VI_ATTR_RM_SESSION just before closing the INSTR session. You can then close the resource manager session with viClose().