SureConnect oscilloscope example

View on GitHub

' OscilloscopeConnectionTest.vb
'
' This example performs a connection test.
'
' Find more information on http://www.tiepie.com/LibTiePie .

Imports System
Imports System.Threading
Imports TiePie.LibTiePie

Module OscilloscopeConnectionTestExample

    Sub Main()
        ' Print library information:
        PrintLibraryInfo()

        ' Enable network search:
        Network.AutoDetectEnabled = True

        ' Update device list:
        DeviceList.Update()

        ' Try to open an oscilloscope with connection test support:
        Dim scp As Oscilloscope = Nothing

        If DeviceList.Count <> 0 Then
            For i As UInt32 = 0 To DeviceList.Count - 1
                Dim item As DeviceListItem = DeviceList.GetItemByIndex(i)
                If item.CanOpen(DeviceType.Oscilloscope) Then
                    scp = item.OpenOscilloscope()

                    ' Check for connection test support:
                    If scp.HasConnectionTest Then
                        Exit For
                    Else
                        scp.Dispose()
                        scp = Nothing
                    End If
                End If
            Next
        End If

        If Not IsNothing(scp) Then
            Try
                ' Get the number of channels:
                Dim channelCount As UInt16 = scp.Channels.Count

                ' Enable all channels that support connection testing:
                For Ch As UInt16 = 0 To channelCount - 1
                    scp.Channels(Ch).Enabled = scp.Channels(Ch).HasConnectionTest
                Next

                ' Start connection on current active channels:
                scp.StartConnectionTest()

                ' Wait for connectiontest to complete:
                While Not scp.IsConnectionTestCompleted
                    Thread.Sleep(10) ' 10 ms delay, to save CPU time.
                End While

                ' Get data:
                Dim Data() As TriState = scp.GetConnectionTestData

                ' Print results:
                Console.WriteLine("Connection test result:")
                For ch As UInt16 = 0 To channelCount - 1
                    Console.Write("Ch" + ch.ToString + " = ")

                    Select Case Data(ch)
                        Case TriState.Undefined
                            Console.WriteLine("undefined")
                        Case TriState.False
                            Console.WriteLine("false")
                        Case TriState.True
                            Console.WriteLine("true")
                        Case Else
                            Console.WriteLine("unknown state")
                    End Select
                Next

            Catch e As System.Exception
                Console.WriteLine("Exception: " + e.Message)
                System.Environment.Exit(1)
            End Try

            ' Close oscilloscope:
            scp.Dispose()
            scp = Nothing
        Else
            Console.WriteLine("No oscilloscope available with connection test support!")
            Environment.Exit(1)
        End If

        Environment.Exit(0)
    End Sub

End Module