viVQueryf Example with String and Indefinite Length Block

Agilent VISA.NET

viVQueryf Example with String and Indefinite Length Block

Sub Test_viVQueryf() ' 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) ' ' ================================================== ' Write a String and read an indefinite length block ' ================================================== ' ' Set up the test instrument to send an IEEE-488 ' indefinite length block terminated with a line-feed. ' err = viVPrintf(vi, "RECEIVE" & vbLf, 0) err = viVPrintf(vi, "#0abcdefghij" & vbLf, 0) ' This now done in viVQueryf: '     ----> err = viVPrintf(vi, "SEND" & vbLf, 0) ' ' Declare the byte array, a count variable and a ' array to hold the parameter addresses for viVScanf ' Dim retCount As Long Dim byteArray(20) As Byte Dim paramsArray(3) As Long ' ' Create and initialize a string for the 'writeFmt' ' parameter of viVQueryf. Note that we can't put ' pointers to strings in the paramsArray because ' strings in VB are unicode and viVQueryf needs ' a ascii strings. ' Dim writeFmtString As String writeFmtString = "SEND" ' ' Put the parameter addresses in the array ' paramsArray(0) = VarPtr(retCount) paramsArray(1) = VarPtr(byteArray(0)) ' ' Set 'retCount to the maximum number of elements ' that the array can hold. ' retCount = UBound(byteArray) - LBound(byteArray) + 1 ' ' Read the array from the test instrument and set ' the retCount variable to the actual count of ' elements read. Note that a line-feed is appended ' to the format strings. On the writeFmt string, ' this is causes a flush of the formatted write buffer ' to the device. The line-feed must be in the format ' and not simply in the data since a line-feed in the ' data will not cause a flush. On the readFmt string, ' the line-feed ensures that we flush the new-line out ' of the read buffer and don't leave it for a future ' formatted read to trip over. ' err = viVQueryf(vi, writeFmtString & vbLf, "%#b" & vbLf, paramsArray(0)) ' ' Display some of the results ' buf = Format(err, "Error = ##########0") & vbLf & _ Format(retCount, "Retur\n \Cou\nt = ##########0") & vbLf & _ Chr(byteArray(0)) & Chr(byteArray(1)) & Chr(byteArray(2)) & _ Chr(byteArray(3)) & Chr(byteArray(4)) & Chr(byteArray(5)) & _ Chr(byteArray(6)) & Chr(byteArray(7)) & Chr(byteArray(8)) & _ Chr(byteArray(9)) & vbLf MsgBox buf, 0, "viVQueryf of an IEEE 488 Indefinite Length Byte Array" ' ' Close the VISA sessions ' err = viClose(vi) err = viClose(drm) End Sub

See Also

viVQueryf Using the VISA C API in Microsoft Visual Basic 6