jklemmack/csharp-usb-hid-driver

Event Handling in VB.net

Opened this issue · 2 comments

Hi...

I was able to write data to the device using usb.write() function.In the post 
it says there are 2 ways to read data from the device. I tried to integrate usb 
event handler with VB.net but cudnt compile the Vb application. Is there any 
working example for VB.net using event handler ??? All i want is acquiring the 
received byte array as an event driven function. Please can some1 send me the 
code ? rnsburg@gmail.com

Thank You
Regards
Anuradha 

Original issue reported on code.google.com by rnsb...@gmail.com on 10 May 2012 at 4:41

Public C2 As New USBHIDDRIVER.USBInterface("vid_1781", "pid_07d0")
Public USBbuff(64) As Byte
C2.enableUsbBufferEvent(New System.EventHandler(AddressOf HIDEvent))

Public Sub HIDEvent()
        Dim x As Integer

        'Console.WriteLine("Event Caught")
        If USBHIDDRIVER.USBInterface.usbBuffer.Count > 0 Then
            Console.WriteLine("Event Caught: Buffer Count =" & USBHIDDRIVER.USBInterface.usbBuffer.Count)
            While USBHIDDRIVER.USBInterface.usbBuffer.Count > 0
                USBbuff = USBHIDDRIVER.USBInterface.usbBuffer(0)
                USBHIDDRIVER.USBInterface.usbBuffer.RemoveAt(0)
            End While
        Else
            Console.WriteLine("Event Caught: No Items")
        End If
    End Sub

Original comment by GeoffCal...@gmail.com on 23 Jan 2013 at 10:24

Public Sub HIDEvent()
        While USBHIDDRIVER.USBInterface.usbBuffer.Count > 0
            USBbuff = USBHIDDRIVER.USBInterface.usbBuffer(0)
            SyncLock (USBHIDDRIVER.USBInterface.usbBuffer.SyncRoot)
                USBHIDDRIVER.USBInterface.usbBuffer.RemoveAt(0)
            End SyncLock
            DataRecieved(USBbuff)

        End While
    End Sub
    Delegate Sub DataRecievedCallback(UsbIN() As Byte)
    Private Sub DataRecieved(ByVal UsbIN() As Byte)
        Dim x As Integer        
        If Me.TextBox_Cell.InvokeRequired Then
            Dim d As New DataRecievedCallback(AddressOf DataRecieved)
            Me.Invoke(d, New Object() {UsbIN})
        Else
           //Process your data of UsbIN here
        End If
    End Sub


I am still having an issue where if a lot of packets come in from the device, I 
will lose some of them. Is there anyway to make sure no packets get lost.

Original comment by GeoffCal...@gmail.com on 25 Jan 2013 at 1:58