﻿Imports System.Runtime.InteropServices
Public Class Form1
    '******************************************************* [定数] ***
    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                       'AddComand関数で使用
    Dim CmdNumArray(50) As Integer          'コマンドの配列
    Dim CmdNumPos As Integer                'コマンド中の位置
    Dim Adrs As Integer                     'アドレス作成時に使用
    Dim ErrText As String                   'テキスト表示用
    Dim gChCmd As GCHandle                  'Cmd配列用GCHandle

    '///// [ フォームのロード ] //////////////////////////////////////////////////////////////
    Private Sub Form1_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load

        For Adrs = 0 To 30                  '相手機器アドレス作成
            YrAdrBox.Items.Add(CStr(Adrs))
        Next Adrs

        YrAdrBox.Text = CStr(0)             '初期設定(相手機器アドレスの初期アドレス)
        CmdNum = 1                          '初期設定(コマンドの数)

        'PinnedハンドルをCmdに分配する
        If gChCmd.IsAllocated = False Then
            gChCmd = GCHandle.Alloc(Cmd, GCHandleType.Pinned)
        End If
        'Pinnedハンドルでオブジェクトのアドレスを取得
        pCmd = gChCmd.AddrOfPinnedObject()

    End Sub


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

        Ret = GpibInit(ErrText)             'SubFunc.basにあります。
        TextRet.Text = ErrText

        If YrAdrBox.Items.Count = 31 Then   'マイアドレスをリストから削除します。
            YrAdrBox.Items.RemoveAt((MyAddr))
            If MyAddr = YrAddr Then
                YrAdrBox.SelectedIndex = 0
            End If
        End If

    End Sub

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

        Cmd(0) = CmdNum - 1                 'コマンドの総数です。

        If (CmdNum <= 1) Then               'コマンドがなかった場合
            ErrText = "コマンドを入力してください。"
        Else
            Ret = GpComand(pCmd)            'コマンド(文字列)の送信
            CheckRet("GpComand", Ret, ErrText)
        End If

        TextRet.Text = ErrText

    End Sub

    '///// [ DCL ] ///////////////////////////////////////////////////////////////////////////
    Private Sub DCL_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles DCL.Click
        bRet = AddCommand(GP_DCL, 1)        'AddComand(定数､ 個数)
        If bRet = True Then
            CmdNumArray(CmdNumPos) = 1
            CmdNumPos = CmdNumPos + 1
            TextRet.Text = ""
        End If
        CmdList.Text = DispCommandList()    'SubClassにあります。

    End Sub

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

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

    End Sub

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

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

    End Sub

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

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

    End Sub

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

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

    End Sub

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

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

    End Sub

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

        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

        YrAddr = CInt(YrAdrBox.Text)

    End Sub

    '///// [ コマンドを全て消去 ] ////////////////////////////////////////////////////////////
    Private Sub DelAll_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles DelAll.Click

        CmdNum = 1
        CmdNumPos = 0

        CmdList.Text = ""

    End Sub

    '///// [ コマンド1個消去 ] ///////////////////////////////////////////////////////////////
    Private Sub DelOne_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles DelOne.Click

        If CmdNumPos = 0 Then               'メッセージがない場合
            TextRet.Text = "マルチラインメッセージがありません。"
        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

        Ret = GpibExit()                    'SubClassにあります。
        Me.Close()                          'ダイアログの終了

    End Sub

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

        'GCHandleの解放
        If gChCmd.IsAllocated = True Then
            gChCmd.Free()
        End If

    End Sub
End Class
