Results 1 to 2 of 2

Thread: Detecting USB Device

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2012
    Posts
    27

    Detecting USB Device

    I need assistance converting the following C# code which works into VB code. The code detects when a specific type of USB device is connected and disconnected and writes the status to a status strip:

    Code:
    App_PnP_Callback evHandler = new App_PnP_Callback(PnP_Event_Handler);
    usbDevices = new USBDeviceList(CyConst.DEVICES_HID, evHandler);
    
     public void PnP_Event_Handler(IntPtr pnpEvent, IntPtr hRemovedDevice)
            {
                if (pnpEvent.Equals(CyConst.DBT_DEVICEREMOVECOMPLETE))
                {
                    USBEventArgs e = new USBEventArgs();
                    usbDevices.Remove(hRemovedDevice, e);
                }
    
                if (pnpEvent.Equals(CyConst.DBT_DEVICEARRIVAL))
                {
                    usbDevices.Add();
                }
    
                myHidDevice = usbDevices[VID, PID] as CyHidDevice;
                CyHidDevice myHidReport = myHidDevice as CyHidDevice;
    
    
                if (CheckGenericHidDevice())
                {
                    Status.Text = "Connected";
                    this.Refresh();
                }
                else
                {
                    Status.Text = "Disconnected";
                    this.Refresh();
                }
            }
    
    
            private bool CheckGenericHidDevice()
            {
                myHidDevice = usbDevices[VID, PID] as CyHidDevice; //Get hold of Voltmeter Device
                if (myHidDevice == null)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
    My attempt at converting the code to VB.net. The code compiles but does not detect when I add/remove the usb device:

    Code:
    Dim evHandler As App_PnP_Callback = New App_PnP_Callback(AddressOf PnP_Event_Handler)
            usbDevices = New USBDeviceList(CyConst.DEVICES_HID, evHandler)
    
        End Sub
    
        Private Sub PnP_Event_Handler(ByVal pnpEvent As IntPtr, ByVal hRemovedDevice As IntPtr)
    
            If pnpEvent.Equals(CyConst.DBT_DEVICEREMOVECOMPLETE) Then
    
                Dim e As USBEventArgs = New USBEventArgs()
                usbDevices.Remove(hRemovedDevice, e)
                'StatusStrip1.Text = "DisConnected"
    
    
    
                If pnpEvent.Equals(CyConst.DBT_DEVICEARRIVAL) Then
    
                    usbDevices.Add()
                    'StatusStrip1.Text = "Connected"
    
                End If
    
    
                myHidDevice = usbDevices(VID, PID)
                Dim myHidReport As CyHidDevice = myHidDevice
    
    
                If CheckGenericHidDevice() Then
    
                    StatusStrip1.Text = "Connected"
                    Me.Refresh()
    
                Else
    
                    StatusStrip1.Text = "Disconnected"
                    Me.Refresh()
    
                End If
            End If
    
    
        End Sub
    
        Private Function CheckGenericHidDevice() As Boolean
    
            myHidDevice = usbDevices(VID, PID) 'as CyHidDevice
            If myHidDevice.Equals(Nothing) Then
    
                Return False
    
            Else
    
            Return True
            End If
    
        End Function

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Detecting USB Device

    The first issue I see is the fact that you have nested If statements where there are none in the original C#.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width