﻿Public Class Form1
    Const BDINDEX As Short = 0                              ' デバイス用アクセスボードの指標
    Const NO_SECONDARY_ADDR As Short = 0                    ' デバイスの2次GPIBアドレス
    Const TIMEOUT As Short = T10s                           ' 入出力タイムアウト値
    Const EOTMODE As Short = 1                              ' デバイスのEOIモード
    Const EOSMODE As Short = 0                              ' EOS文字とモード

    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 Command1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command1.Click

        Call ibonl(Dev, 0)
        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 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 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" & vbCrLf & "ibsta = &H" & Hex(ibsta) & vbCrLf & "iberr = " & iberr
            MsgBox(ErrMsg, MsgBoxStyle.Critical, "Error")
            End
        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 & vbCrLf & "ibsta = &H" & Hex(ibsta) & vbCrLf & "iberr = " & iberr & " <" & ErrorMnemonic(iberr) & ">"
        MsgBox(ErrMsg, MsgBoxStyle.Critical, "Error")

    End Sub
End Class
