Imports easyUSBHidNetClass

Public Class Form1

    Dim DataWrite(64) As Byte 'Buffer write
    Dim DataRead(64) As Byte  'Buffer read

    

    Public Sub New()
        InitializeComponent()
    End Sub


    Private Sub EasyUSBHidNetClass1_anyDeviceUSB_found(sender As Object, e As EventArgs) Handles EasyUSBHidNetClass1.anyDeviceUSB_found
        lblStatusUSB.Text = "Any Device was removed"
    End Sub
    'This funtion is called when any USB HID device removed. 
    Private Sub EasyUSBHidNetClass1_anyDeviceUSB_removed(sender As Object, e As EventArgs) Handles EasyUSBHidNetClass1.anyDeviceUSB_removed
        '    InvokeRequired required compares the thread ID of the
        '    calling thread to the thread ID of the creating thread.
        '    If these threads are different, it returns true.  We can
        '    use this attribute to determine if we can append text
        '    directly to the textbox or if we must launch an a delegate
        '    function instance to write to the textbox/label.
        If InvokeRequired Then
            '    InvokeRequired returned TRUE meaning that this function
            '    was called from a thread different than the current
            '    thread.  We must launch a deleage function.
            '    Invoke the new delegate sending the same Data to the
            '    delegate that was passed into this function from the
            '    other thread.
            Invoke(New EventHandler(AddressOf EasyUSBHidNetClass1_anyDeviceUSB_removed), New Object() {sender, e})
        Else
            ' If this function was called from the same thread that 
            ' holds the required objects then just add the text.
            Me.lblStatusUSB.Text = ("Any Device not found")
        End If
    End Sub
    '****************************************************************************
    'Function:
    '            private void EasyUSBHidNetClass1_DeviceUSB_dataReceived(object sender, DataRecievedEventArgs args)

    'Summary:
    '            This function prints any data received on the USB.

    'Description:
    '            This function is called when the data was received for device.  This
    '            function attempts to write that data to the txtDataReceived textbox.  

    'Precondition:
    '        None()

    'Parameters:
    '            object sender     - Sender of the event (this form)
    '            args              - The event arguments

    '        Return Values
    '        None()

    'Remarks:
    '        None()
    '          ***************************************************************************/
    'this funtion is called when any data USB is received. The variable args received the Data
    Private Sub EasyUSBHidNetClass1_DeviceUSB_dataReceived(sender As Object, args As DataRecievedEventArgs) Handles EasyUSBHidNetClass1.DeviceUSB_dataReceived
        '    InvokeRequired required compares the thread ID of the
        '    calling thread to the thread ID of the creating thread.
        '    If these threads are different, it returns true.  We can
        '    use this attribute to determine if we can append text
        '    directly to the textbox or if we must launch an a delegate
        '    function instance to write to the textbox/label.
        If InvokeRequired Then
            '    InvokeRequired returned TRUE meaning that this function
            '    was called from a thread different than the current
            '    thread.  We must launch a deleage function.
            '    Invoke the new delegate sending the same Data to the
            '    delegate that was passed into this function from the
            '    other thread.
            Try
                Invoke(New DataRecievedEventHandler(AddressOf EasyUSBHidNetClass1_DeviceUSB_dataReceived), New Object() {sender, args})
            Catch ex As Exception

            End Try
        Else
            ' If this function was called from the same thread that 
            ' holds the required objects then just add the text.
            ' ***********************************
            '       //Example: 
            '       // args.data contains the value received on the USB.
            '       // Buffer PC        Buffer Microcontroller
            '       // args.data(0) <=> writebuff(0) 
            '       // args.data(1) <=> writebuff(1)
            '       // args.data(2) <=> writebuff(2)
            '       // ...
            '       // args.data(63) <=> writebuff(63)
            '       //************************************
            Try
                'Dim i As Integer
                ' i = ((args.data(12) * 256) + (args.data(11))) 'byteHigh + byteLow = Int -> AN4 (0 ~1023) 
                lblResult.Text = args.data(0).ToString()
            Catch

            End Try

        End If
    End Sub

    'This is necessary to register the this.Handle 
    Protected Overrides Sub OnHandleCreated(ByVal e As EventArgs)
        MyBase.OnHandleCreated(e)
        EasyUSBHidNetClass1.RegisterHandle(Handle)
    End Sub
    'This is a callback function that gets called when a Windows message is received by the form.
    'We will receive various different types of messages, but the ones we really want to use are the WM_DEVICECHANGE messages.
    Protected Overrides Sub WndProc(ByRef m As Message)
        EasyUSBHidNetClass1.USBHidMensagens(m)
        MyBase.WndProc(m)

    End Sub

    Private Sub EasyUSBHidNetClass1_DeviceUSB_dataSent(sender As Object, e As EventArgs) Handles EasyUSBHidNetClass1.DeviceUSB_dataSent
        lblStatusUSB.Text = "Data sent successfully!"
    End Sub

    Private Sub EasyUSBHidNetClass1_DeviceUSB_found(sender As Object, e As EventArgs) Handles EasyUSBHidNetClass1.DeviceUSB_found
        lblStatusUSB.Text = "Device found!"
    End Sub

    Private Sub EasyUSBHidNetClass1_DeviceUSB_removed(sender As Object, e As EventArgs) Handles EasyUSBHidNetClass1.DeviceUSB_removed
        lblStatusUSB.Text = "Device removed"
    End Sub

    Private Sub Form1_Activated(sender As Object, e As EventArgs) Handles MyBase.Activated
        EasyUSBHidNetClass1.SerialNumber = "DEMO VERSION" 'YOUR SERIAL NUMBER: license USBHidNetClass 
        EasyUSBHidNetClass1.DeviceUSB_Config_VID_PID(&H1234, &H1) 'VID and PID
    End Sub

    Private Sub Toggle_LED_Click(sender As Object, e As EventArgs) Handles Toggle_LED.Click
        DataWrite(63) = DataWrite(63) Xor 1
        SendDataToDevice(DataWrite)
    End Sub

    Private Sub SendDataToDevice(ByVal DataWrite() As Byte)
        Try
            If Me.EasyUSBHidNetClass1.DeviceUsbHID IsNot Nothing Then      'VERIFY device USB conection 
                Me.EasyUSBHidNetClass1.DeviceUsbHID.writeData(0, DataWrite) 'Send to Microcontroller the USB Writebuffer 
            Else
                MessageBox.Show("was not possible to send data")
            End If
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub
End Class
