Results 1 to 4 of 4

Thread: Finding and using valid com ports

  1. #1

    Thread Starter
    Addicted Member aa9gg's Avatar
    Join Date
    Apr 2009
    Location
    Chicagoland
    Posts
    184

    Finding and using valid com ports

    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
    Last edited by aa9gg; Feb 12th, 2010 at 01:43 PM. Reason: wrong forum????
    FCC Section 97.313(a) “At all times, an amateur station must use the minimum transmitter power necessary to carry out the desired communications.”

    I'd rather run a "Killer-Watt" than a KiloWatt - QRP Rules!!!

  2. #2
    Lively Member
    Join Date
    Sep 2009
    Posts
    95

    Re: Finding and using valid com ports

    Quote Originally Posted by aa9gg View Post
    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):

    Code:
    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

  3. #3

    Thread Starter
    Addicted Member aa9gg's Avatar
    Join Date
    Apr 2009
    Location
    Chicagoland
    Posts
    184

    Re: Finding and using valid com ports

    That does not return any usb serial ports. Here is what I wound up doing using a frame/listbox/and a button:

    Code:
    '---------------------------------------------------------------
    ' -----------------------------------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.
    FCC Section 97.313(a) “At all times, an amateur station must use the minimum transmitter power necessary to carry out the desired communications.”

    I'd rather run a "Killer-Watt" than a KiloWatt - QRP Rules!!!

  4. #4
    Lively Member
    Join Date
    Sep 2009
    Posts
    95

    Re: Finding and using valid com ports

    Quote Originally Posted by aa9gg View Post
    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:

    Code:
    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.
    Last edited by NeedHelp!; Feb 19th, 2010 at 04:29 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width