我需要创建一个程序,以检测某个USB设备是否已插入。因此,让我们说,我们有一个设备,有VID (Vendor ID) = 9839
和PID (Product ID) = 5453
。
我需要一个代码,当我插入设备时,程序会自动获得设备的VID
和PID
,并将它们写在两个文本框中。
在简单之后,我使用:
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的解决方案,Demo是一个免费的dll,但是我的pc是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;``
}
}
发布于 2013-12-29 14:20:24
你试过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
发布于 2014-12-22 17:25:18
创建名为cmbHdd的组合框,并将代码放在窗体上。这将填充与usb设备的组合,并从Win32_DiskDrive获得所有所需的信息,以及Win32_USBHub的PID和供应商ID希望,这有助于。麦伐他耳队
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim mosDisks As New ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive")
For Each moDisk As ManagementObject In mosDisks.[Get]()
cmbHdd.Items.Add(moDisk("Model").ToString())
Next
End Sub
Private Sub cmbHdd_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles cmbHdd.SelectedIndexChanged
Try
Dim GetQuery As String = ("SELECT * FROM Win32_DiskDrive WHERE Model = '" & cmbHdd.SelectedItem & "'")
Dim mosDisks As New ManagementObjectSearcher(GetQuery)
For Each moDisk As ManagementObject In mosDisks.[Get]()
lblType.Text = moDisk("MediaType").ToString().Trim
lblModel.Text = moDisk("Model").ToString().Trim
lblSerial.Text = moDisk("SerialNumber").ToString().Trim
lblInterface.Text = moDisk("InterfaceType").ToString().Trim
lblCapacity.Text = moDisk("Size").ToString() & " bytes (" & Math.Round((((CDbl(Convert.ToDouble(moDisk("Size"))) / 1024) / 1024) / 1024), 2) & " GB)".Trim
lblPartitions.Text = moDisk("Partitions").ToString().Trim
lblSignature.Text = moDisk("Signature").ToString().Trim
lblFirmware.Text = moDisk("FirmwareRevision").ToString().Trim
lblSylinders.Text = moDisk("TotalCylinders").ToString().Trim
lblSectors.Text = moDisk("TotalSectors").ToString().Trim
lblHeads.Text = moDisk("TotalHeads").ToString().Trim
lblTracks.Text = moDisk("TotalTracks").ToString().Trim
lblBytesPerSector.Text = moDisk("BytesPerSector").ToString().Trim
lblSectorsPerTrack.Text = moDisk("SectorsPerTrack").ToString().Trim
lblTrackPerCylinder.Text = moDisk("TracksPerCylinder").ToString().Trim
' lblProductID.Text = moDisk("PNPDeviceID").ToString().Trim
lblVendorID.Text = moDisk("PNPDeviceID").ToString().Trim
Next
Dim USBClass As New System.Management.ManagementClass("Win32_USBHub")
Dim USBCollection As System.Management.ManagementObjectCollection = USBClass.GetInstances()
Dim _USB As System.Management.ManagementObject
Dim _tempID As String = ""
For Each _USB In USBCollection
Dim splitString As String() = (_USB("DeviceID")).Split(New [Char]() {"/"c, "\"c, CChar(vbTab)})
_tempID = splitString(1)
If (lblVendorID.Text).Contains(splitString(2)) Then
lblSerial.Text = splitString(2)
Exit For
End If
_tempID = ""
Next
If _tempID <> "" Then
Dim splitID As String() = _tempID.Split(New [Char]() {"&"c, CChar(vbTab)})
Dim splitVendor As String() = splitID(0).Split(New [Char]() {"_"c, CChar(vbTab)})
Dim splitProduct As String() = splitID(1).Split(New [Char]() {"_"c, CChar(vbTab)})
lblVendorID.Text = splitVendor(1)
lblProductID.Text = splitProduct(1)
End If
Catch ex As Exception
End Try
End Sub
https://stackoverflow.com/questions/20825321
复制相似问题