Solved

Get a list of available COM ports

Posted on 1998-11-08
1
1,010 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Slow process to read Excel 15 110
How to deal with "Consider using Code First Migrations to update the database" ? 7 214
Doc'in system (example?) BA 7 87
Windows Process running 3 47
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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…

912 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

24 Experts available now in Live!

Get 1:1 Help Now