'
'Phidget Hello World Program for all devices
'(c) Phidgets 2012
'

Module Module1

    ' ========= General VB.NET Functions =============
    Sub printError(ByVal number As Integer, ByVal description As String)
        Console.WriteLine("Error on function call: {0} - {1}!", number, description)

    End Sub

    ' ========== Event Handling Functions ==========
    Private Sub manager_Attach(ByVal sender As Object, ByVal e As Phidgets.Events.AttachEventArgs) Handles manager.Attach
        Dim serialNumber As Integer
        Dim name As String

        Try
            serialNumber = e.Device.SerialNumber
            name = e.Device.Name
            Console.WriteLine("Hello Device {0}, Serial Number: {1}!", serialNumber.ToString, name)
        Catch ex As Phidgets.PhidgetException
            printError(ex.Code, ex.Description)
        End Try
    End Sub

    Private Sub manager_Detach(ByVal sender As Object, ByVal e As Phidgets.Events.DetachEventArgs) Handles manager.Detach
        Dim serialNumber As Integer
        Dim name As String

        Try
            serialNumber = e.Device.SerialNumber
            name = e.Device.Name
            Console.WriteLine("Goodbye Device {0}, Serial Number: {1}!", serialNumber.ToString, name)
        Catch ex As Phidgets.PhidgetException
            printError(ex.Code, ex.Description)
        End Try
    End Sub

    Private Sub manager_Error(ByVal sender As Object, ByVal e As Phidgets.Events.ErrorEventArgs) Handles manager.Error
        Dim number As Integer
        Dim description As String

        number = e.Code
        description = e.Description

        'The error triggered by the event
        Console.WriteLine("Error Event: {0} - {1}!", number.ToString, description)

    End Sub



    Dim WithEvents manager As Phidgets.Manager

    ' =========== Main Code ==========
    Sub Main()

        manager = New Phidgets.Manager()

        'No exception thrown on open
        manager.open()

        Console.WriteLine("Phidget Simple Playground (plug and unplug devices)")
        Console.WriteLine("Press Enter to end anytime")
        Console.Read()

        Console.WriteLine("Closing...")

        Try
            RemoveHandler manager.Attach, AddressOf manager_Attach
            RemoveHandler manager.Detach, AddressOf manager_Detach
            RemoveHandler manager.Error, AddressOf manager_Error
            manager.close()
            manager = Nothing
        Catch ex As Phidgets.PhidgetException
            printError(ex.Code, ex.Description)
        End Try

    End Sub

End Module
