-
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
-
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#.