Module ScopeMoudule
    Public myMgr As Ivi.Visa.Interop.ResourceManager
    Public myscope As Ivi.Visa.Interop.FormattedIO488

    Private strQueryResult As String

    Public Function ScopeOpen(ScopeID As String, timeoutNum As Integer) As String

        Try
            myMgr = New Ivi.Visa.Interop.ResourceManager
            myscope = New Ivi.Visa.Interop.FormattedIO488

            myscope.IO = myMgr.Open(ScopeID)
            myscope.IO.Clear()
            myscope.IO.Timeout = timeoutNum

            strQueryResult = DoQueryString("*IDN?")
            Debug.Print("Identification string: " + strQueryResult)

            Return strQueryResult
        Catch ex As Exception
            Return "FAIL"
        End Try
    End Function

    Public Function ScopeRunCk() As Boolean
        Try
            strQueryResult = DoQueryString("*IDN?")
            If InStr(strQueryResult, "KEYSIGHT") > 0 Then
                Return False
            Else
                Return True
            End If
        Catch ex As Exception
            Return True
        End Try
    End Function

    Public Function DoCommand(command As String) As String
        Dim errStr As String

        Try
            myscope.WriteString(command)
            errStr = CheckInstrumentErrors()
            If errStr.Replace(" ", "") <> "" Then
                Return errStr
            End If

            Return "PASS"
        Catch ex As Exception
            Return "VISA COM Error: " + vbCrLf + CStr(Err.Number) + ", " + Err.Source + "," + Err.Description
        End Try

    End Function

    '    Public Sub DoCommand(command As String)
    '        On Error GoTo VisaComError
    '        myscope.WriteString(command)
    '        CheckInstrumentErrors()
    '        Exit Sub

    'VisaComError:
    '        MsgBox("VISA COM Error: " + vbCrLf + CStr(Err.Number) + ", " + Err.Source + "," + Err.Description, vbExclamation, "VISA COM Error")
    '    End Sub

    Public Function DoQueryString(query As String) As String
        Dim errStr As String
        Try

            myscope.WriteString(query)
            DoQueryString = myscope.ReadString
            errStr = CheckInstrumentErrors()
            If errStr.Replace(" ", "") <> "" Then
                Return DoQueryString & " / " & errStr
            End If
            Return DoQueryString

        Catch ex As Exception
            Return "VISA COM Error: " + vbCrLf + CStr(Err.Number) + ", " + Err.Source + "," + Err.Description
        End Try

    End Function

    '    Public Function DoQueryString(query As String) As String
    '        Dim errStr As String
    '        On Error GoTo VisaComError
    '        myscope.WriteString(query)
    '        DoQueryString = myscope.ReadString
    '        CheckInstrumentErrors()
    '        Return DoQueryString
    'VisaComError:
    '        MsgBox("VISA COM Error: " + vbCrLf + CStr(Err.Number) + ", " + Err.Source + "," + Err.Description, vbExclamation, "VISA COM Error")
    '        Return "FAIL"
    '    End Function

    Private Function CheckInstrumentErrors() As String
        Dim strerrval As String
        Dim strout As String = ""

        myscope.WriteString(":SYSTem:ERRor?")
        strerrval = myscope.ReadString
        While Val(strerrval) <> 0
            strout = "INST Error: " + strerrval
            myscope.WriteString(":SYSTem:ERRor?")
            strerrval = myscope.ReadString
        End While

        If Not strout = "" Then
            MsgBox(strout, vbExclamation, "INST Error Messages")
            myscope.FlushWrite(False)
            myscope.FlushRead()
            Return strout
        End If

        Return ""
    End Function

    'Private Sub CheckInstrumentErrors()
    '    Dim strerrval As String
    '    Dim strout As String = ""

    '    myscope.WriteString(":SYSTem:ERRor?")
    '    strerrval = myscope.ReadString
    '    While Val(strerrval) <> 0
    '        strout = "INST Error: " + strerrval
    '        myscope.WriteString(":SYSTem:ERRor?")
    '        strerrval = myscope.ReadString
    '    End While

    '    If Not strout = "" Then
    '        MsgBox(strout, vbExclamation, "INST Error Messages")
    '        myscope.FlushWrite(False)
    '        myscope.FlushRead()
    '    End If
    'End Sub

End Module
