Notes on Using viRead/viWrite in Visual Basic 6

Agilent VISA.NET

Notes on Using viRead/viWrite in Visual Basic 6

The visa32.bas file supplied with VISA declares the buffer parameter in viRead and viWrite as a String data type.  If you need to read or write data to a numeric array, it can be very cumbersome and inefficient to convert string data to numeric array data.  It is much more efficient to add new declarations for viRead and viWrite to the visa32.bas file and call through these modified declarations.  This allows you to read and write directly to a numeric array without having to convert to or from a string.

Note: Do not modify the original visa32.bas file that was installed with the IO Libraries Suite.  Instead, copy visa32.bas to your local project directory and modify the copy.  When using the Project > Add Module command in VB6, be sure you add the local copy of the visa32.bas file to your project and not the visa32.bas file from the original installation directory!

VB6 does not allow overloaded function declarations; this means that if you need to declare additional functions, their names must be unique.

Using viRead as an example, the original declaration in visa32.bas for viRead is as follows:

Declare Function viRead Lib "VISA32.DLL" Alias "#256" _
ByVal vi As Long, ByVal Buffer As String, ByVal count As Long, retCount As Long) As Long

To create versions of viRead that can handle a byte and an integer array, you can copy the viRead declaration in visa32.bas and insert two copies of it back into the file, but rename these copies and change the buffer data type as shown:

Declare Function viRead Lib "VISA32.DLL" Alias "#256" _
(ByVal vi As Long, ByVal Buffer As String, ByVal count As Long, retCount As Long) As Long

Declare Function viReadByte Lib "VISA32.DLL" Alias "#256" _
(ByVal vi As Long, ByRef Buffer As Byte, ByVal count As Long, retCount As Long) As Long

Declare Function viReadInteger Lib "VISA32.DLL" Alias "#256"
(ByVal vi As Long, ByRef Buffer As Integer, ByVal count As Long, retCount As Long) As Long 

The modified portions of the new declarations are shown in bold.  If you need to read additional data types, you can create your own modified function declarations for the data types you need.

Note that in the original declaration, the string was declared as ByVal.  This is needed because VB6 will properly marshal a unicode VB6 string to an ASCII string and pass the pointer to the start of the ASCII buffer when the string is passed 'ByVal'.  For numeric arrays, however, no marshalling is performed; you need to pass the address of the beginning of the numeric array to VISA.  That requires the ByRef keyword.

Once these new declarations have been added, you can call the newly declared functions from your VB6 program as follows:

Dim retCount As Long
Dim arraySize As Long
Dim intArray(20) As Integer
'
' Set arraySize and retCount to the maximum number
' of elements that the array can hold.
'
arraySize = (UBound(intArray) - LBound(intArray) + 1) * 2
'
' Read the array from the test instrument and set
' the retCount variable number of bytes read.
' Remember that retCount needs to be divided
' by the size each element to get the number of
' elements read.
'
err = viReadInteger(vi, intArray(0), arraySize, retCount)

You can add declarations for viWrite variations if you want to use viWrite to handle numeric arrays directly.

Remember, though, that the count variables indicate the size in bytes, not in elements.  You must adjust the count value based on the size of the numeric element you are reading and writing.

See Also

viRead, viWrite
Using the VISA C API in Microsoft Visual Basic 6