Generator triggered burst example

View on GitHub

' GeneratorTriggeredBurst.vb
'
' This example generates a 100 kHz square waveform, 25% duty cycle, 0..5 V, 20 periods, this waveform is triggered by the external trigger (EXT 1).
' Connect EXT 1 to GND to trigger the burst.
'
' Find more information on http://www.tiepie.com/LibTiePie .

Imports System
Imports TiePie.LibTiePie

Module GeneratorTriggeredBurstExample

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

        ' Enable network search:
        Network.AutoDetectEnabled = True

        ' Update device list:
        DeviceList.Update()

        ' Try to open a generator with triggered burst support:
        Dim gen As Generator = Nothing

        If DeviceList.Count <> 0 Then
            For i As UInt32 = 0 To DeviceList.Count - 1
                If DeviceList.GetItemByIndex(i).CanOpen(DeviceType.Generator) Then
                    gen = DeviceList.GetItemByIndex(i).OpenGenerator()

                    ' Check for triggered burst support:
                    If (gen.ModesNative And Constants.GM_BURST_COUNT) <> 0 And gen.TriggerInputs.Count > 0 Then
                        Exit For
                    Else
                        gen.Dispose()
                        gen = Nothing
                    End If
                End If
            Next
        End If

        If Not IsNothing(gen) Then
            Try
                ' Set signal type:
                gen.SignalType = SignalType.Square

                ' Set frequency:
                gen.Frequency = 100000.0 ' 100 kHz

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

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

                ' Set symmetry (duty cycle):
                gen.Symmetry = 0.25 ' 25 %

                ' Set burst mode:
                gen.Mode = GeneratorMode.BurstCount

                ' Set burst count:
                gen.BurstCount = 20 ' 20 periods

                ' Locate trigger input:
                Dim triggerInput As TriggerInput = gen.TriggerInputs.GetById(Constants.TIID_EXT1)

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

                ' Enable trigger input:
                triggerInput.Enabled = True

                ' Set trigger input kind:
                triggerInput.Kind = TriggerKind.FallingEdge

                ' Enable output:
                gen.OutputOn = True

                ' Print generator info:
                PrintDeviceInfo(gen)

                ' Start signal generation
                gen.Start()

                ' Wait for keystroke:
                Console.WriteLine("Press Enter to stop signal generation...")
                Console.ReadLine() '

                ' Stop generator:
                gen.Stop()

                ' Disable output:
                gen.OutputOn = False

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

            ' Close generator:
            gen.Dispose()
            gen = Nothing
        Else
            Console.WriteLine("No generator available with triggered burst support!")
            Environment.Exit(1)
        End If

        Environment.Exit(0)
    End Sub

End Module