для просмотра доступных портов вы можете использовать:
'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-устройства
обратите внимание: когда USB-устройство подключено через другой USB-порт, преобразованный порт RS232 также может быть другим