Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Get a list of available COM ports

Posted on 1998-11-08
1
Medium Priority
?
1,033 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 400 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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

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…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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…

722 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