Public Class Main
    Public Sub InstrWrite(rsrcName As String, strCmd As String)
        Dim status As Long
        Dim dfltRM As Long
        Dim sesn As Long
        Dim rSize As Long

        strCmd += ChrW(10) ' add "\n"
        'Initialize the system
        status = viOpenDefaultRM(dfltRM)
        'Failed to Initialize the system
        If (status < VI_SUCCESS) Then
            MsgBox("No VISA resource was opened！")
            Exit Sub
        End If
        'Open the VISA instrument
        status = viOpen(dfltRM, rsrcName, VI_NULL, VI_NULL, sesn)
        'Failed to open the instrument
        If (status < VI_SUCCESS) Then
            ' MsgBox("Failed to open the instrument！")
            Exit Sub
        End If


        'Write command to the instrument
        status = viWrite(sesn, System.Text.Encoding.Default.GetBytes(strCmd), Len(strCmd), rSize)
        'Failed to write to the instrument
        If (status < VI_SUCCESS) Then
            ' MsgBox("Faild to write to the instrument！")
            Exit Sub
        End If

        'Close the session
        status = viClose(sesn)

        status = viClose(dfltRM)

    End Sub
    Public Function InstrRead(rsrcName As String) As String
        Dim status As Long
        Dim dfltRM As Long
        Dim sesn As Long
        Dim recv(512) As Byte
        Dim strTemp As String
        Dim rSize As Long

        'Begin by initializing the system
        status = viOpenDefaultRM(dfltRM)
        'Initialize failed
        If (status < VI_SUCCESS) Then
            MsgBox("No VISA instrument was opened!")
            InstrRead = ""
            Exit Function
        End If
        'Open the instrument
        status = viOpen(dfltRM, rsrcName, VI_NULL, VI_NULL, sesn)
        'Open instrument failed
        If (status < VI_SUCCESS) Then
            'MsgBox("Failed to open the instrument!")
            InstrRead = ""
            Exit Function
        End If

        'Read from the instrument
        status = viRead(sesn, recv, 512, rSize)
        'Read failed
        If (status < VI_SUCCESS) Then
            'MsgBox("Failed to read from the instrument!")
            InstrRead = ""
            Exit Function
        End If

        'Close the system
        status = viClose(sesn)

        status = viClose(dfltRM)

        'Remove the space at the end of the string
        Dim zeroChars() As Char = {ChrW(0)}
        strTemp = System.Text.Encoding.Default.GetString(recv, 0, rSize).TrimEnd(zeroChars)
        InstrRead = strTemp
    End Function

    Private Sub Connect_Click(sender As Object, e As EventArgs) Handles Connect.Click
        Dim status As Long
        Dim dfltRM As Long
        Dim fList As Long
        Dim nList As Long
        Dim rsrcName As System.Text.StringBuilder
        Dim i, j As Long
        Dim strRet As String
        Dim strSrc As String = ""
        Dim bFind As Boolean

        rsrcName = New System.Text.StringBuilder
        'Open the VISA instrument
        status = viOpenDefaultRM(dfltRM)
        'Failed to open the instrument
        If (status < VI_SUCCESS) Then
            MsgBox("No VISA resource was opened ！")
            Exit Sub
        End If

        'Find instrument resource
        Call viFindRsrc(dfltRM, "?*", fList, nList, rsrcName)
        'Get the list of the instrument(resource)
        strRet = ""
        bFind = False
        For i = 0 To nList - 1
            'Get the instrument name
            InstrWrite(rsrcName.ToString, "*IDN?")
            System.Threading.Thread.Sleep(200)

            strRet = InstrRead(rsrcName.ToString)
            strSrc = rsrcName.ToString
            'Continue to switch the resource until find a instrument
            strRet = UCase(strRet)
            If (strRet.Contains("HDM")) Then
                bFind = True
                Exit For
            End If

            Call viFindNext(fList + i - 1, rsrcName)
        Next i

        If (bFind = True) Then
            InstrAddr.Text = strSrc
        Else
            InstrAddr.Text = ""
            MsgBox("Didn't find any device!")
        End If
    End Sub

    Private Sub BtnSendCommand_Click(sender As Object, e As EventArgs) Handles BtnSendCommand.Click
        If (InstrAddr.Text = "") Then
            MsgBox("Please write the instrument address！")
        End If

        InstrWrite(InstrAddr.Text, TxtCommand.Text)
    End Sub

    Private Sub BtnReadResponse_Click(sender As Object, e As EventArgs) Handles BtnReadResponse.Click
        Dim strTemp As String
        strTemp = InstrRead(InstrAddr.Text)
        TxtResponse.Text = strTemp
    End Sub

    Private Sub BtnSendAndRead_Click(sender As Object, e As EventArgs) Handles BtnSendAndRead.Click
        Dim strTemp As String
        If (InstrAddr.Text = "") Then
            MsgBox("Please write the instrument address！")
        End If

        InstrWrite(InstrAddr.Text, TxtCommand.Text)
        strTemp = InstrRead(InstrAddr.Text)
        TxtResponse.Text = strTemp
    End Sub
End Class
