﻿Imports System.Runtime.InteropServices
Imports VB = Microsoft.VisualBasic
Public Class Form1
    Dim SendBuf As String                   'A receiving buffer
    Dim RecvBuf As New String("", 10000)    'A sending buffer
    Dim bTimerOn As Short                   'At a timer selection
    Dim SrqOn As Short                      'On SRQ is used
    Dim MsgNum As Integer                   'Handle of message
    Dim Pstb(15) As Integer                 'An array of status byte
    Dim Adrs As Integer                     'It is used at the of address making
    Dim ErrText As String                   'For a text
    Dim Srlen As Integer                    'A length of a character string
    Dim Delim As Integer                    'Delimiter
    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 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                  'Making address of device
            YrAdrBox.Items.Add(CStr(Adrs))
        Next Adrs

        YrDevice.Items.Add("[ ADVANTEST R6871E Digital MultiMeter ]")
        YrDevice.Items.Add("[ Sanwa Digital Multimeter 9300MF ]")
        YrDevice.Items.Add("[ Iwatsu Digital Multimeter SC-7404 ]")

        DelimBox.Text = "CR+LF"
        YrAdrBox.Text = CStr(2)             'Initial setting
        SendData.Text = "R0"                'Initial setting
        Timer_Renamed.Checked = True        'Initial setting/At a timer selection
        YrDevice.SelectedIndex = 0          'First of all at ADVANTEST a selection

        '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 ]

        Initialize.Enabled = False

        Ret = GpibInit(ErrText)
        If Ret = 1 Then
            TextRet.Text = ErrText
            Initialize.Enabled = True
            Exit Sub
        End If
        If YrAdrBox.Items.Count = 31 Then           'At MyAddress from a list delete
            YrAdrBox.Items.RemoveAt((MyAddr))
            If MyAddr = YrAddr Then
                YrAdrBox.SelectedIndex = 0
            End If
        End If

        PStart.Enabled = True

        SendBuf = SendData.Text
        Srlen = Len(SendBuf)                        'A length of transmit data
        If Srlen = 0 Then
            ErrText = "There is not send data."
            PStart.Enabled = False
            PStop.Enabled = False
            TextRet.Text = ErrText
            Initialize.Enabled = True
            Exit Sub
        End If

        Cmd(0) = 2
        Cmd(1) = MyAddr
        Cmd(2) = YrAddr
        Ret = GpTalk(pCmd, Srlen, SendBuf)        'At data sending
        If CheckRet("GpTalk", Ret, ErrText) = 1 Then
            TextRet.Text = ErrText
            Initialize.Enabled = True
            Exit Sub
        End If

        ErrText = "Initialize was completed."       'Normal termination

        Call DelimBox_SelectedIndexChanged(DelimBox, New System.EventArgs())

        TextRet.Text = ErrText
        Initialize.Enabled = True

    End Sub

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

        PStop.Enabled = True
        PStart.Enabled = False

        MsgNum = &H430S                             'User-defined(&H400 - &H7FFF)

        If Timer_Renamed.Checked = True Then        'At a timer a selection
            If bTimerOn = 0 Then
                Timer1.Enabled = True
                bTimerOn = 1
            End If
        Else
            SrqOn = 1                               'At a On SRQ a selection
            Ret = GpSetEventSrq(Handle.ToInt32, &H430S, SrqOn)
            If CheckRet("GpSetEventSrq", Ret, ErrText) = 1 Then
                TextRet.Text = ErrText
                Exit Sub
            End If
            Ret = GpSetEvent(SrqOn)
            If CheckRet("GpSetEvent", Ret, ErrText) = 1 Then
                TextRet.Text = ErrText
                Exit Sub
            End If
            Ret = GpGet(YrAddr)                     'At [GET] command performance
            If CheckRet("GpGet", Ret, ErrText) = 1 Then
                SrqOn = 0                           'An event is stop in case of an error
                Ret = GpSetEvent(SrqOn)
                CheckRet("GpSetEvent", Ret, ErrText)
            End If
        End If

        TextRet.Text = ErrText

    End Sub

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

        TextRet.Text = "Data receiving was stopped."

        PStop.Enabled = False
        PStart.Enabled = True

        SrqOn = 0                                   'Event stop
        Ret = GpSetEvent(SrqOn)

        bTimerOn = 0
        Timer1.Enabled = False                      'Timer stop

    End Sub

    Private Sub Timer1_Tick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Timer1.Tick '[ Timer ]

        Dim ChkSrq As Integer                       'For check of SRQ
        Dim ErrChk As Short                         'For check of error

        For Cnt = 0 To 15                           'Initialize
            Pstb(Cnt) = 0
        Next
        ErrChk = 0                                  'Initial setting

        Do
            Ret = GpGet(YrAddr)                     'At [GET] command performance
            If CheckRet("GpGet", Ret, ErrText) = 1 Then
                ErrChk = 1 : Exit Do
            End If
            ChkSrq = 0                              'Initialize
            While (ChkSrq = 0)                      'When ChkSrq is 0, it becomes an infinite loop.
                If (EscCheck() = 1) Then            'Exit the loop if the [ESC] key is detected.
                    ErrChk = 1 : ErrText = "Please click the key named [Esc]"
                    Exit Do
                End If
                Ret = GpBoardsts(&H12S, ChkSrq)     'Gets the ChkSrq status (whether SRQ is issued).
                If CheckRet("GpBoardsts", Ret, ErrText) = 1 Then ErrChk = 1 : Exit Do
            End While
            If ErrChk = 1 Then
                Exit Do
            End If

            Do
                Cmd(0) = 1                          'Number of command
                Cmd(1) = YrAddr                     'Address of device
                Ret = GpPoll(pCmd, pPstb)           'They carry out polling.
                GpWaittime(10)
                If Timer1.Enabled = False Then
                    Exit Do
                End If
            Loop While Ret = 128

            If CheckRet("GpPoll", Ret, ErrText) = 1 Then
                ErrChk = 1 : Exit Do
            End If

            Cmd(0) = 2                              'Number of command
            Cmd(1) = YrAddr                         'Address of device
            Cmd(2) = MyAddr                         'My Address
            Srlen = Len(RecvBuf)                    'A length of RecvBuf is measured
            Ret = GpListen(pCmd, Srlen, RecvBuf)    'Data is receiving
            If CheckRet("GpListen", Ret, ErrText) = 1 Then
                ErrChk = 1 : Exit Do
            End If
            Cmd(0) = 2                              'Talker + Listener
            Cmd(1) = &H3FS                          'Unlisten /UNL
            Cmd(2) = &H5FS                          'Untalken /UNT
            Ret = GpComand(pCmd)                    'Command sending
            If CheckRet("GpComand", Ret, ErrText) = 1 Then
                ErrChk = 1 : Exit Do
            End If
            Do
                'n terms of data in the case of 0
                If Srlen = 0 Then Exit Do

                ReceiveData.Text = VB.Left(RecvBuf, Srlen)
            Loop While 0
            TextRet.Text = "Data was done [ " & Srlen & " ] byte receiving."
        Loop While 0
        'When there is an error
        Cmd(0) = 2                  'Number of command
        Cmd(1) = &H3FS              'Unlisten /UNL
        Cmd(2) = &H5FS              'Untalken /UNT
        Ret = GpComand(pCmd)        'Command sending
        'When there is an error, at a time switch stopped
        If ErrChk = 1 Then Timer1.Enabled = False

    End Sub

    Private Sub DelRet_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles DelRet.Click '[ Delete return ]

        TextRet.Text = ""

    End Sub

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

        YrAddr = CInt(YrAdrBox.Text)
        Initialize.Enabled = True

    End Sub

    Private Sub YrDevice_SelectedIndexChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles YrDevice.SelectedIndexChanged '[ Sending data change ]
        Select Case (YrDevice.SelectedIndex)                'Setting of a receiver vessel
            Case 0
                SendData.Text = "F1R5M2IT1DL0RE7BZ1S0"      'Advantest
            Case 1
                SendData.Text = "F1R0I0C1"                  'Sanwa
            Case 2
                SendData.Text = "R0"                        'Iwatsu
        End Select

    End Sub

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

        Ret = GpibExit()
        Me.Close()

    End Sub

    '[ By SRQ receiving ]
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

        If m.Msg = MsgNum Then
            Dim ErrOccured As Short

            ReceiveData.Text = ""
            For Cnt = 0 To 15
                Pstb(Cnt) = 0
            Next
            ErrOccured = 0

            Cmd(0) = 1
            Cmd(1) = YrAddr
            Do
                Ret = GpPoll(pCmd, pPstb)               'Performance of a serial poll
                If CheckRet("GpPoll", Ret, ErrText) = 1 Then ErrOccured = 1 : Exit Do
                Cmd(0) = 2
                Cmd(1) = YrAddr
                Cmd(2) = MyAddr
                Srlen = 10000
                Ret = GpListen(pCmd, Srlen, RecvBuf)        'Performance of receiving
                If CheckRet("GpListen", Ret, ErrText) = 1 Then ErrOccured = 1 : Exit Do
                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 ErrOccured = 1 : Exit Do
                If Srlen <> 0 Then
                    ReceiveData.Text = VB.Left(RecvBuf, Srlen)
                End If
                Ret = GpGet(YrAddr)                         'At [GET] command performance
                If CheckRet("GpGet", Ret, ErrText) = 1 Then ErrOccured = 1 : Exit Do
                ErrText = "Data was done [ " & Srlen & " ] byte receiving."
            Loop While 0
            If ErrOccured = 1 Then
                SrqOn = 0
                Ret = GpSetEventSrq(Handle.ToInt32, &H430S, SrqOn)      'Stop of an event
                Cmd(0) = 2                  'Number of command
                Cmd(1) = &H3FS              'Unlisten /UNL
                Cmd(2) = &H5FS              'Untalken /UNT
                Ret = GpComand(pCmd)        'Command sending
            Else
                Ret = GpEnableNextEvent()   'They make following available
            End If

            TextRet.Text = ErrText
        End If
        MyBase.WndProc(m)
    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
    End Sub
End Class
