﻿Imports System.Runtime.InteropServices
Public Class Form1
    '******************************************************* [Constant] ***
    Const GP_GTL As Short = &H1S           'GTL / Go To Local
    Const GP_SDC As Short = &H4S           'SDC / Selected Device Clear
    Const GP_PPC As Short = &H5S           'PPC / Parallel Poll Configure
    Const GP_GET As Short = &H8S           'GET / Group Excute Trigger
    Const GP_TCT As Short = &H9S           'TCT / Take ConTrol
    Const GP_LLO As Short = &H11S          'LLO / Local Lock Out
    Const GP_DCL As Short = &H14S          'DCL / Device CLear
    Const GP_PPU As Short = &H15S          'PPU / Parallel Poll Unconfigure
    Const GP_SPE As Short = &H18S          'SPE / Serial Poll Enable
    Const GP_SPD As Short = &H19S          'SPD / Serial Poll Disable
    Const GP_MLA As Short = &H20S          'MLA / My Listener Address
    Const GP_UNL As Short = &H3FS          'UNL / UnListen
    Const GP_MTA As Short = &H40S          'MTA / My Talker Address
    Const GP_UNT As Short = &H5FS          'UNT / UnTalker

    Dim bRet As Short                       'It is used AddComand function
    Dim CmdNumArray(50) As Integer          'Array of command
    Dim CmdNumPos As Integer                'A place of a command array
    Dim Adrs As Integer                     'It is used at the time of address making
    Dim ErrText As String                   'For a text
    Dim gChCmd As GCHandle                  'GCHandle for cmd 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 address of device
            YrAdrBox.Items.Add(CStr(Adrs))
        Next Adrs

        YrAdrBox.Text = CStr(1)             'Initial setting(First of all in address of device)
        CmdNum = 1                          'Initial setting(First of all in number of command)

        'Specify Pinned handle to Cmd
        If gChCmd.IsAllocated = False Then
            gChCmd = GCHandle.Alloc(Cmd, GCHandleType.Pinned)
        End If
        'Get address object by Pinned handle
        pCmd = gChCmd.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

        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

        Send.Enabled = True

        Ret = GpBoardsts(&H8S, MyAddr)
        TextRet.Text = ErrText

    End Sub

    Private Sub Send_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Send.Click '[ Sending ]

        Cmd(0) = CmdNum - 1                     'Number of command

        If (CmdNum <= 1) Then                   'If there was not a command
            ErrText = "Please input a command."
        Else
            Ret = GpComand(pCmd)                'Command (character string) sending
            CheckRet("GpComand", Ret, ErrText)
        End If

        TextRet.Text = ErrText

    End Sub

    Private Sub DCL_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles DCL.Click '[ DCL ]
        bRet = AddCommand(GP_DCL, 1)            'AddComand(A constant, Number)
        If bRet = True Then
            CmdNumArray(CmdNumPos) = 1
            CmdNumPos = CmdNumPos + 1
            TextRet.Text = ""
        End If
        CmdList.Text = DispCommandList()        'SubClass.bas

    End Sub

    Private Sub GET_Renamed_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles GET_Renamed.Click '[ GET ]

        bRet = AddCommand(GP_GET, 2)
        If bRet = True Then
            CmdNumArray(CmdNumPos) = 2
            CmdNumPos = CmdNumPos + 1
            TextRet.Text = ""
        End If
        CmdList.Text = DispCommandList()

    End Sub

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

        bRet = AddCommand(GP_GTL, 2)
        If bRet = True Then
            CmdNumArray(CmdNumPos) = 2
            CmdNumPos = CmdNumPos + 1
            TextRet.Text = ""
        End If
        CmdList.Text = DispCommandList()

    End Sub

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

        bRet = AddCommand(GP_LLO, 1)
        If bRet = True Then
            CmdNumArray(CmdNumPos) = 1
            CmdNumPos = CmdNumPos + 1
            TextRet.Text = ""
        End If
        CmdList.Text = DispCommandList()

    End Sub

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

        bRet = AddCommand(GP_SDC, 2)
        If bRet = True Then
            CmdNumArray(CmdNumPos) = 2
            CmdNumPos = CmdNumPos + 1
            TextRet.Text = ""
        End If
        CmdList.Text = DispCommandList()

    End Sub

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

        bRet = AddCommand(GP_UNL, 1)
        If bRet = True Then
            CmdNumArray(CmdNumPos) = 1
            CmdNumPos = CmdNumPos + 1
            TextRet.Text = ""
        End If
        CmdList.Text = DispCommandList()

    End Sub

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

        bRet = AddCommand(GP_UNT, 1)
        If bRet = True Then
            CmdNumArray(CmdNumPos) = 1
            CmdNumPos = CmdNumPos + 1
            TextRet.Text = ""
        End If
        CmdList.Text = DispCommandList()

    End Sub

    Private Sub YrAdrBox_SelectedIndexChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles YrAdrBox.SelectedIndexChanged '[ When remote machine vessel address was modified ]

        YrAddr = CInt(YrAdrBox.Text)

    End Sub

    Private Sub DelAll_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles DelAll.Click '[ At a command all an erase ]

        CmdNum = 1
        CmdNumPos = 0

        CmdList.Text = ""

    End Sub

    Private Sub DelOne_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles DelOne.Click '[ 1 command erase ]

        If CmdNumPos = 0 Then                   'When there is not message
            TextRet.Text = "There is not multi line message."
        Else
            If CmdNumArray(CmdNumPos - 1) = 1 Then
                Cmd(CmdNum) = 0
                CmdNum = CmdNum - 1
            Else
                Cmd(CmdNum) = 0
                Cmd(CmdNum - 1) = 0
                CmdNum = CmdNum - 2
            End If
            CmdNumArray(CmdNumPos - 1) = 0
            CmdNumPos = CmdNumPos - 1
            TextRet.Text = ""
        End If
        CmdList.Text = DispCommandList()

    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
