如何检索连接到 USB 端口的 USB 设备的端口号? [英] How might I retrieve the port number of a USB device that is connected to a USB port?

查看:85
本文介绍了如何检索连接到 USB 端口的 USB 设备的端口号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 VB6 检索连接到 USB 端口的 USB 设备的端口号?我已经可以得到 USB 设备名称,让我们说:\\?\USB#Vid_0801&Pid_2250#7&91e2848&0&1#{4d36e978-e325-11ce-bfc1-08002be10318}

How might I retrieve the port number of a USB device that is connected to a USB port, using VB6? I can already get the USB Device name, let's say: \\?\USB#Vid_0801&Pid_2250#7&91e2848&0&1#{4d36e978-e325-11ce-bfc1-08002be10318}

如何自动检测分配给它的端口号?我正在制作一个程序,该程序将自动确定连接到任何 USB 端口的特定设备的端口号,并自动开始与其通信.

How do I automatically detect what port number is assigned to it? I am making a program that will automatically determine the port number of a specific device attached to any of the USB port and start communicating on it automatically.

推荐答案

查看您可以使用的可用端口:

to view the available comports you can use :

'1 form with :
'    1 combobox : name=Combo1
'    1 mscomm control : name=MSComm1

Option Explicit

Public Enum PortAttr
  PortFree = 0
  PortInUse = 1
  PortUnknown = 2
End Enum

Private Sub Form_Load()
  'show available ports
  ShowPorts
End Sub

Private Sub ShowPorts()
  Dim intIndex As Integer
  Dim intPort As Integer
  Dim intFree As Integer
  On Error GoTo ErrorFound
  With MSComm1
    If .PortOpen Then .PortOpen = False
    intPort = .CommPort
    Combo1.Clear
    Combo1.AddItem "--- Not Used ---"
    Combo1.ItemData(0) = -2 'not possible
    Combo1.AddItem "---- In Use ----"
    Combo1.ItemData(1) = -2 'not possible
    intFree = 0
    For intIndex = 1 To 10
      Select Case CheckPort(intIndex)
        Case PortFree
          intFree = intFree + 1
          Combo1.AddItem "Com" & CStr(intIndex), intFree
          Combo1.ItemData(intFree) = intIndex
        Case PortInUse
          Combo1.AddItem "Com" & CStr(intIndex)
      End Select
    Next intIndex
    If .PortOpen Then .PortOpen = False
    .CommPort = intPort
    If CheckPort(intPort) = PortFree Then
      If .PortOpen = False Then .PortOpen = True
    End If
  End With 'MSComm1
Exit Sub
ErrorFound:
  MsgBox Err.Description, vbCritical, "Error " & CStr(Err.Number)
  On Error GoTo 0
End Sub

Private Function CheckPort(intPort As Integer) As PortAttr
  On Error GoTo ErrorFound
  With MSComm1
    If .PortOpen Then .PortOpen = False
    .CommPort = intPort
    .PortOpen = True
    CheckPort = PortFree
    If .PortOpen = False Then .PortOpen = True
  End With 'MSComm1
Exit Function
ErrorFound:
  Select Case Err.Number
    Case 8002 'port doesnt exist
      CheckPort = PortUnknown
    Case 8005 'port already in use
      CheckPort = PortInUse
  End Select
  On Error GoTo 0
End Function

在该列表中你可以标记你已经知道的端口,所以其他的应该是来自USB设备的端口

in that list you can mark the ports which you already know, so the others should be the port from the usb device

注意:当USB设备通过另一个USB端口连接时,转换后的rs232端口也可能是不同的

pay attention : when the usb device is connected through another usb port, the converted rs232 port might be a different one as well

这篇关于如何检索连接到 USB 端口的 USB 设备的端口号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆