OscilloscopeStream.vb

' OscilloscopeStream.vb
'
' This example performs a stream mode measurement and writes the data to OscilloscopeStream.csv.
'
' Find more information on http://www.tiepie.com/LibTiePie .

Imports System
Imports System.IO
Imports System.Threading
Imports TiePie.LibTiePie

Module OscilloscopeStreamExample

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

        ' Enable network search:
        Network.AutoDetectEnabled = True

        ' Update device list:
        DeviceList.Update()

        ' Try to open an oscilloscope with with stream measurement 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 stream measurement support:
                    If (scp.MeasureModes And Constants.MM_STREAM) <> 0 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

                ' Set measure mode:
                scp.MeasureMode = MeasureMode.Stream

                ' Set sample frequency:
                scp.SampleFrequency = 1000.0 ' 1 kHz

                ' Set record length:
                scp.RecordLength = 1000 ' 1 kS
                Dim recordLength As UInt64 = scp.RecordLength ' Read actual record length.

                ' For all channels:
                For ch As UInt16 = 0 To channelCount - 1
                    Dim channel As OscilloscopeChannel = scp.Channels(ch)

                    ' Enable channel to measure it:
                    channel.Enabled = True

                    ' Set range:
                    channel.Range = 8 ' 8 V

                    ' Set coupling:
                    channel.Coupling = Coupling.DCV ' DC Volt
                Next

                ' Print oscilloscope info:
                PrintDeviceInfo(scp)

                ' Start measurement:
                scp.Start()

                ' Open file with write/update permissions:
                Dim filename As String = "OscilloscopeStream.csv"
                Dim File As New StreamWriter(filename, False)

                If IO.File.Exists(filename) Then
                    ' Write csv header:
                    File.Write("Sample")
                    For i As UInt16 = 0 To channelCount - 1
                        File.Write(String.Format(";Ch{0}", i + 1))
                    Next
                    File.Write(Environment.NewLine)

                    Dim currentSample As UInt64 = 0

                    ' Measure 10 chunks:
                    For chunk As UInt16 = 0 To 9
                        ' Print a message, to inform the user that we still do something:
                        Console.WriteLine("Data chunk " + chunk.ToString)

                        ' Wait for measurement to complete:
                        While Not (scp.IsDataReady Or scp.IsDataOverflow)
                            Thread.Sleep(10) ' 10 ms delay, to save CPU time.
                        End While

                        ' Throw error on data overflow:
                        If scp.IsDataOverflow Then
                            Throw New System.Exception("Data overflow!")
                        End If

                        ' Get data:
                        Dim data As Single()() = scp.GetData()

                        ' Write the data to csv:
                        For i As UInt64 = 0 To recordLength - 1
                            File.Write(currentSample + i.ToString)
                            For ch As UInt16 = 0 To channelCount - 1
                                File.Write(";" + data(ch)(i).ToString)
                            Next
                            File.Write(Environment.NewLine)
                        Next

                        currentSample += recordLength
                    Next

                    Console.WriteLine("Data written to: " + filename)

                    ' Close file:
                    File.Close()

                    ' Stop measurement:
                    scp.Stop()
                Else
                    Console.WriteLine("Couldn't open file: " + filename)
                    Environment.Exit(1)
                End If

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

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

        Environment.Exit(0)
    End Sub

End Module