OscilloscopeBlockSegmented.vb

' OscilloscopeBlockSegmented.vb
'
' This example performs a block mode measurement of 5 segments and writes the data to OscilloscopeBlockSegmented.csv.
'
' Find more information on http://www.tiepie.com/LibTiePie .

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

Module OscilloscopeBlockSegmentedExample

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

        ' Enable network search:
        Network.AutoDetectEnabled = True

        ' Update device list:
        DeviceList.Update()

        ' Try to open an oscilloscope with block 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 block measurement support:
                    If ((scp.MeasureModes And Constants.MM_BLOCK) <> 0) And (scp.SegmentCountMax > 1) 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.Block

                ' Set sample frequency:
                scp.SampleFrequency = 1000000.0 ' 1 MHz

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

                ' Set pre sample ratio:
                scp.PreSampleRatio = 0 ' 0 %

                ' Set segment count:
                scp.SegmentCount = 5 ' 5 segments

                Dim channel As OscilloscopeChannel

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

                    ' Disable channel:
                    channel.Enabled = False

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

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

                ' Enable channel 1:
                channel = scp.Channels(0)
                channel.Enabled = True

                ' Set trigger timeout:
                scp.TriggerTimeOut = 0.1 ' 100 ms

                ' Disable all channel trigger sources:
                For ch As UInt16 = 0 To channelCount - 1
                    scp.Channels(ch).Trigger.Enabled = False
                Next

                ' Setup channel trigger:
                Dim channelTrigger As OscilloscopeChannelTrigger = scp.Channels(0).Trigger ' Ch 1

                ' Enable trigger source:
                channelTrigger.Enabled = True

                ' Kind:
                channelTrigger.Kind = TriggerKind.RisingEdge

                ' Level:
                channelTrigger.Level(0) = 0.5 ' 50 %

                ' Hysteresis:
                channelTrigger.Hysteresis(0) = 0.05 ' 5 %

                ' Print oscilloscope info:
                PrintDeviceInfo(scp)

                ' Start measurement:
                scp.Start()

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

                ' Get all data from the scope:
                Dim data(scp.SegmentCount - 1)()() As Single
                Dim seg As UInt16 = 0
                While (scp.IsDataReady)
                    data(seg) = scp.GetData()
                    seg += 1
                End While

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

                ' Write the data to csv:
                If IO.File.Exists(filename) Then
                    ' Write csv header:
                    File.Write("Sample")
                    For seg = 0 To scp.SegmentCount - 1
                        File.Write(String.Format(";Segment{0}", seg + 1))
                    Next
                    File.Write(Environment.NewLine)

                    ' Write the Ch1 data to csv:
                    For i As UInt64 = 0 To recordLength - 1
                        File.Write(i.ToString)
                        For seg = 0 To scp.SegmentCount - 1
                            File.Write(";" + data(seg)(0)(i).ToString)
                        Next
                        File.Write(Environment.NewLine)
                    Next

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

                    ' Close file:
                    File.Close()
                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 block measurement and segmented trigger support!")
            Environment.Exit(1)
        End If

        Environment.Exit(0)
    End Sub

End Module