' GeneratorBurst.vb
'
' This example generates a 50 Hz sine waveform, 4 Vpp, 100 periods.
'
' Find more information on http://www.tiepie.com/LibTiePie .
Imports System
Imports System.Threading
Imports TiePie.LibTiePie
Module GeneratorBurstExample
    Sub Main()
        ' Print library information:
        PrintLibraryInfo()
        ' Enable network search:
        Network.AutoDetectEnabled = True
        ' Update device list:
        DeviceList.Update()
        ' Try to open a generator with burst support:
        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.Generator) Then
                    gen = item.OpenGenerator()
                    ' Check for burst support:
                    If (gen.ModesNative And Constants.GM_BURST_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.Sine
                ' Set frequency:
                gen.Frequency = 50.0 ' 50 Hz
                ' Set amplitude:
                gen.Amplitude = 2.0 ' 2 V
                ' Set offset:
                gen.Offset = 0.0 ' 0 V
                ' Set burst mode:
                gen.Mode = GeneratorMode.BurstCount
                ' Set burst count:
                gen.BurstCount = 100 ' 100 periods
                ' Enable output:
                gen.OutputOn = True
                ' Print Generator info:
                PrintDeviceInfo(gen)
                ' Start signal generation
                gen.Start()
                ' Wait for burst to complete:
                While gen.IsBurstActive
                    Thread.Sleep(10) ' 10 ms delay, to save CPU time.
                End While
                ' 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 burst support!")
            Environment.Exit(1)
        End If
        Environment.Exit(0)
    End Sub
End Module
