Solved

Get a list of available COM ports

Posted on 1998-11-08
1
1,007 Views
Last Modified: 2013-11-25
How can I get a list of the available serial ports in a computer with MFC?
0
Comment
Question by:emilio
1 Comment
 
LVL 11

Accepted Solution

by:
mikeblas earned 100 total points
ID: 1324360
The right way to do this is actually the simple way. Since MFC has no support for serial programming, you have to go straight to the Win32 API. You need to try to open each port you want to consider. If the port is available, the open will work. If the port is in use, you'll get a sharing error. If the port is not installed, you'll get a file not found error.

You can screw around with enumerating devices from the VCD, but that only gets you the devices installed on the machine--whether they're properly configured or already in use is still information that you can only get by trying to actually open the file.

Use this console app to get started:

// build with
//    cl enum.cpp

#include <windows.h>
#include <stdio.h>

#pragma comment(lib, "kernel32.lib")
#pragma comment(lib, "user32.lib")

void main()
{
   int nPort;
   
   for (nPort = 1; nPort <= 10; nPort++)
   {
      printf("Trying %d...\n", nPort);

      char szPort[10];
      wsprintf(szPort, "COM%d", nPort) ;

      // open COMM device

      HANDLE hPort =
         CreateFile(szPort, GENERIC_READ | GENERIC_WRITE,
                  0,
                  NULL,
                  OPEN_EXISTING,
                  FILE_ATTRIBUTE_NORMAL,
                  NULL);

      if (hPort == INVALID_HANDLE_VALUE)
      {
         DWORD dwLastError = GetLastError();
         printf("\tFailed: ");

         if (dwLastError == ERROR_ACCESS_DENIED)
            printf("Port exists, but is in use!\n");
         else if (dwLastError == ERROR_FILE_NOT_FOUND)
            printf("Port doesn't exist.\n");
         else
            printf("Unrecognized cause: %d\n", dwLastError);
      }
      else
      {
         printf("\tSuccess!\n");
         CloseHandle(hPort);
      }
   }

   return;
}


B ekiM


0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now