OscilloscopeGeneratorTrigger.vb

' OscilloscopeGeneratorTrigger.vb
'
' This example sets up the generator to generate a 1 kHz triangle waveform, 4 Vpp.
' It also sets up the oscilloscope to perform a block mode measurement, triggered on "Generator new period".
' A measurement is performed and the data is written to OscilloscopeGeneratorTrigger.csv.
'
' Find more information on http://www.tiepie.com/LibTiePie .

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

Module OscilloscopeGeneratorTriggerExample

    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 and a generator in the same device:
        Dim scp As Oscilloscope = Nothing
        Dim gen As Generator = 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) And item.CanOpen(DeviceType.Generator) Then
                    scp = item.OpenOscilloscope()

                    If (scp.MeasureModes And Constants.MM_BLOCK) <> 0 Then
                        gen = item.OpenGenerator()
                        Exit For
                    Else
                        scp.Dispose()
                        scp = Nothing
                    End If
                End If
            Next
        End If

        If Not IsNothing(scp) And Not IsNothing(gen) Then
            Try
                ' Oscilloscope settings:

                ' 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 = 1 ' 1 s

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

                ' Locate trigger input:
                Dim triggerInput As TriggerInput = scp.TriggerInputs.GetById(Constants.TIID_GENERATOR_NEW_PERIOD) ' or Constants.TIID_GENERATOR_START or Constants.TIID_GENERATOR_STOP

                If IsNothing(triggerInput) Then
                    Throw New System.Exception("Unknown trigger input!")
                End If

                ' Enable trigger source:
                triggerInput.Enabled = True

                ' Generator settings:

                ' Set signal type:
                gen.SignalType = Constants.ST_TRIANGLE

                ' Set frequency:
                gen.Frequency = 1000.0 ' 1 kHz

                ' Set amplitude:
                gen.Amplitude = 2 ' 2 V

                ' Set offset:
                gen.Offset = 0 ' 0 V

                ' Enable output:
                gen.OutputOn = True

                ' Print oscilloscope info:
                PrintDeviceInfo(scp)

                ' Print generator info:
                PrintGeneratorInfo(gen)

                ' Start measurement:
                scp.Start()

                ' Start signal generation:
                gen.Start()

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

                ' Stop generator:
                gen.Stop()

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

                ' Open file with write/update permissions:
                Dim filename As String = "OscilloscopeGeneratorTrigger.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

            ' Close generator:
            gen.Dispose()
            gen = Nothing
        Else
            Console.WriteLine("No oscilloscope available with block measurement support or generator available in the same unit!")
            Environment.Exit(1)
        End If

        Environment.Exit(0)
    End Sub

End Module