Handling arrays is a bit different than handing strings or doubles.  Recall that you use ReadString and WriteString to handle data with an instrument.  In the case of handling arrays, use ReadList to read the array from the instrument and WriteList to send arrays to an instrument.  With binary data, use ReadIEEEBlock and WriteIEEEBlock.

Reading Arrays

When the instrument returns multiple readings as a comma separated list, it's only a matter of declaring the variable as an array. For example, the IDN of an instrument is an array of four strings. The code below reads the IDN of an instrument and displays each part of the array as a string in four separate message boxes:

    Dim idn() As String
    Dim i As Integer
    instrument.WriteString "*IDN?"
    idn = instrument.ReadList(VisaComLib.IEEEASCIIType.ASCIIType_BSTR, ",")
    For i = (LBound(idn)) To (UBound(idn))
        MsgBox ("At index " & i & ", the idn is " & idn(i))
    Next i

The ReadList method accepts two arguments:  the IEEEASCIIType of the array you are reading and the list separator as a String.  The method returns each part of the array as a string at separate indexes according to their order.  In this example, ReadList reads the array as a string and separates each part of the array when it sees a comma. 

Use ReadList to read an array of numbers.  Simply change the IEEEASCII type to number a format.

    Dim preamble() As Double
    scope.WriteString "Waveform:Preamble?"
    preamble = .ReadList(VisaComLib.IEEEASCIIType.ASCIIType_R8)

In this example, the WriteString command sends a preamble query to an oscilloscope. The preamble is an array of numbers. ReadList reads the numbers in the array each as a double and stores it into preamble.

Writing Arrays

Writing an array of data to an instrument is handled similarly to sending a double to an instrument.  Before using WriteList, use the WriteString command to allocate space in memory for the array of data.  The following code sends a 4-step staircase waveform as an array to an Keysight function generator.

    Dim i As Integer
    Dim dblSend(3) As Double
    ' create the waveform
    For i = 0 To 3
        dblSend(i) = i * 0.25
    Next i
    With arbGen
        .WriteString "Data Volatile, ", False
        .WriteList dblSend, VisaComLib.IEEEASCIIType.ASCIIType_BSTR, ","
    End With

After the array of data is created, it is written to volatile memory in the function generator.  WriteString accepts two arguments:  a string to send to the instrument and a Boolean value that indicates whether or not to append the data from the next line of code to the string from the first argument.  In this example, WriteString tells the instrument to put the following data in volatile memory and then appends the data on the WriteList line.

The WriteList command accepts three arguments: the data to write, the IEEEASCII type to write the data as, and a list separator. This example tells WriteString to write the array dblSend as a string with values separated by commas to volatile memory. The two commands in this example work together to send an array of numbers to the instrument.

Note:  The following code will set up the function generator so that you can view the staircase on an oscilloscope.  Consult your function generator's user manual for more information.  If the waveform does not show up on the oscilloscope screen, press the Autoscale button on the oscilloscope.

With arbGen
    .WriteString "*RST"                          ' instrument reset
    .IO.Timeout = 5000                           ' ensure completion
    .WriteString "Data:Delete:All"               ' clears memory
    ' insert code here
    .WriteString "Output:Load 50"                ' output termination is 50 ohms
    .WriteString "Data:Copy DampSine, Volatile"  ' copy arb to non-volatile memory
    .WriteString "Function:User DampSine"        ' select the active arb waveform
    .WriteString "Function:Shape User"           ' output selected arb waveform
    .WriteString "Frequency 5000; Voltage 1"     ' freq is 5kHz at 1 Vpp
    .WriteString "Output ON"                     ' use with 33220A
End With

Reading Binary Blocks

Sometimes, instruments output data as a list of numbers or an IEEE binary block. The following example shows how to read the staircase waveform on the oscilloscope created in the previous example:

    Dim ydata() As Byte
    Dim i As Integer
    With scope
        .WriteString "Waveform:points 250; Source Channel1"        
        .WriteString "Waveform:Format byte"
        .WriteString "Waveform:data?"
        ydata = .ReadIEEEBlock(VisaComLib.IEEEBinaryType.BinaryType_UI1)
    End With

The ReadIEEEBlock method inserts the data into the byte array, ydata(). ReadIEEEBlock passes an IEEE binary type that corresponds to the data you are reading. In this case, we used a one byte unsigned integer to read the array of bytes.

Writing Binary Blocks

Function generators can accept IEEE binary blocks of data. Here is an example of how to send a binary block of data to a function generator. First, use this code to store the points of a damped sine wave into the array, intSend.

    Dim intSend() As Integer
    Dim numTemp As Single
    Dim intTemp As Integer
    Dim lastPoint As Long
    Dim TwoPiF As Double
    Dim i As Long
    Const pi = 3.1416
    Const dampFactor = -5
    Const NumberCycles = 10
    lastPoint = UBound(intSend) * 0.875
    TwoPiF = 2 * pi * NumberCycles / lastPoint
    For i = 1 To lastPoint
        numTemp = Sin(TwoPiF * i) * 2047
        intSend(i) = CInt(numTemp * Exp(dampFactor * i / lastPoint))
    Next i
    For i = i To UBound(intSend)
        intSend(i) = CInt(0)
    Next i

Use WriteIEEEBlock to send a binary block to an instrument. The following code will write an IEEE block to a function generator's volatile memory.

    arbGen.WriteIEEEBlock "Data:Dac Volatile,", intSend

The first argument of the command WriteIEEEBlock indicates where to write the IEEE block; the second argument indicates what data to write. The WriteIEEEBlock command writes the array intSend into volatile memory. For more information on instrument commands, consult the instrument's user manual.