none
Detecting a serial COM Port

    Question

  • I am developing a UI for an embedded device that I have designed and built.  The device uses an FT232 chip which, when connected via USB is automatically assigned one of the COM ports.  I was wondering if there is a way to set up a loop which would automatically detect which COM port the device has been assigned.  I could then add some handshaking to verify that the port is correct.
    Monday, November 09, 2009 4:44 PM

Answers

  • I provide code like this in my book, Visual Basic Programmer's Guide to Serial Communications 4, which provides more detailed information that that provided by the built-in SerialPort object (using the WMI wrapper types in System.Management).  However, SerialPort.GetPorts() returns a string array of port names for installed and active ports.  Perhaps this is all that you need?

      Dim MyPorts() As String = SerialPort.GetPorts()
      For I As Integer = 0 to MyPorts.Length -1
         Debug.Print (MyPorts(I))  'see the active ports here
      Next I

    Dick
    Dick Grier, MVP. Author of Visual Basic Programmer's Guide to Serial Communications 4. See www.hardandsoftware.net.
    • Marked as answer by Jeff Shan Monday, November 16, 2009 1:26 AM
    Monday, November 09, 2009 5:08 PM
  • I've had experiences of the SerialPort.GetPorts returning names of ports that do not exist (having existed but been unplugged, for example). Its been rare, but it has happened on more than one occasion.

    Its a bit more of a pain but i've used one of the API calls from the FTDI library to determine what port a device is currently connected to - if you know you only have 1 device connected, passing 1 (or it could be a 0 if its zero based) will give you the name of the port currently assigned to that device.

    One of the other methods is to use the WM_DEVICECHANGE message, looking for the DBT_DEVICEARRIVAL and DBT_DEVICEREMOVALCOMPLETE events to monitor device attachment before casting the LParam pointer to a DBT_DEVTYP_PORT structure, giving you the name of the serial port just added/removed from the system.

    This may not be of any use, but it could be another approach.

    Kev
    • Marked as answer by Jeff Shan Monday, November 16, 2009 1:26 AM
    Tuesday, November 10, 2009 6:38 PM

All replies

  • I provide code like this in my book, Visual Basic Programmer's Guide to Serial Communications 4, which provides more detailed information that that provided by the built-in SerialPort object (using the WMI wrapper types in System.Management).  However, SerialPort.GetPorts() returns a string array of port names for installed and active ports.  Perhaps this is all that you need?

      Dim MyPorts() As String = SerialPort.GetPorts()
      For I As Integer = 0 to MyPorts.Length -1
         Debug.Print (MyPorts(I))  'see the active ports here
      Next I

    Dick
    Dick Grier, MVP. Author of Visual Basic Programmer's Guide to Serial Communications 4. See www.hardandsoftware.net.
    • Marked as answer by Jeff Shan Monday, November 16, 2009 1:26 AM
    Monday, November 09, 2009 5:08 PM
  • Dick,

       Thanks for the help.  I think I may be able to code a handshake algorithm inside that for loop to see if the current serial port in the loop is the one connected to the embedded device.  Do you think that is a good approach to this problem?

    -Eric

    Monday, November 09, 2009 7:08 PM
  • Sure.  That is what I do for GPS devices and modems.  If there is something specific that you know about the data format, then it should be straignt forward.

    Dick
    Dick Grier, MVP. Author of Visual Basic Programmer's Guide to Serial Communications 4. See www.hardandsoftware.net.
    Tuesday, November 10, 2009 5:22 PM
  • I've had experiences of the SerialPort.GetPorts returning names of ports that do not exist (having existed but been unplugged, for example). Its been rare, but it has happened on more than one occasion.

    Its a bit more of a pain but i've used one of the API calls from the FTDI library to determine what port a device is currently connected to - if you know you only have 1 device connected, passing 1 (or it could be a 0 if its zero based) will give you the name of the port currently assigned to that device.

    One of the other methods is to use the WM_DEVICECHANGE message, looking for the DBT_DEVICEARRIVAL and DBT_DEVICEREMOVALCOMPLETE events to monitor device attachment before casting the LParam pointer to a DBT_DEVTYP_PORT structure, giving you the name of the serial port just added/removed from the system.

    This may not be of any use, but it could be another approach.

    Kev
    • Marked as answer by Jeff Shan Monday, November 16, 2009 1:26 AM
    Tuesday, November 10, 2009 6:38 PM