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.

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 the corresponding resource manager session. 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().

C Example

#include "visa.h"

  #define MANF_ID        0xFF6    /* 12-bit VXI manufacturer ID of device */
  #define MODEL_CODE     0x0FE    /* 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     status;
    ViSession    defaultRM, instr;
    ViFindList   fList;
    ViChar       desc[VI_FIND_BUFLEN];
    ViUInt32     numInstrs;
    ViUInt16     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;
    }

Visual Basic Example

Rem Find the first matching device and return a session to it
Private Function AutoConnect(instrSesn As ViSession) As ViStatus

    Const MANF_ID = &HFF6 '12-bit VXI manufacturer ID of a device
    Const MODEL_CODE = &H0FE '12-bit or 16-bit model code of a device

   Dim stat    As ViStatus
   Dim dfltRM  As ViSession  
   Dim sesn    As ViSession
   Dim fList   As ViFindList
   Dim desc    As String * VI_FIND_BUFLEN
   Dim nList   As Long
   Dim iManf   As Integer
   Dim iModel  As Integer 

    stat = viOpenDefaultRM(dfltRM)
    If (stat < VI_SUCCESS) Then

        Rem Error initializing VISA ... exiting
        AutoConnect = stat
        Exit Function

    End If

    Rem Find all VXI instruments in the system
    stat = viFindRsrc(dfltRM, "?*VXI?*INSTR", fList, nList, desc)
    If (stat < VI_SUCCESS) Then

        Rem Error finding resources ... exiting
        viClose (dfltRM)
        AutoConnect = stat
        Exit Function

    End If

    Rem Open a session to each and determine if it matches
    While (nList)

        stat = viOpen(dfltRM, desc, VI_NULL, VI_NULL, sesn)
        If (stat >= VI_SUCCESS) Then

            stat = viGetAttribute(sesn, VI_ATTR_MANF_ID, iManf)
            If ((stat >= VI_SUCCESS) And (iManf = MANF_ID)) Then

                stat = viGetAttribute(sesn, VI_ATTR_MODEL_CODE, iModel)
                If ((stat >= VI_SUCCESS) And (iModel = MODEL_CODE)) Then

                    Rem We have a match, return session without closing
                    instrSesn = sesn
                    stat = viClose (fList)
                    Rem Do not close dfltRM; that would close sesn too
                    AutoConnect = VI_SUCCESS
                    Exit Function

                End If

            End If
            stat = viClose (sesn)

        End If
        stat = viFindNext(fList, desc)
        nList = nList - 1

    Wend
    Rem No match was found, return an error
    stat = viClose (fList)
    stat = viClose (dfltRM)
    AutoConnect = VI_ERROR_RSRC_NFOUND

End Function