Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 188
  • Last Modified:

Determin the free com port

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
saidi
Asked:
saidi
1 Solution
 
stefanrCommented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now