﻿Imports VB = Microsoft.VisualBasic
Public Class Form1

    Private mlngRet As Integer
    Private mintDevId As Short

    '**************************************************************************************************
    '   Set initial value
    '**************************************************************************************************
    Private Sub Slave_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
        txtDevName.Text = "GPIB000"
        cboDelim.SelectedIndex = 1 'CR+LF
        cboEoi.SelectedIndex = 1 'Use
        txtSend.Text = "CONTEC Slave Sample" 'Default data to be sent
        txtSPollStb.Text = CStr(0)
    End Sub

    '**************************************************************************************************
    '   Ending process(force to end)
    '**************************************************************************************************
    Private Sub Slave_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
        Call cmdExit_Click(cmdExit, New System.EventArgs())
    End Sub

    '**************************************************************************************************
    '   Display transmitted string to list box of "Return code"(sub routine)
    '   Return value: TRUE(normally), FALSE(abnormally)
    '**************************************************************************************************
    Private Function funcSetlstRet(ByRef strRet As String) As Boolean
        Dim blnRet As Boolean
        blnRet = False 'Return value

        If Len(strRet) = 0 Then 'Check the number of the string
            blnRet = True
        Else
            If lstRet.Items.Count >= 256 Then 'Set the maximum number of list box
                lstRet.Items.RemoveAt(0)
                lstRet.Items.Add(strRet)
            Else
                lstRet.Items.Add(strRet)
            End If
            lstRet.SelectedIndex = lstRet.Items.Count - 1
        End If
        funcSetlstRet = blnRet
    End Function
    '**************************************************************************************************
    '   Set the target (sub routine)
    '   Function: Set Equipment ID, Delimiter and EOI
    '   Return value: TRUE(normally), FALSE(abnormally)
    '**************************************************************************************************
    Private Function funcSetParam() As Boolean

        Dim Delim As Short
        Dim Eoi As Short
        Dim Eos As Short
        Dim strTmp As System.Text.StringBuilder

        'Set delimiter and EOI line
        Delim = cboDelim.SelectedIndex
        Eoi = cboEoi.SelectedIndex
        Eos = 0
        mlngRet = GpibSetDelim(mintDevId, Delim, Eoi, Eos)

        'Process return value
        If mlngRet <> 0 Then
            strTmp = New System.Text.StringBuilder("", 256)
            Call GpibGetErrorString(mlngRet, strTmp)
            Call funcSetlstRet(strTmp.ToString)
        Else
            'Display
            strTmp = New System.Text.StringBuilder("[Partner apparatus setting] completed normally")
            Call funcSetlstRet(strTmp.ToString)
        End If

        funcSetParam = True

    End Function

    '**************************************************************************************************
    '   Initialization
    '   Function: Get Device ID from device name
    '**************************************************************************************************
    Private Sub cmdInit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdInit.Click
        Dim strTmp As System.Text.StringBuilder
        Dim lngData As Integer

        'Perform initialization function
        mlngRet = GpibInit(txtDevName.Text, mintDevId)
        'Process return value
        If mlngRet <> 0 Then
            strTmp = New System.Text.StringBuilder("", 256)
            Call GpibGetErrorString(mlngRet, strTmp)
            Call funcSetlstRet(strTmp.ToString)
        Else
            'When got Device ID normally
            strTmp = New System.Text.StringBuilder("GpibInit : Device ID = " & mintDevId)
            Call funcSetlstRet(strTmp.ToString)
            'Get devive address
            mlngRet = GpibGetStatus(mintDevId, &H8S, lngData)
            If mlngRet = 0 Then txtMyAddr.Text = CStr(lngData)
            'Confirm whether it is set to slave in property page
            mlngRet = GpibGetStatus(mintDevId, &HAS, lngData)
            'Confirm whether it is set to slave
            If (mlngRet = 0) And (lngData = 0) Then
                Call MsgBox("It is not a slave. Please set it to slave in property page", MsgBoxStyle.OkOnly)
            End If
            'Display
            strTmp = New System.Text.StringBuilder("[Initialize] completed normally")
            Call funcSetlstRet(strTmp.ToString)
        End If

        GpibSendIFC(mintDevId, 1)   'Send IFC
    End Sub
    '**************************************************************************************************
    '   Exit
    '   Function: Disable Device ID
    '**************************************************************************************************
    Private Sub cmdExit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdExit.Click
        Dim strTmp As System.Text.StringBuilder

        'Proform exit function
        mlngRet = GpibExit(mintDevId)
        'Process return value
        If mlngRet <> 0 Then
            strTmp = New System.Text.StringBuilder("", 256)
            Call GpibGetErrorString(mlngRet, strTmp)
            Call funcSetlstRet(strTmp.ToString)
        End If
        End
    End Sub


    '**************************************************************************************************
    '   Send Data
    '   Function: Send data to slave device
    '**************************************************************************************************
    Private Sub cmdSend_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdSend.Click
        Dim SendLen As Integer
        Dim strTmp As System.Text.StringBuilder
        Dim SendBuf As System.Text.StringBuilder

        'Set Equipment ID, delimiter and EOI
        If funcSetParam() = False Then Exit Sub
        'Get data to be sent from edit box
        SendBuf = New System.Text.StringBuilder(txtSend.Text)
        SendLen = Len(txtSend.Text)
        If SendLen = 0 Then
            strTmp = New System.Text.StringBuilder("GpibSendData : there is no data")
            funcSetlstRet(strTmp.ToString)
            Exit Sub
        End If

        funcSetlstRet("GpibSendData : sending data...")
        'Send data
        mlngRet = GpibSendData(mintDevId, SendLen, SendBuf)
        If mlngRet <> 0 Then
            'Error
            strTmp = New System.Text.StringBuilder("", 256)
            Call GpibGetErrorString(mlngRet, strTmp)
            Call funcSetlstRet(strTmp.ToString)
        Else
            'When sent data normally
            strTmp = New System.Text.StringBuilder("GpibSendData : the number of sent data = " & SendLen)
            Call funcSetlstRet(strTmp.ToString)
            strTmp = New System.Text.StringBuilder("[Send data] completed normally")
            Call funcSetlstRet(strTmp.ToString)
        End If
    End Sub

    '**************************************************************************************************
    '   Receive data
    '   Function: Receive data from master device
    '**************************************************************************************************
    Private Sub cmdRec_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdRec.Click
        Dim RecLen As Integer
        Dim strTmp As System.Text.StringBuilder
        Dim RecBuf As System.Text.StringBuilder


        'Set Equipment ID, delimiter and EOI
        If funcSetParam() = False Then Exit Sub

        RecBuf = New System.Text.StringBuilder("", 256)
        RecLen = 256 'Set recieving buffer size
        Call funcSetlstRet("GpibRecData : receiving data...") 'Display in list
        'Receive data
        mlngRet = GpibRecData(mintDevId, RecLen, RecBuf)
        RecBuf.Length = RecLen
        If mlngRet <> 0 Then
            'Error
            strTmp = New System.Text.StringBuilder("", 256)
            Call GpibGetErrorString(mlngRet, strTmp)
            Call funcSetlstRet(strTmp.ToString) 'Display in list
        Else
            'When received data normally
            txtRec.Text = VB.Left(RecBuf.ToString, RecLen)
            strTmp = New System.Text.StringBuilder("GpibRecData : the number of received data = " & RecLen)
            Call funcSetlstRet(strTmp.ToString) 'Display in list
            strTmp = New System.Text.StringBuilder("[Receive data] completed normally")
            Call funcSetlstRet(strTmp.ToString) 'Display in list
        End If
    End Sub

    '**************************************************************************************************
    '   Set status byte
    '   Function: Transmit SRQ and set status byte
    '**************************************************************************************************
    Private Sub cmdSendSrq_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdSendSrq.Click
        Dim strTmp As System.Text.StringBuilder
        Dim intSrqSend As Short

        'Get setting value of SRQ
        If optSrqNotSend.Checked = True Then
            intSrqSend = 0
        Else
            intSrqSend = 1
        End If
        'Transmit SRQ and perform the functin of seting status byte
        mlngRet = GpibSendSRQ(mintDevId, intSrqSend, Val(txtSPollStb.Text))
        'Process return value
        If mlngRet <> 0 Then
            strTmp = New System.Text.StringBuilder("", 256)
            Call GpibGetErrorString(mlngRet, strTmp)
            Call funcSetlstRet(strTmp.ToString)
        Else
            'Display
            strTmp = New System.Text.StringBuilder("[Transmit SRQ and set status byte] completed normally")
            Call funcSetlstRet(strTmp.ToString)
        End If
    End Sub

    '**************************************************************************************************
    '   Set parallel poll response
    '**************************************************************************************************
    Private Sub cmdParallelpollSetting_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdParallelpollSetting.Click

        Dim Response As Short
        Dim Ist As Short
        Dim strTmp As System.Text.StringBuilder


        If optPPollResponseNot.Checked = True Then
            '[No response]
            Response = 0
        ElseIf optPPollResponseIst0.Checked = True Then
            '[Response of ISTbitiIST=0j]
            Response = 1
            Ist = 0
        ElseIf optPPollResponseIst1.Checked = True Then
            '[Response of ISTbitiIST=1j]
            Response = 1
            Ist = 1
        ElseIf optPPollResponseSrq.Checked = True Then
            '[Response with SRQ]
            Response = 2
        End If

        'Perform the function of setting parallel poll response
        mlngRet = GpibSetPPollResponse(mintDevId, Response)
        If mlngRet <> 0 Then
            'Error
            strTmp = New System.Text.StringBuilder("", 256)
            Call GpibGetErrorString(mlngRet, strTmp)
            Call funcSetlstRet(strTmp.ToString)
        Else
            strTmp = New System.Text.StringBuilder("[Set parallel poll response] completed normally")
            Call funcSetlstRet(strTmp.ToString)
        End If

        If Response = 1 Then
            ' Perform the function of setting ISTbit
            mlngRet = GpibSetIst(mintDevId, Ist)
            If mlngRet <> 0 Then
                'Error
                strTmp = New System.Text.StringBuilder("", 256)
                Call GpibGetErrorString(mlngRet, strTmp)
                Call funcSetlstRet(strTmp.ToString)
            Else
                strTmp = New System.Text.StringBuilder("[Set IST of parallel poll response] completed normally")
                Call funcSetlstRet(strTmp.ToString)
            End If
        End If

    End Sub

    '**************************************************************************************************
    '   Clear send/receive text data
    '**************************************************************************************************
    Private Sub cmdDataDel_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdDataDel.Click
        txtSend.Text = ""
        txtRec.Text = ""
    End Sub

    '**************************************************************************************************
    '   Clear return code
    '**************************************************************************************************
    Private Sub cmdRetDel_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdRetDel.Click
        lstRet.Items.Clear()
    End Sub

    '**************************************************************************************************
    '   Input rule(only number)
    '**************************************************************************************************
    Private Sub txtSPollStb_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtSPollStb.KeyPress
        Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
        If (KeyAscii < Asc("0") Or KeyAscii > Asc("9")) And KeyAscii <> System.Windows.Forms.Keys.Back Then
            KeyAscii = 0
        End If
        If KeyAscii = 0 Then
            eventArgs.Handled = True
        End If
    End Sub
End Class
