OscilloscopeBlock.vb

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

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

Module OscilloscopeBlockExample

    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 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 = 10000 ' 10 kS
                Dim recordLength As UInt64 = scp.RecordLength ' Read actual record length.

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

                ' 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

                ' 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 data:
                Dim data As Single()() = scp.GetData()

                ' Open file with write/update permissions:
                Dim filename As String = "OscilloscopeBlock.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 i As UInt16 = 0 To channelCount - 1
                        File.Write(String.Format(";Ch{0}", i + 1))
                    Next
                    File.Write(Environment.NewLine)

                    ' Write the data to csv:
                    For i As UInt64 = 0 To recordLength - 1
                        File.Write(i.ToString)
                        For ch As UInt16 = 0 To channelCount - 1
                            File.Write(";" + data(ch)(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 support!")
            Environment.Exit(1)
        End If

        Environment.Exit(0)
    End Sub

End Module