Solved

Determin the free com port

Posted on 1998-09-21
1
172 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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

763 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now