我需要创建一个程序来检测是否插入了某个USB设备。因此,可以说我们有一个具有VID (Vendor ID) = 9839
和的设备PID (Product ID) = 5453
。
我需要一个代码,当我插入设备时,程序会自动获取VID
和PID
的设备,并将它们写入两个texbox中。
在那之后很简单,我使用:
If textbox1.Text = "9839" And textbox2.Text = "5453" then
MsgBox("You plugged the device!")
Else
MsgBox("Device is not plugged")
End If
但我需要的代码用于获取VID
和PID
插入的设备到文本框。因此,如果有人可以帮助我,请告诉我:)
我尝试使用USBCLASSLibrary演示解决方案,但它是免费的dll,但我的电脑是x64的,而dll是x32的,所以我在C#(错误的图像格式)或其他错误中遇到错误。
我尝试使用在CodeProject上找到的代码
private void USBPort_USBDeviceAttached(object sender,
USBClass.USBDeviceEventArgs e)
{
if (!MyUSBDeviceConnected)
{
if (USBClass.GetUSBDevice(MyDeviceVID, MyDevicePID,
ref USBDeviceProperties, false))
{
//My Device is connected
MyUSBDeviceConnected = true;
}
}
}
private void USBPort_USBDeviceRemoved(object sender,
USBClass.USBDeviceEventArgs e)
{
if (!USBClass.GetUSBDevice(MyDeviceVID, MyDevicePID,
ref USBDeviceProperties, false))
{
//My Device is removed
MyUSBDeviceConnected = false;``
}
}
您是否尝试过HID?
Debug.WriteLine(" HIDD_ATTRIBUTES structure filled without error.")
Debug.WriteLine(" Structure size: " & MyHid.DeviceAttributes.Size)
Debug.WriteLine(" Vendor ID: " & Hex(MyHid.DeviceAttributes.VendorID))
Debug.WriteLine(" Product ID: " & Hex(MyHid.DeviceAttributes.ProductID))
Debug.WriteLine(" Version Number: " & Hex(MyHid.DeviceAttributes.VersionNumber))
然后,尝试:
Try
myVendorID = Int32.Parse(txtVendorID.Text, NumberStyles.AllowHexSpecifier)
myProductID = Int32.Parse(txtProductID.Text, NumberStyles.AllowHexSpecifier)
Catch ex As Exception
End Try
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句