﻿Imports System.Runtime.InteropServices
Public Class Form1
    Dim Pstb(15) As Integer                 'An array of status byte
    Dim Pstb2(15) As Integer                'For the 2nd frustum
    Dim PpollData As Integer                'As the result that it was poll parallel
    Dim Yradr2 As Integer                   'The Address of device address 2nd frustum
    Dim Cmd2(50) As Integer                 'It is parallel message (command)/, and use
    Dim Adrs As Integer                     'It is used at the time of device making
    Dim ErrText As String                   'Text for display
    Dim Srlen As Integer                    'A length of a character string
    Dim gChCmd As GCHandle                  'GCHandle for cmd array
    Dim gChPstb As GCHandle                 'GCHandle for Pstb array
    Dim pPstb As IntPtr                     'IntPtr for Pstb array
    Dim gChCmd2 As GCHandle                 'GCHandle for cmd2 array
    Dim gChPstb2 As GCHandle                'GCHandle for Pstb2 array
    Dim pPstb2 As IntPtr                    'IntPtr for Pstb2 array
    Dim pCmd2 As IntPtr                     'IntPtr for cmd2 array

    Private Sub Parallel_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
            YrAdrBox1.Items.Add(CStr(Adrs))
            YrAdrBox2.Items.Add(CStr(Adrs))
        Next Adrs

        YrAdrBox1.Text = CStr(1)            'Initial setting
        YrAdrBox2.Text = CStr(2)            'Initial setting

        'Specify Pinned handle to Cmd
        If gChCmd.IsAllocated = False Then
            gChCmd = GCHandle.Alloc(Cmd, GCHandleType.Pinned)
        End If
        If gChCmd2.IsAllocated = False Then
            gChCmd2 = GCHandle.Alloc(Cmd2, GCHandleType.Pinned)
        End If
        'Specify Pinned handle to Pstb
        If gChPstb.IsAllocated = False Then
            gChPstb = GCHandle.Alloc(Pstb, GCHandleType.Pinned)
        End If
        If gChPstb2.IsAllocated = False Then
            gChPstb2 = GCHandle.Alloc(Pstb2, GCHandleType.Pinned)
        End If
        'Get address object by Pinned handle
        pCmd = gChCmd.AddrOfPinnedObject()
        pCmd2 = gChCmd2.AddrOfPinnedObject()
        pPstb = gChPstb.AddrOfPinnedObject()
        pPstb2 = gChPstb2.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

        Polling.Enabled = True

        If YrAdrBox1.Items.Count = 31 Then  'MyAddress is deleted from a list.
            YrAdrBox1.Items.RemoveAt((MyAddr))
            YrAdrBox1.SelectedIndex = 0
            YrAdrBox2.Items.RemoveAt((MyAddr))
            YrAdrBox2.SelectedIndex = 1
        End If
        If Mode = 1 Then ErrText = "This equipment 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 ]

        Dim Stppu As Integer                'Setting of PPU

        PollSet.Text = ""                   'Initialization of a text
        PollGo.Text = ""
        PollSts.Text = ""
        TextRet.Text = ""

        For Cnt = 0 To 15
            Pstb(Cnt) = 0                   'Initialization of parameter
            Pstb2(Cnt) = 0                  'Initialization of parameter
        Next
        Cmd(0) = 2                          'The number of a listener
        Cmd(1) = YrAddr                     'The 1st listener
        Cmd(2) = &H69S                      'Distribution of a line(69H = DIO2)
        Cmd(3) = Yradr2                     'The 2nd listener
        Cmd(4) = &H6AS                      'Distribution of a line(6AH = DIO3)
        Stppu = 1                           'Use of PPU

        Ret = GpStppoll(pCmd, Stppu)      'A layout of a line for a responded output of a parallel pole
        If CheckRet("GpStppoll", Ret, ErrText) = 1 Then
            TextRet.Text = ErrText
            Exit Sub
        End If
        PollSet.Text = ErrText

        Ret = GpExppoll(PpollData)          'Performance of parallel-poll
        If CheckRet("GpExppoll", Ret, ErrText) = 1 Then
            TextRet.Text = ErrText
            Exit Sub
        End If
        PollGo.Text = ErrText

        Select Case PpollData               'At a result of parallel-poll an analysis
            Case 2                          'Data Line Bit = 00000010(DIO2)
                Cmd(0) = 1                  'The number of a listener
                Cmd(1) = YrAddr             'The 1st listener
                Ret = GpPoll(pCmd, pPstb) 'Performance of serial-poll
                If CheckRet("GpPoll_1", Ret, ErrText) = 1 Then
                    TextRet.Text = ErrText
                    Exit Sub
                End If
                PollSts.Text = "Talker Address =[ " & Cmd(Pstb(0)) & " ] StatusByte = [ " & Hex(Pstb(Pstb(0))) & " ]H"
            Case 4                          'Data Line Bit = 00000100(DIO3)
                Cmd(0) = 1                  'The number of a listener
                Cmd(1) = Yradr2             'The 2nd listener
                Ret = GpPoll(pCmd, pPstb) 'Performance of serial-poll
                If CheckRet("GpPoll_2", Ret, ErrText) = 1 Then
                    TextRet.Text = ErrText
                    Exit Sub
                End If
                PollSts.Text = "Talker Address =[ " & Cmd(Pstb(0)) & " ] StatusByte = [ " & Hex(Pstb(Pstb(0))) & " ]H"
            Case 6                          'Data Line Bit = 00000110(DIO2 + DIO3)
                Cmd(0) = 1                  'The number of a listener
                Cmd(1) = YrAddr             'The 1st address
                Ret = GpPoll(pCmd, pPstb) 'Performance of serial-poll
                If CheckRet("GpPoll_1", Ret, ErrText) = 1 Then
                    TextRet.Text = ErrText
                    Exit Sub
                End If
                Cmd2(0) = 1                 'The number of a listener
                Cmd2(1) = Yradr2            'The 2nd listener
                Ret = GpPoll(pCmd2, pPstb2) 'Performance of serial-poll
                If CheckRet("GpPoll_2", Ret, ErrText) = 1 Then
                    TextRet.Text = ErrText
                    Exit Sub
                End If
                PollSts.Text = "Talker =[ " & Cmd(Pstb(0)) & " ] StatusByte = [ " & Hex(Pstb(Pstb(0))) & " ] H + Talker =[ " & Cmd2(Pstb2(0)) & " ]  StatusByte =[ " & Hex(Pstb2(Pstb2(0))) & " ] H"
            Case Else
                ErrText = "There was not SRQ."
        End Select

        TextRet.Text = ErrText

    End Sub

    Private Sub YrAdrBox1_SelectedIndexChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles YrAdrBox1.SelectedIndexChanged '[ Change of the 1st Address of device ]

        YrAddr = CInt(YrAdrBox1.Text)

    End Sub

    Private Sub YrAdrBox2_SelectedIndexChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles YrAdrBox2.SelectedIndexChanged '[ Change of the 2nd Address of device ]

        Yradr2 = CInt(YrAdrBox2.Text)

    End Sub

    Private Sub PollDel_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles PollDel.Click '[ An erase of a polling text ]

        PollSet.Text = ""
        PollGo.Text = ""
        PollSts.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 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
        If gChPstb.IsAllocated = True Then
            gChPstb.Free()
        End If
        If gChCmd2.IsAllocated = True Then
            gChCmd2.Free()
        End If
        If gChPstb2.IsAllocated = True Then
            gChPstb2.Free()
        End If
    End Sub
End Class
