Finding Resources Example (VB)

NI-VISA

Finding Resources Example (Visual Basic)

Note  The Visual Basic examples in the NI-VISA Help use the VISA data types where applicable. This feature is available only on Windows. To use this feature, select the VISA library (visa32.dll) as a reference from Visual Basic. This makes use of the type library embedded into the DLL.

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
Dim dfltRM
Dim sesn
Dim fList
Dim desc
Dim nList
Dim iManf
Dim iModel
As ViStatus
As ViSession
As ViSession
As ViFindList
As String * VI_FIND_BUFLEN
As Long
As Integer
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