﻿Public Class Form1
    Const BDINDEX As Short = 0
    Const NO_SECONDARY_ADDR As Short = 0
    Const TIMEOUT As Short = T10s
    Const EOTMODE As Short = 1
    Const EOSMODE As Short = 0

    Const ARRAYSIZE As Short = 100                  ' Size of read buffer

    Dim ResByte As Short
    Dim Dev As Short
    Dim Valuestr As New String("", ARRAYSIZE)

    Dim ErrMsg As New String("", 100)
    Dim ErrorMnemonic As Object

    Private Sub End_Renamed_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles End_Renamed.Click

        End

    End Sub

    Private Sub Form1_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load

        Addr.Text = CStr(1)
        Inp.Text = "*IDN?"
        Outp.Text = ""
        Poll.Text = CStr(0)

    End Sub

    Private Sub Init_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Init.Click

        Dim pad As Short

        pad = CShort(Addr.Text)
        Call ibdev(BDINDEX, pad, NO_SECONDARY_ADDR, TIMEOUT, EOTMODE, EOSMODE, Dev)

        If (ibsta And EERR) Then
            ErrMsg = "Unable to open device" & Chr(13) & "ibsta = &H" & Hex(ibsta) & Chr(13) & "iberr = " & iberr
            MsgBox(ErrMsg, MsgBoxStyle.Critical, "Error")
            End
        End If

    End Sub

    Private Sub Master_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Master.Click

        Call ibwrt(Dev, Inp.Text)

        If (ibsta And EERR) Then
            Call GPIBCleanup("Unable to write to device")
        End If

    End Sub

    Private Sub Slave_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Slave.Click

        Call ibrd(Dev, Valuestr)

        If (ibsta And EERR) Then
            Call GPIBCleanup("Unable to read from device")
        End If

        Outp.Text = Mid(Valuestr, 1, ibcntl(0))

    End Sub

    Private Sub SPoll_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles SPoll.Click

        Call ibrsp(Dev, ResByte)

        If (ibsta And EERR) Then
            Call GPIBCleanup("Unable to serial poll")
        End If

        Poll.Text = ResByte.ToString

    End Sub

    Private Sub GPIBCleanup(ByRef msg As String)
        Dim ErrMsg As String

        ' After each GPIB call, the application checks whether the call
        ' succeeded. If an NI-488.2 call fails, the GPIB driver sets the
        ' corresponding bit in the global status variable. If the call
        ' failed, this procedure prints an error message, takes the device
        ' offline and exits.

        ErrorMnemonic = New Object() {"EDVR", "ECIC", "ENOL", "EADR", "EARG", "ESAC", "EABO", "ENEB", "EDMA", "", "EOIP", "ECAP", "EFSO", "", "EBUS", "ESTB", "ESRQ", "", "", "", "ETAB"}

        ErrMsg = msg & Chr(13) & "ibsta = &H" & Hex(ibsta) & Chr(13) & "iberr = " & iberr & " <" & ErrorMnemonic(iberr) & ">"
        MsgBox(ErrMsg, MsgBoxStyle.Critical, "Error")

    End Sub
End Class
