viVScanf Example Reading an IEEE 488 Definite Length Block and Returning a Byte Array

Agilent VISA.NET

viVScanf Example Reading an IEEE 488 Definite Length Block and Returning a Byte Array

Sub Test_DefiniteLengthBlock() ' 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 an IEEE 488 definite length block ' ================================================== ' ' Set up the test instrument to send an IEEE-488 ' definite length block terminated with a line-feed. ' err = viVPrintf(vi, "RECEIVE" & vbLf, 0) err = viVPrintf(vi, "#210abcdefghij" & vbLf, 0) 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(2) As Long ' ' 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 the format string ' specifies the line-feed -- this is needed to ' sure the line-feed is flushed from the formatted ' IO buffer. ' err = viVScanf(vi, "%#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, "viVScanf of an IEEE 488 Definite Length Byte Array" ' ' Close the VISA sessions ' err = viClose(vi) err = viClose(drm) End Sub

See Also

viVScanf Using the VISA C API in Microsoft Visual Basic 6