﻿Imports System.Runtime.InteropServices
Imports VB = Microsoft.VisualBasic
Public Class Form1
    Dim SendBuf As String               'A sending buffer
    Dim DVCNT As Integer                'It is the count which does an equipment remote.
    Dim DVSTOP As Integer               'An equipment is made to hang-up.
    Dim SData As String                 'Transmit data in a constant voltage source
    Dim Adrs As Integer                 'It is used at the time of address making
    Dim ErrText As String               'Text for display
    Dim Srlen As Integer                'A length of a character string
    Dim Delim As Integer                'Delimiter
    Dim gChCmd As GCHandle              'GCHandle for cmd array

    Private Sub DelimBox_SelectedIndexChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles DelimBox.SelectedIndexChanged

        Eoi = 1

        Delim = DelimBox.SelectedIndex
        Ret = GpDelim(Delim, Eoi)

    End Sub

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

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

        DelimBox.Text = "CR+LF"

        ErrText = ""
        TargetName.Items.Add("[ YOKOGAWA Digital Voltage Source TYPE 2553 ]")
        TargetName.Items.Add("[ ADVATEST Voltage Source TR6150 ]")
        TargetName.SelectedIndex = 0

        YrAdrBox.Text = CStr(8)         'Initial setting
        DVCNT = 0

        DVSTOP = 0                      'A constant voltage source operation stopped state
        SendBuf = "V3P0S00000O1"
        Srlen = Len(SendBuf)
        SendData.Text = SendBuf

        '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 Init_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Init.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

        YrAddr = CInt(YrAdrBox.Text)

        Select Case YrAdrBox.Text
            Case CStr(0)
                'It is not done at all.
            Case CStr(1)
                Ret = GpSdc(YrAddr)         ' Selection Address of device is initialized.
                If CheckRet("GpSdc", Ret, ErrText) = 1 Then
                    TextRet.Text = ErrText
                    Exit Sub
                End If
                SendBuf = "V5L3L7"
                Srlen = Len(SendBuf)
                Cmd(0) = 2
                Cmd(1) = MyAddr
                Cmd(2) = YrAddr
                Ret = GpTalk(pCmd, Srlen, SendBuf)
                If CheckRet("GpTalk", Ret, ErrText) = 1 Then
                    TextRet.Text = ErrText
                    Exit Sub
                End If
        End Select

        Cmd(0) = 2                      'Number of command
        Cmd(1) = &H3FS                  'Unlisten/UNL
        Cmd(2) = &H5FS                  'Untalken/UNT
        Ret = GpComand(pCmd) 'command sending
        If CheckRet("GpComand", Ret, ErrText) = 1 Then
            TextRet.Text = ErrText
            Exit Sub
        End If

        If (Ret = 0) Or ((Ret And &HFFS) = 0) Then
            'When Address of device always transmits SRQ,
            'a returning value of PP amounts to 256.
            ErrText = "Initialization was completed.Please set up an Address of device name and address."
            YrInit.Enabled = True
        End If

        Call DelimBox_SelectedIndexChanged(DelimBox, New System.EventArgs())
        TextRet.Text = ErrText

    End Sub

    Private Sub SendRun_Tick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles SendRun.Tick '[ Command sending(Timer) ]

        Do
            Select Case (TargetName.SelectedIndex)      'In a device name change of data sending
                Case 0
                    SendBuf = "V3P0S" & VB.Right("0" & DVCNT.ToString(), 2) & "000O1" : Srlen = Len(SendBuf)
                    If DVCNT = 0 Then SendBuf = "V3P0S00000O0" : Srlen = Len(SendBuf)
                    If DVCNT >= 12 Then DVCNT = 0 Else DVCNT = DVCNT + 1
                    SendData.Text = SendBuf

                    Srlen = Len(SendBuf)
                    Cmd(0) = 2
                    Cmd(1) = MyAddr
                    Cmd(2) = YrAddr
                    Ret = GpTalk(pCmd, Srlen, SendBuf)
                    If CheckRet("GpTalk", Ret, ErrText) = 1 Then Exit Do

                    Ret = GpGet(YrAddr)
                    If CheckRet("GpGet", Ret, ErrText) = 1 Then Exit Do
                    ErrText = "Data sending was terminated."
                Case 1
                    SendBuf = "D" & VB.Right("0" & DVCNT.ToString(), 2) & "E" : Srlen = Len(SendBuf)
                    If DVCNT > 10 Then DVCNT = 0 Else DVCNT = DVCNT + 1
                    SendData.Text = SendBuf
                    Srlen = Len(SendBuf)

                    Cmd(0) = 2
                    Cmd(1) = MyAddr
                    Cmd(2) = YrAddr
                    Ret = GpTalk(pCmd, Srlen, SendBuf)
                    If CheckRet("GpTalk", Ret, ErrText) = 1 Then Exit Do

                    Ret = GpGet(YrAddr)
                    If CheckRet("GpGet", Ret, ErrText) = 1 Then Exit Do
                    TextRet.Text = "Data sending was terminated."
            End Select
            Cmd(0) = 2              'Number of command
            Cmd(1) = &H3FS          'Unlisten/UNL
            Cmd(2) = &H5FS          'Untalken/UNT
            Ret = GpComand(pCmd)    'Command sending
            Exit Sub
        Loop While 0

        SendRun.Enabled = False
        SendStart.Enabled = True
        SendStop.Enabled = False

        Cmd(0) = 2                  'Number of command
        Cmd(1) = &H3FS              'Unlisten/UNL
        Cmd(2) = &H5FS              'Untalken/UNT
        Ret = GpComand(pCmd)        'Command sending

    End Sub

    Private Sub SendStart_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles SendStart.Click '[ A sending start ]

        YrInit.Enabled = False
        SendStart.Enabled = False
        SendStop.Enabled = True

        SendRun.Enabled = True      'A timer start

    End Sub

    Private Sub SendStop_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles SendStop.Click '[ Sending hang-up ]

        YrInit.Enabled = True
        SendStart.Enabled = True
        SendStop.Enabled = False

        SendRun.Enabled = False     'Time switch hang-up

    End Sub

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

        TextRet.Text = ""

    End Sub

    Private Sub TargetName_SelectedIndexChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles TargetName.SelectedIndexChanged '[ Change of transmit data by target change ]

        Select Case (TargetName.SelectedIndex)      'A selection of Address of device
            Case 0                                  'At YOKOGAWA a selection
                SData = "V3P0"
                SendData.Text = "V3P0S00000O1"
            Case 1                                  'At ADVANTEST a selection
                SData = "V5L3L7"
                SendData.Text = "V5L3L7"
        End Select

    End Sub

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

        YrAddr = CInt(YrAdrBox.Text)

    End Sub

    Private Sub YrInit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles YrInit.Click '[ Initialization of Address of device(A confirmation of Address of device) ]

        Cmd(0) = 2
        Cmd(1) = MyAddr
        Cmd(2) = YrAddr
        Srlen = Len(SData)
        Ret = GpTalk(pCmd, Srlen, SData)        'At a command sending
        If (Ret And &HFFS) > 0 Then             'Processing at the time of an error
            CheckRet("GpTalk", Ret, ErrText)
            TextRet.Text = ErrText
        Else                                    'Normal termination
            SendStart.Enabled = True
            SendData.Enabled = False
            TextRet.Text = "Address of device was initialized."
        End If

    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
