viVPrintf/viVScanf Example using String

Agilent VISA.NET

viVPrintf/viVScanf Example using String

Sub Test_String() ' Using viVScanf and viVPrintf in Visual Basic 6 is tricky because ' of the way VB6 passes arguments on the stack. The 'vararg' ' argument to the data needs to be a 'byRef' pointer -- which means ' it needs to be a pointer to a pointer. ' ' VB treats string arrays passed to external DLL's as a special case ' so when a string is passed, it ends up being passed correctly as a ' pointer to a pointer. ' ' Numeric arrays, on the other hand, cannot be passed directly. To ' pass a pointer to the numeric array pointer, we create another ' variable which we set to the address of the start of the array ' and pass that variable 'byRef' to the function. ' ' Declare variables used by VISA ' Dim err As Long Dim drm As Long Dim vi As Long Dim buf As String ' ' Open a default resource manager session and open the device ' err = viOpenDefaultRM(drm) err = viOpen(drm, "GPIB2::2::INSTR", 0, 0, vi) ' ' ================================================== ' Read a string array using viVScanf ' ================================================== ' Dim buf256 As String * 256 ' pre-allocated, string Dim posn As Long ' ' Tell the instrument to send its IDN string ' err = viVPrintf(vi, "*IDN?" + vbLf, 0) ' ' Read the string from the instrument ' err = viVScanf(vi, "%t", buf256) ' ' Truncate the returned string at the first null ' (A null terminated string is returned) ' posn = InStr(buf256, String(1, 0)) If (posn > 0) Then buf = Left(buf256, posn - 1) Else buf = buf256 End If ' ' display the string that was read from the instrument ' MsgBox buf, 0, "viVScanf of a String" ' ' Close the VISA sessions ' err = viClose(vi) err = viClose(drm) End Sub

See Also

viVPrintf, viVScanf Using the VISA C API in Microsoft Visual Basic 6