Click to See Complete Forum and Search --> : Finding and using valid com ports
aa9gg
Feb 11th, 2010, 06:13 PM
I just spent all day searching the web trying to find this answer with no luck. Tons of stuff, but not for VB6.
I'm working with an application using one of those usb to rs232 cables and I need my program to know what port the adapter installed itself on. How does one scan for valid com ports? It may not be this device if they have an actual com port on the computer, so I just need to know and make available all installed com ports.
I think this is the wrong forum.....re-posting in VB6 and earlier
NeedHelp!
Feb 19th, 2010, 07:49 AM
I think this is the wrong forum.....re-posting in VB6 and earlier
Yes, that is the wrong forum... ;)
But I did not find your post in the other forum. So i am answering here...
If you still need help, try this (you need the MSComm control):
Public Function OpenPort(iPort as Integer) As Boolean
If iPort = 0 Then
Call MsgBox("Please specify port number.", vbInformation, "No port specified")
Exit Function
End If
On Error GoTo PortError
With Form1.MSComm1
.CommPort = iPort
.PortOpen = True
End With
OpenPort = True
Exit Function
PortError:
OpenPort = False
MsgBox "Could not open COM port " & Port, vbCritical, "Error!"
End Function
aa9gg
Feb 19th, 2010, 08:20 AM
That does not return any usb serial ports. Here is what I wound up doing using a frame/listbox/and a button:
'---------------------------------------------------------------
' -----------------------------------put this on menu option click
'---------------------------------------------------------------
Public Sub select_serial_port()
' this routine scans for ALL serial ports and displays in listbox
Form1.com_select.Clear ' clears out COM_SELECT listbox entries
Set WMIObjectSet = GetObject("winmgmts:\\.\root\CIMV2").ExecQuery("SELECT * FROM Win32_PnPEntity") 'Win32_SerialPort")
For Each wmiobject In WMIObjectSet
If InStr(wmiobject.Name, "COM") Then ' this a valid com port
If InStr(wmiobject.Name, "COM ") Then GoTo nope ' this is not
Form1.com_select.AddItem wmiobject.Name
nope: End If
Next
Set WMIObjectSet = Nothing
Form1.com_port_sel_frame.Visible = True
End Sub
'---------------------------------------------------------------
' --------------------------------------- put this on list click
'---------------------------------------------------------------
Public Sub com_select_Click() ' pick com port from list
list_pos = com_select.ListIndex ' position in list
length = Len(com_select.List(list_pos)) ' size of port info string
find_pos = (InStr(com_select, "COM") + 3) ' get ready to extract port #
length = length - find_pos
port_info = Mid$(com_select, find_pos, length) ' extract port #'
SELECTED_PORT_NAME.Caption = com_select.List(list_pos) 'port_info
SESSION_PORT_NUMBER = Val(port_info)
com_port_sel_frame.Visible = False
End Sub
'---------------------------------------------------------------
' --------------------------------------- put this on button click
'---------------------------------------------------------------
Private Sub PORT_SELECT_Click()
If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
On Error GoTo trap_error
MSComm1.Settings = 19200 ' baud rate always the same
MSComm1.CommPort = Val(SESSION_PORT_NUMBER)
If MSComm1.PortOpen = False Then MSComm1.PortOpen = True
com_port_sel_frame.Visible = False
GoTo done
trap_error: error_msg = MsgBox("Internal Error....Please Select Again", vbExclamation)
done:
End Sub
It displays the device name as well as the com port in the list box. From there com port number is extracted from the string value. Works very well.
NeedHelp!
Feb 19th, 2010, 03:26 PM
That does not return any usb serial ports.
Yes, it just checks (with the help of following loop), if the port is available or not:
For i = 1 to 4 'usually there is a maximum of 4 COM ports
If OpenPort(i) Then
'... e.g. enabling CheckBox(i)
End If
Next i
- I thought, this was your original question and its a little faster.
It displays the device name as well as the com port in the list box. From there com port number is extracted from the string value. Works very well.
It's good, but it is not perfect: on my system it also finds "COMODO Internet Security Firewall Driver" and "COMODO Internet Security Helper Driver" on port zero.
- You maybe should exclude all results with the COM port number zero, before it is listed in the ListBox to make it even better. :)
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.