Solved

Determin the free com port

Posted on 1998-09-21
1
176 Views
Last Modified: 2010-04-01
How can i check which com port is free, my program must chech on start which COM port is free(not used by mouse or other device) and display it.
0
Comment
Question by:saidi
[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 3

Accepted Solution

by:
stefanr earned 50 total points
ID: 1173216
In WIN32 you try to open the port with ::CreateFile, like this:

///////////////////////////////////////////////////////////////////////////////
// IsPortVacant - tests if a port is ready to be opened.
///////////////////////////////////////////////////////////////////////////////
BOOL CComPortEx::IsPortVacant(short nComPort)
{
   CString sComPort;
   if (nComPort < 10)
      sComPort.Format(_T("COM%hd"), nComPort);
   else
      sComPort.Format(_T("\\\\.\\COM%hd"), nComPort);

   HANDLE hFile = ::CreateFile(
      sComPort,                                       // Pointer to name of the file.
      GENERIC_READ | GENERIC_WRITE,                   // Access (read-write) mode.
      0,                                              // Share mode.
      NULL,                                           // Pointer to security descriptor.
      OPEN_EXISTING,                                  // How to create.
      FILE_ATTRIBUTE_NORMAL,                          // File attributes.
      NULL                                            // Handle of file with attributes to copy.
      );

   if (hFile != INVALID_HANDLE_VALUE)
   {
      ::CloseHandle(hFile);
      return TRUE;
   }

   return FALSE;
}

0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

751 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