﻿Imports System.Runtime.InteropServices
Public Class Form1
    Dim Pstb(15) As Integer             'An array of status byte
    Dim Adrs As Integer                 'It is used at the time of address making
    Dim ErrText As String               'Text for display
    Dim gChCmd As GCHandle              'GCHandle for cmd array
    Dim gChPstb As GCHandle             'GCHandle for Pstb array
    Dim pPstb As IntPtr                 'IntPtr for Pstb array

    Private Sub Form1_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load '[ Form load ]

        For Adrs = 0 To 30              'Making of Address of device
            YrAdrBox.Items.Add(CStr(Adrs))
        Next Adrs

        YrAdrBox.Text = CStr(1)         'Initial setting

        'Specify Pinned handle to Cmd
        If gChCmd.IsAllocated = False Then
            gChCmd = GCHandle.Alloc(Cmd, GCHandleType.Pinned)
        End If
        'Specify Pinned handle to Pstb
        If gChPstb.IsAllocated = False Then
            gChPstb = GCHandle.Alloc(Pstb, GCHandleType.Pinned)
        End If
        'Get address object by Pinned handle
        pCmd = gChCmd.AddrOfPinnedObject()
        pPstb = gChPstb.AddrOfPinnedObject()

    End Sub

    Private Sub Initialize_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Initialize.Click '[ Initialize ]

        Ret = GpibInit(ErrText)         'SubClass.bas
        If Ret = 1 Then
            TextRet.Text = ErrText
            Exit Sub
        End If

        MyAddress.Text = CStr(MyAddr)
        Polling.Enabled = True

        If YrAdrBox.Items.Count = 31 Then 'MyAddress is deleted from a list.
            YrAdrBox.Items.RemoveAt((MyAddr))
            If MyAddr = YrAddr Then
                YrAdrBox.SelectedIndex = 0
            End If
        End If

        If Mode = 1 Then ErrText = "This device is not a master."

        'If an error was detected, it is displayed.
        TextRet.Text = ErrText

    End Sub

    Private Sub Polling_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Polling.Click '[ Polling ]

        For Cnt = 0 To 15               'Initialization of parameter
            Pstb(Cnt) = 0
        Next

        PollSts.Text = ""
        TextRet.Text = "Polling..."
        TextRet.Refresh()

        Cmd(0) = 1
        Cmd(1) = YrAddr
        Ret = GpPoll(pCmd, pPstb)   'Serial-poll performance
        If CheckRet("GpPoll", Ret, ErrText) = 1 Then
            TextRet.Text = ErrText
            Exit Sub
        ElseIf CheckRet("GpPoll", Ret, ErrText) = 2 Then
            ErrText = "There was not [SRQ]."
            TextRet.Text = ErrText
            Exit Sub
        End If
        PollSts.Text = "Talker address = [ " & Cmd(Pstb(0)) & " ] StatusByte = [" & Hex(Pstb(Pstb(0))) & " ] H"
        TextRet.Text = "[SRQ] was confirmation."
        TextRet.Text = ErrText

    End Sub

    Private Sub yradrbox_SelectedIndexChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles YrAdrBox.SelectedIndexChanged '[ Change of Address of device address ]

        YrAddr = CInt(YrAdrBox.Text)

    End Sub

    Private Sub DelRet_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles DelRet.Click '[ An erase of a returning value ]

        TextRet.Text = ""

    End Sub

    Private Sub DelSts_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles DelSts.Click '[ An erase of polling status ]

        PollSts.Text = ""

    End Sub

    Private Sub Exit_Renamed_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Exit_Renamed.Click '[ Exit ]

        Ret = GpibExit()                'SubClass.bas
        Me.Close()

    End Sub

    Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed

        'GCHandle free
        If gChCmd.IsAllocated = True Then
            gChCmd.Free()
        End If

    End Sub
End Class
