Reading Data in Binary Format

Other topics about Sample Programs

Overview

This sample program demonstrates how to retrieve formatted data arrays in the Binary transfer format.

This program holds the sweep on channel 1, then retrieves and displays the stimulus array.

See Retrieving Measurement Results for this programming.

Sample Program in Excel VBA using VISA

Sub read_bin_Click()

    Dim defrm As Long

    Dim vi As Long

    Dim Result As String * 10000

    Dim Res() As Double

    Dim Nop As Long

    Const TimeOutTime = 10000

    ' Open Analyzer

    Call viOpenDefaultRM(defrm)

    Call viOpen(defrm, "GPIB0::17::INSTR", 0, 0, vi)

    Call viSetAttribute(vi, VI_ATTR_TMO_VALUE, TimeOutTime)

    '

    Call viVPrintf(vi, ":CALC1:PAR1:SEL" + vbLf, 0)

    Call viVPrintf(vi, ":INIT1:CONT OFF" + vbLf, 0)

    Call viVPrintf(vi, ":ABOR" + vbLf, 0)

    '

    ' Reading out Measurement Frequency Data in Binary transfer format

    Call viVPrintf(vi, ":FORM:DATA REAL" + vbLf, 0)

    Call viVPrintf(vi, ":CALC1:DATA:FDAT?" + vbLf, 0)

    Call Scpi_read_binary_double_array(vi, Res, Nop)

    '

    ' Write data in cells of Excel

    Range("A6:D1607").Clear

    For i = 0 To Nop - 1

        j = i Mod 2

        k = i \ 2

        Cells(k + 6, j + 3).Value = Res(i)

    Next i

    '

    ' Read out Measurement Frequency Data in Binary transfer format

    Call viVPrintf(vi, ":SENS1:FREQ:DATA?" + vbLf, 0)

    Call Scpi_read_binary_double_array(vi, Res, Nop)

    '

    ' Write data in cells of Excel

    For i = 0 To Nop - 1

        Cells(i + 6, 1) = i + 1

        Cells(i + 6, 2).Value = Res(i)

    Next i

    ' Close

    Call viClose(vi)

    Call viClose(defrm)

End Sub

    '===================================

    ' BinaryAry Read Subroutine

    '===================================

Sub Scpi_read_binary_double_array(vi As Long, data() As Double, Nop As Long)

    Dim dblArray(10000) As Double

    Dim paramsArray(3) As Long

    Dim err As Long

    Dim i As Long

    Dim lf_eoi As String * 1

    

    Nop = UBound(dblArray) - LBound(dblArray) + 1

    paramsArray(0) = VarPtr(Nop)

    paramsArray(1) = VarPtr(dblArray(0))

    err = viVScanf(vi, "%#Zb%1t", paramsArray(0))

    If err <> 0 Then MsgBox "Binary Error"

    

    ReDim data(Nop - 1)

    For i = 0 To Nop - 1

        data(i) = dblArray(i)

    Next

End Sub

Sample Program in Excel VBA using VISA-COM

Private Sub Read_BINARY_Click()

  Dim ReadData() As Double

  Dim Poin As Integer

  Dim FreqData() As Double

  '*** The variables of the resource manager and the instrument I/O are declared.

  Dim ioMgr As VisaComLib.ResourceManager

  Dim Age506x As VisaComLib.FormattedIO488

  '*** The memory area of the resource manager and the instrument I/O are acquired.

  Set ioMgr = New VisaComLib.ResourceManager

  Set Age506x = New VisaComLib.FormattedIO488

 

  '*** Open the instrument.

  Set Age506x.IO = ioMgr.Open("GPIB0::17::INSTR")

  Age506x.IO.Timeout = 10000

 

  '*** Abort sweeping of channel1/trace1.

  Age506x.WriteString ":CALC1:PAR1:SEL", True

  Age506x.WriteString ":INIT1:CONT OFF", True

  Age506x.WriteString ":ABOR", True

  '*** Set the binary format.

  Age506x.WriteString ":FORM:DATA REAL", True

 

  '*** Get num of point and the stimulus data.

  Age506x.WriteString ":SENS1:SWE:POIN?", True

  Poin = Age506x.ReadNumber

  Age506x.WriteString ":SENS1:FREQ:DATA?", True

  FreqData = Age506x.ReadIEEEBlock(BinaryType_R8, False, True)

  '*** Get the measurement data.

  Age506x.WriteString ":CALC1:DATA:FDAT?", True

  ReadData = Age506x.ReadIEEEBlock(BinaryType_R8, False, True)

 

  '*** set data for new sheet

  Sheets.Add

  ActiveSheet.Name = Format(Year(Date), "0000") & Format(Month(Date), "00") & Format(Day(Date), "00") _

                     & Format(Hour(Time), "00") & Format(Minute(Time), "00") & Format(Second(Time), "00")

  ActiveSheet.Cells(5, 3) = "Frequency"

  ActiveSheet.Cells(5, 4) = "Data 1"

  ActiveSheet.Cells(5, 5) = "Data 2"

  For i = 1 To Poin

      ActiveSheet.Cells(i + 5, 3) = FreqData(i - 1)

    

      ActiveSheet.Cells(i + 5, 4).Value = ReadData(i * 2 - 2)

      ActiveSheet.Cells(i + 5, 5).Value = ReadData(i * 2 - 1)

  Next i

 

  '*** end procedure

  Age506x.WriteString ":FORM:DATA ASC", True

  Age506x.IO.Close

End Sub

 

Sample Program in HT Basic (read_bin.htb)

10 REAL Fdata(1:1601,1:2),Freq(1:1601)

20 DIM Buff$[9],Img$[30]

30 INTEGER Nop,I

40 !

50 ASSIGN @Agte506x TO 717

60 ASSIGN @Binary TO 717;FORMAT OFF

70 !

80 OUTPUT @Agte506x;":CALC1:PAR1:SEL"

90 OUTPUT @Agte506x;":INIT1:CONT OFF"

100 OUTPUT @Agte506x;":ABOR"

110 OUTPUT @Agte506x;":SENS1:SWE:POIN?"

120 ENTER @Agte506x;Nop

130 REDIM Fdata(1:Nop,1:2),Freq(1:Nop)

140 !

150 ! Reading out in binary transfer format

160 !

170 OUTPUT @Agte506x;":FORM:DATA REAL"

180 !

190 OUTPUT @Agte506x;":CALC1:DATA:FDAT?"

200 ENTER @Agte506x USING "#,8A";Buff$

210 ENTER @Binary;Fdata(*)

220 ENTER @Agte506x USING "#,1A";Buff$

230 OUTPUT @Agte506x;":SENS1:FREQ:DATA?"

240 ENTER @Agte506x USING "#,8A";Buff$

250 ENTER @Binary;Freq(*)

260 ENTER @Agte506x USING "#,1A";Buff$

270 !

280 ! Displaying

290 !

300 OUTPUT @Agte506x;":CALC1:FORM?"

310 ENTER @Agte506x;Fmt$

320 SELECT Fmt$

330 CASE "MLOG","PHAS","GDEL","MLIN","SWR","REAL","IMAG","UPH"

340 Img$="MD.4DE,2X,MD.6DE"

350 PRINT " Frequency Data"

360 FOR I=1 TO Nop

370 PRINT USING Img$;Freq(I),Fdata(I,1)

380 NEXT I

390 CASE ELSE

400 Img$="MD.4DE,2X,MD.6DE,2X,MD.6DE"

410 PRINT " Frequency Data1 Data2"

420 FOR I=1 TO Nop

430 PRINT USING Img$;Freq(I),Fdata(I,1),Fdata(I,2)

440 NEXT I

450 END SELECT

460 !

470 END