viVScanf Example Returning a Double Array

Agilent VISA.NET

viVScanf Example Returning a Double Array

Sub Test_Double() ' 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 double array ' ================================================== ' ' Set up the test instrument to send a comma ' separated array of floating point values ' terminated with a line-feed. ' err = viVPrintf(vi, "RECEIVE" & vbLf, 0) err = viVPrintf(vi, "1.1,2.2,3.3,4.4,5.5" & vbLf, 0) err = viVPrintf(vi, "SEND" & vbLf, 0) ' ' Declare the numeric array, a count variable and a ' array to hold the parameter addresses for viVScanf ' Dim retCount As Long Dim dblArray(20) As Double Dim paramsArray(2) As Long ' ' Put the parameter addresses in the array ' paramsArray(0) = VarPtr(retCount) paramsArray(1) = VarPtr(dblArray(0)) ' ' Set 'retCount to the maximum number of elements ' that the array can hold. ' retCount = UBound(dblArray) - LBound(dblArray) + 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, "%,#lf" & vbLf, paramsArray(0)) ' ' Display some of the results ' buf = Format(err, "Error = ##########0") & vbLf & _ Format(retCount, "Retur\n \Cou\nt = ##########0") & vbLf & _ Format(dblArray(0), "##0.0, ") & vbLf & _ Format(dblArray(1), "##0.0, ") & vbLf & _ Format(dblArray(2), "##0.0, ") & vbLf & _ Format(dblArray(3), "##0.0, ") & vbLf & _ Format(dblArray(4), "##0.0, ") & vbLf MsgBox buf, 0, "viVScanf of a Double 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