Solved

Get a list of available COM ports

Posted on 1998-11-08
1
1,024 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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…

632 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