3.2 GP-IB Example program

The following example programs are for the National Instruments GP-IB board (including the PC card).


Example 1
Using a setting command


► Send the command in the format specified, when the conditions for the command to be acceptable are met.


Private Sub Sample1_Click()
'*******************************************************************************
' GPIB SAMPLE PROGRAM NO.1
'*******************************************************************************
    Adr = 5 'GP-IB Address = 5
    Call ibdev(0, Adr, 0, T1s, 1, 0, rec%)
    If (rec% < 0) Then
        MsgBox "Could not open device", 64, "ERROR"
        Call ibonl(rec%, 0)
        Exit Sub
    End If
    Call ibwrt(rec%, ":FUNCTION MEM")
    Call ibwrt(rec%, ":CONFIGURE:TDIV +1.E-3")
    Call ibwrt(rec%, ":CONFIGURE:SHOT 20")
    Call ibwrt(rec%, ":TRIGGER:SOURCE OR")
    Call ibwrt(rec%, ":TRIGGER:KIND CH1,LEVEL")
    Call ibwrt(rec%, ":TRIGGER:PRETRIG 5")
    Call ibwrt(rec%, ":TRIGGER:LEVEL CH1,2")
    Call ibwrt(rec%, ":TRIGGER:SLOPE CH1,UP")
    Call ibwrt(rec%, ":TRIGGER:KIND CH2,OFF")
    Call ibwrt(rec%, ":TRIGGER:KIND CH3,OFF")
    Call ibwrt(rec%, ":TRIGGER:KIND CH4,OFF")
    Call ibwrt(rec%, ":START")
    Call ibonl(rec%, 0)
End Sub



Example 2
Using a query


► Send the query int the format specified, when the conditions for the query to be acceptable are met.
► Next switch the unit to be the talker, and receive the output data.
► The response data from the query is returned in the format specified for the corresponding command.


Private Sub Sample2_Click()
'***************************************************************************
'  GPIB SAMPLE PROGRAM NO.2
'***************************************************************************
    Adr = 5 'GP-IB Address = 5
    Call ibdev(0, Adr, 0, T1s, 1, 0, rec%)
    If (rec% < 0) Then
        MsgBox "Could not open device", 64, "ERROR"
        Call ibonl(rec%, 0)
        Exit Sub
    End If
    
    Call ibwrt(rec%, ":HEADER OFF")
    Call ibwrt(rec%, ":FUNCTION?")
    rd$ = Space$(30)
    Call ibrd(rec%, rd$)
    Text1.Text = Left$(rd$, ibcnt% - 1)
    Call ibwrt(rec%, ":SYSTEM:TIME?")
    rd$ = Space$(30)
    Call ibrd(rec%, rd$)
    Text1.Text = Text1.Text & Chr$(13) & Chr$(10) & Left$(rd$, ibcnt% - 1)
    Call ibonl(rec%, 0)
End Sub



Example 3
Using service requests


► Using the *SRE and *ESE commands.
► This program sets the service request response enable, and sets the jump address in the controller for a service request interrupt.
► It then enables the service request inerrupt.
► The response data from the query is returned in the format specified for the corresponding command.


Private Sub Sample3_Click()
'***************************************************************************
' GPIB SAMPLE PROGRAM NO.3
'***************************************************************************
    Adr = 5
    Call ibdev(0, Adr, 0, T10s, 1, 0, rec%)
    If (rec% < 0) Then
    MsgBox "Could not open device", 64, "ERROR"
    Call ibonl(rec%, 0)
    Exit Sub
    End If
    Call ibwrt(rec%, "HEADER OFF")
    Call ibwrt(rec%, "*SRE 32")
    Call ibwrt(rec%, "*ESE 16")
    Call ibwrt(rec%, "*CLS")
    Call ibwrt(rec%, ":FUNCTION MEM")
    For i = 1 To 16
    gra$ = ":DISPLAY:GRAPH CH1," + LTrim(Str$(i))
    Call ibwrt(rec%, gra$)
    Call ibwait(rec%, RQS Or TIMO)
    If (ibsta% And RQS) Then
    Call err_check
    Exit Sub
    End If
    Next
    Call ibonl(rec%, 0)
End Sub

Sub err_check(rec As Integer)
    Call ibrsp(rec%, spr%)
    Call ibwrt(rec%, "*ESR?")
    rd$ = Space$(30)
    Call ibrd(rec%, rd$)
    b = Val(rd$)
    If (b And &H4) <> 0 Then Text1.Text = Text1.Text & Chr$(13) & Chr$(10) _
    & "Query Error!"
    If (b And &H8) <> 0 Then Text1.Text = Text1.Text & Chr$(13) & Chr$(10) _
    & "Machine Error!"
    If (b And &H10) <> 0 Then Text1.Text = Text1.Text & Chr$(13) & Chr$(10) _
    & "Execute Error!"
    If (b And &H20) <> 0 Then Text1.Text = Text1.Text & Chr$(13) & Chr$(10) _
    & "Command Error!"
    Call ibonl(rec%, 0)
End Sub



Example 4
Outputting stored data


► Using the :MEMORY:MAXPOINT? query, this program checks whether data can be output from the memory. If this query returns zero, no data is stored, and it cannot therefore be output.
► Next, the program specifies the channel and point for output, using the :MEMORY:POINT command. As data is input or output, the point is incremented automatically. If capturing data consecutively, it is sufficient to specify the point once only.
► To capture data in ASCII format use the :MEMORY:ADATA? query, and to capture data as voltage values use the :MEMORY:VDATA? query. The number of data samples which may be output in one set is 1 to 80 using :ADATA? and 1 to 40 using the :VDATA? query.
► Outputting data in bigger sets reduces the overall processing time.
► Read data (2000 samples) for channel 1 when stored with a 20-division recording length.


Private Sub Sample4_Click()
'***************************************************************************
' GPIB SAMPLE PROGRAM NO.4
'***************************************************************************
    Adr = 5
    Dim d(2000)
    Call ibdev(0, Adr, 0, T1s, 1, 0, rec%)
    If (rec% < 0) Then
    MsgBox "Could not open device", 64, "ERROR"
    Call ibonl(rec%, 0)
    Exit Sub
    End If
    Call ibwrt(rec%, ":FUNCTION MEM")
    Call ibwrt(rec%, ":CONFIGURE:SHOT 20")
    Call ibwrt(rec%, ":TRIGGER:MODE SINGLE")
    Call ibwrt(rec%, ":START;:STOP;*OPC?")
    rd$ = Space$(30)
    Call ibrd(rec%, rd$)
    Call ibwrt(rec%, ":HEADER OFF")
    Call ibwrt(rec%, ":MEMORY:MAXPOINT?")
    rd$ = Space$(30)
    Call ibrd(rec%, rd$)
    mx% = Val(rd$)
    If (mx% <> 2000) Then
        Call ibonl(rec%, 0)
        Exit Sub
    End If
    Call ibwrt(rec%, ":MEMORY:POINT CH1,0")
    For i = 0 To 2000
    Call ibwrt(rec%, ":MEMORY:VDATA? 1")
    rd$ = Space$(30)
    Call ibrd(rec%, rd$)
    d(i) = Val(rd$)
    Next
    For i = 0 To 2000
        Text1.Text = d(i)
    Next
    Call ibonl(rec%, 0)
End Sub



Example 5
Inputting storage data


► This program prepares storage memory, using the :MEMORY:PREPARE command.
► Next, the program specifies the channel and point for input, using the :MEMORY:POINT command, and then used the :MEMORY:ADATA command to input data.


Private Sub Sample5_Click()
'***************************************************************************
' GPIB SAMPLE PROGRAM NO.5
'***************************************************************************
    Adr = 5
    Call ibdev(0, Adr, 0, T10s, 1, 0, rec%)
    If (rec% < 0) Then
    MsgBox "Could not open device", 64, "ERROR"
    Call ibonl(rec%, 0)
    Exit Sub
    End If
    Call ibwrt(rec%, ":FUNCTION MEM")
    Call ibwrt(rec%, ":CONFIGURE:SHOT 20")
    Call ibwrt(rec%, ":MEMORY:PREPARE")
    Call ibwait(rec%, TIMO) '待機
    Call ibwrt(rec%, ":MEMORY:POINT CH1,0")
    For i = 0 To 2000
        SND$ = ":MEMORY:ADATA " + LTrim(Str$(Int(500 * Sin(3.14 * i / 500))))
        Call ibwrt(rec%, SND$)
    Next
    Call ibonl(rec%, 0)
End Sub



Example 6
Start measurement operation mode, and if no trigger is detected execute a STOP.


► Bit 5 (ESB) of the status register is obtained by serial polling.
► Forcible termination by :ABORT results if no trigger is detected when bit 2 (trigger wait completed) is checked by :ESR0?.
► Termination occurs upon verification of bit 1 (start processing completed) by :ESR0?.


Private Sub Sample6_Click()
'***************************************************************************
' GPIB SAMPLE PROGRAM NO.6
'***************************************************************************
    Adr = 5
    Call ibdev(0, Adr, 0, T1s, 1, 0, rec%)
    
    If (rec% < 0) Then
        MsgBox "Could not open device", 64, "ERROR"
        Call ibonl(rec%, 0)
        Exit Sub
    End If
    
    Call ibwrt(rec%, ":HEADER OFF")
    Call ibwrt(rec%, "*CLS;*ESE 1")
    Call ibwrt(rec%, ":FUNCTION MEM")
    Call ibwrt(rec%, ":CONFIGURE:TDIV 1.E-3")
    Call ibwrt(rec%, ":CONFIGURE:SHOT 20")
    Call ibwrt(rec%, ":TRIGGER:SOURCE OR")
    Call ibwrt(rec%, ":TRIGGER:KIND CH1,LEVEL;KIND CH2,LEVEL")
    Call ibwrt(rec%, ":TRIGGER:KIND CH3,OFF;KIND CH4,OFF")
    Call ibwrt(rec%, ":TRIGGER:LEVEL CH1,0;SLOPE CH1,UP")
    Call ibwrt(rec%, ":TRIGGER:LEVEL CH2,0;SLOPE CH2,UP")
    Call ibwrt(rec%, ":TRIGGER:MODE SINGLE")
    Call ibwrt(rec%, ":START;*OPC")
    
    Do
        Call ibrsp(rec%, spr%)
    Loop While ((spr% And &H20) = 0)
    
    Call ibwait(rec%, TIMO)
    Call ibwrt(rec%, ":ESR0?")
    rd$ = Space$(30)
    Call ibrd(rec%, rd$)
    esr0% = Val(rd$)
    
    If (esr0% And &H4) = 0 Then
        Call ibwrt(rec%, ":ABORT")
        Call ibonl(rec%, 0)
        MsgBox "NOT TRIGGER"
        Exit Sub
    End If
    
    Do While ((esr0% And &H2) = 0)
        Call ibwrt(rec%, ":ESR0?")
        rd$ = Space$(30)
        Call ibrd(rec%, rd$)
        esr0% = Val(rd$)
    Loop
    
    MsgBox "STORAGE END"
    Call ibonl(rec%, 0)
End Sub



Example 7
Checking the presence of input unit, and displaying the input ranges on the screen.



Private Sub Sample7_Click()
'***************************************************************************
' GPIB SAMPLE PROGRAM NO.7
'***************************************************************************
    Adr = 5
    Call ibdev(0, Adr, 0, T1s, 1, 0, rec%)
    
    If (rec% < 0) Then
        MsgBox "Could not open device", 64, "ERROR"
        Call ibonl(rec%, 0)
        Exit Sub
    End If
    
    Call ibwrt(rec%, ":HEADER OFF")
    Call ibwrt(rec%, "*OPT?")
    rd$ = Space$(30)
    Call ibrd(rec%, rd$)
    ch1% = Val(Mid$(rd$, 1, 1))
    ch2% = Val(Mid$(rd$, 3, 1))
    ch3% = Val(Mid$(rd$, 5, 1))
    ch4% = Val(Mid$(rd$, 7, 1))
    Call ibwrt(rec%, ":MEMORY:GETREAL")
    
    If (ch1% <> 0) Then
        Call ibwrt(rec%, ":MEMORY:AREAL? CH1")
        rd$ = Space$(30)
        Call ibrd(rec%, rd$)
        ch1_data$ = "CH1 = " & rd$
    Else
        ch1_data$ = "CH1 = NON"
    End If
    
    If (ch2% <> 0) Then
        Call ibwrt(rec%, ":MEMORY:AREAL? CH2")
        rd$ = Space$(30)
        Call ibrd(rec%, rd$)
        ch2_data$ = "CH2 = " & rd$
    Else
        ch2_data$ = "CH2 = NON"
    End If
    
    If (ch3% <> 0) Then
        Call ibwrt(rec%, ":MEMORY:AREAL? CH3")
        rd$ = Space$(30)
        Call ibrd(rec%, rd$)
        ch3_data$ = "CH3 = " & rd$
    Else
        ch3_data$ = "CH3 = NON"
    End If
    
    If (ch4% <> 0) Then
        Call ibwrt(rec%, ":MEMORY:AREAL? CH4")
        rd$ = Space$(30)
        Call ibrd(rec%, rd$)
        ch4_data$ = "CH4 = " & rd$
    Else
        ch4_data$ = "CH4 = NON"
    End If
    
    ret$ = Chr$(13) & Chr$(10)
    Text1.Text = ch1_data$ & ret$ & ch2_data$ & ret$ & ch3_data$ & ret$ & ch4_data$
    Call ibonl(rec%, 0)
End Sub



Example 8
Outputting stored data (binary data)


► Using the :MEMORY:MAXPOINT? query, this program checks whether data can be output form memory. If this query returns zero, no data is stored, and it cannot therefore be output.
► Next, the program specifies the channel and point for output, using the :MEMORY:POINT command. As data is input or output, the point is incremented qutomatically. If capturing data consecutively, it is sufficient to specify the point once only.
► After converting the binary data to voltage value obtained by :MEMORY:BDATA?, it is output on the screen. The number of data samples which may be output in one set is 1 to 200 using :BDATA?.


Private Sub Sample8_Click()
'***************************************************************************
' GPIB SAMPLE PROGRAM NO.8
'***************************************************************************
    Adr = 5
    Dim dat(512) As Byte
    Call ibdev(0, Adr, 0, T1s, 1, 0, rec%)
    
    If (rec% < 0) Then
        MsgBox "Could not open device", 64, "ERROR"
        Call ibonl(rec%, 0)
        Exit Sub
    End If
    
    Call ibwrt(rec%, ":HEADER OFF")
    Call ibwrt(rec%, ":MEMORY:MAXPOINT?")
    rd$ = Space$(30)
    Call ibrd(rec%, rd$)
    sp% = Val(rd$)
    
    If (sp% = 0) Then
        Call ibonl(rec%, 0)
        Exit Sub
    End If
    
    Call ibwrt(rec%, ":UNIT:RANGE? CH1")
    rd$ = Space$(30)
    Call ibrd(rec%, rd$)
    rd$ = Left(rd$, ibcnt - 1)
    range# = Val(Mid(rd$, 5))
    Call ibwrt(rec%, ":MEMORY:POINT CH1,0")
    Call ibwrt(rec%, ":MEMORY:BDATA? 200")
    Call ibrd32(rec%, dat(0), 403)
    
    For i = 1 To 200
        a% = (dat(2 * i) And &H7) - (dat(2 * i) And &H8)
        b% = dat(2 * i + 1)
        c% = a% * 256 + b%
        d% = c% * range# / 160
        Text1.Text = Str$(d#)
    Next
    
    Call ibonl(rec%, 0)
End Sub



Example 9
Saving stored data onto drive A (sequential file)



Private Sub Sample9_Click()
'***************************************************************************
' GPIB SAMPLE PROGRAM NO.9
'***************************************************************************
    Adr = 5
    Call ibdev(0, Adr, 0, T1s, 1, 0, rec%)
    
    If (rec% < 0) Then
        MsgBox "Could not open device", 64, "ERROR"
        Call ibonl(rec%, 0)
        Exit Sub
    End If
    
    Call ibwrt(rec%, ":HEADER OFF")
    Call ibwrt(rec%, ":MEMORY:MAXPOINT?")
    rd$ = Space$(30)
    Call ibrd(rec%, rd$)
    sp% = Val(rd$)
    
    If (sp% = 0) Then
        Call ibonl(rec%, 0)
        Exit Sub
    End If
    
    na$ = "a:\sample.dat"
    Open na$ For Output As #1
    Call ibwrt(rec%, ":MEMORY:POINT CH1,0")
    Print #1, 10
    
    For i = 0 To 10
        Call ibwrt(rec%, ":MEMORY:ADATA? 1")
        rd$ = Space$(30)
        Call ibrd(rec%, rd$)
        Print #1, Val(rd$)
    Next
    Close #1
    Call ibonl(rec%, 0)
End Sub



Example 10
Reading the data saved in Example 9, and loading it into the unit.



Private Sub Sample10_Click()
'***************************************************************************
' GPIB SAMPLE PROGRAM NO.10
'***************************************************************************
    Adr = 5
    Call ibdev(0, Adr, 0, T1s, 1, 0, rec%)
    
    If (rec% < 0) Then
        MsgBox "Could not open device", 64, "ERROR"
        Call ibonl(rec%, 0)
        Exit Sub
    End If
    
    Call ibwrt(rec%, ":HEADER OFF")
    Call ibwrt(rec%, ":MEMORY:PREPARE")
    Call ibwait(rec%, TIMO)
    na$ = "a:\sample.dat"
    Open na$ For Input As #1
    Call ibwrt(rec%, ":MEMORY:POINT CH1,0")
    Line Input #1, mx
    
    For i = 0 To mx
        Line Input #1, dt
        Call ibwrt(rec%, ":MEMORY:ADATA " + LTrim(Str$(dt%)))
    Next
    
    Close #1
    Call ibonl(rec%, 0)
End Sub