Click here to Skip to main content
15,879,474 members
Articles / Desktop Programming / MFC
Article

EnumSerialPorts v1.02 - Enumerating Serial Ports

Rate me:
Please Sign up or sign in to vote.
4.77/5 (18 votes)
3 Mar 2000 219.4K   5.4K   42   28
A freeware MFC function to enumerate serial ports.
  • Download source files - 7 Kb
  • Introduction

    Welcome to EnumSerialPorts v1.01, a simple MFC function to enumerate the serial ports installed on your machine.


    Features
    Usage
    History
    API Reference
    Contacting the Author


    Features

    • Simple C style function interface.
    • The code works on both Windows 95/98 and NT.
    • The code is fully Unicode compliant and include Unicode built options in the workspace file.


    Usage

    • To use the function in your code simply #include "enumser.cpp" in your project and #include "enumser.h" in which ever of your modules needs to make calls to the function.
    • Your code will need to include MFC either statically or dynamically.
    • To see the class in action, have a look at the main function in the module main.cpp.


    History

    v1.0 (3 November 1998)
    • Initial Public release.

    v1.01 (23 February 1999)

    • Code now uses QueryDosDevice() if running on NT to determine which serial ports are available. This avoids having to open the ports at all. It should operate a lot faster in addition.

    V1.02 (12 December 1999)

    • Fixed a problem in the Win9x code path when trying to detect deactivated IRDA-ports. When trying to open those, you will get the error-code ERROR_GEN_FAILURE.


    API Reference

    EnumerateSerialPorts

    void ::EnumerateSerialPorts(CUIntArray& ports);

    Parameters:

    • ports -- Upon return this array will contain the port numbers of all the serial ports installed on the computer.



    Contacting the Author

    PJ Naughter
    Email: pjn@indigo.ie
    Web: http://www.naughter.com
    12 December 1999


    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    Written By
    United States United States
    This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

    Comments and Discussions

     
    GeneralUintArray Pin
    drequinox4-Sep-06 10:44
    drequinox4-Sep-06 10:44 
    GeneralProblem: Other devices starting with "Com"... Pin
    Supermagle11-Nov-05 3:03
    Supermagle11-Nov-05 3:03 
    GeneralRe: Problem: Other devices starting with "Com"... Pin
    Supermagle11-Nov-05 3:12
    Supermagle11-Nov-05 3:12 
    GeneralVIRTUAL SERIAL PORT Pin
    Joseph126-May-03 8:53
    Joseph126-May-03 8:53 
    GeneralRe: VIRTUAL SERIAL PORT Pin
    Member 22530723-Feb-05 16:28
    Member 22530723-Feb-05 16:28 
    GeneralSuggestion for another method Pin
    chris10919-Mar-02 3:03
    chris10919-Mar-02 3:03 
    GeneralThe way I do it ... Pin
    24-Aug-01 7:36
    suss24-Aug-01 7:36 
    GeneralIncorrect COM0 detection Pin
    6-Aug-01 20:26
    suss6-Aug-01 20:26 
    GeneralRe: Incorrect COM0 detection Pin
    12-Aug-01 14:18
    suss12-Aug-01 14:18 
    GeneralRe: Incorrect COM0 detection Pin
    12-Aug-01 14:31
    suss12-Aug-01 14:31 
    GeneralThere is a better way to enumerate ports... Pin
    Christian B25-Jul-01 1:15
    Christian B25-Jul-01 1:15 
    GeneralHelp for Windows CE Pin
    Chris Pichon19-May-01 5:48
    Chris Pichon19-May-01 5:48 
    GeneralRe: Help for Windows CE Pin
    susilrani9-May-08 0:22
    susilrani9-May-08 0:22 
    Generali need to detect a modem . . . Pin
    guy_almo19-Sep-00 5:37
    guy_almo19-Sep-00 5:37 
    GeneralUse vector<UINT> instead of CUIntArray Pin
    Leow9-Apr-00 16:22
    Leow9-Apr-00 16:22 
    GeneralRe: Use vector<UINT> instead of CUIntArray Pin
    pjnaughter16-May-00 13:57
    pjnaughter16-May-00 13:57 
    GeneralRe: Use vector<UINT> instead of CUIntArray Pin
    19-Apr-02 4:50
    suss19-Apr-02 4:50 
    GeneralVirtual Ports Pin
    CMG4-Apr-00 12:06
    CMG4-Apr-00 12:06 
    GeneralRe: Virtual Ports Pin
    PJ Naughter5-Apr-00 11:46
    PJ Naughter5-Apr-00 11:46 
    GeneralRe: Virtual Ports Pin
    Eldad Kuperman12-Apr-00 23:25
    sussEldad Kuperman12-Apr-00 23:25 
    I'm using the following code for enumeration. Tested succesfuly on Win98, NT and 2000. Hope it will work for you.
    No CreateFile/CloseHandle is needed.

    BOOL IsPortAvailable(int nPort)
    {
    TCHAR szPort[15];
    COMMCONFIG cc;
    DWORD dwCCSize;

    _stprintf(szPort, _T("COM%d"), nPort);

    // Check if this port is available
    dwCCSize = sizeof(cc);
    return GetDefaultCommConfig(szPort, &cc, &dwCCSize);
    }

    void EnumerateSerialPorts(CUIntArray& ports)
    {
    ...
    ports.RemoveAll();
    for (int nPort = 1; nPort <= 255; nPort++)
    if (IsPortAvailable( nPort ))
    {
    ports.Add( nPort );
    }
    }
    GeneralRe: Virtual Ports Pin
    Eldad Kuperman12-Apr-00 23:27
    sussEldad Kuperman12-Apr-00 23:27 
    GeneralRe: Virtual Ports Pin
    pjnaughter16-May-00 14:00
    pjnaughter16-May-00 14:00 
    GeneralRe: Virtual Ports Pin
    pjnaughter17-May-00 3:13
    pjnaughter17-May-00 3:13 
    GeneralRe: Virtual Ports Pin
    2-Sep-01 21:52
    suss2-Sep-01 21:52 
    GeneralRe: Virtual Ports Pin
    Anonymous11-Dec-03 23:38
    Anonymous11-Dec-03 23:38 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.