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

x
?
Solved

Determin the free com port

Posted on 1998-09-21
1
Medium Priority
?
183 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 150 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

705 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