Solved

Determin the free com port

Posted on 1998-09-21
1
173 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
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
IdTelnet1DataAvailable handler freezes the Application 4 75
getting a fire fox page (view page source ) in c++ 6 42
c++ syntax question 9 49
FMX TCameraComponent Problem 2 71
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

821 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