Solved

Get a list of available COM ports

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…

820 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