Solved

How can I check for a free COM port during an installation.

Posted on 2004-10-15
3
329 Views
Last Modified: 2013-11-25
I have a project that currently writes a regkey to use COM Port 1 for an external item.  Is there a way to determine if a COM Port is in use and to set the registry to use the next free port?

Thanks!
0
Comment
Question by:dangerwill
  • 2
3 Comments
 
LVL 2

Accepted Solution

by:
tdyck12345 earned 500 total points
ID: 12321971
Create a console application with the following code, and run the application during the install

<DllImport("kernel32.dll", SetlastError:=True)> Private Shared Function CreateFile(<MarshalAs(UnmanagedType.LPStr)> ByVal lpFileName As String, ByVal dwDesiredAccess As Integer, ByVal dwShareMode As Integer, ByVal lpSecurityAttributes As Integer, ByVal dwCreationDisposition As Integer, ByVal dwFlagsAndAttributes As Integer, ByVal hTemplateFile As Integer) As Integer
End Function

<DllImport("kernel32.dll", SetlastError:=True)> Private Shared Function CloseHandle(ByVal hObject As Integer) As Integer
End Function

Private Const GENERIC_READ As Integer = &H80000000
Private Const GENERIC_WRITE As Integer = &H40000000
Private Const FILE_OPEN_EXISTING As Integer = 3
Private Const FILE_FLAG_OVERLAPPED As Integer = &H40000000

Public Sub Main()
      for count as integer = 0 to 10
            if COMAvailable(count) then
                  ' write the value to the registry
            end if
      next
End Sub

Public Function COMAvailable(ByVal COMNum As Integer) As Boolean
        Dim hCOM As Long
        Dim ret As Long

        'try to open the COM port
        hCOM = CreateFile("\\.\COM" & COMNum, GENERIC_READ Or GENERIC_WRITE, 0, 0, FILE_OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0)
        If hCOM = -1 Then
            Return False
        Else
            'close the COM port
            ret = CloseHandle(hCOM)
            Return True
        End If
End Function


Troy
0
 

Author Comment

by:dangerwill
ID: 12321981
Thanks a bunch.  I'll try this out and let you know if it works.
0
 

Author Comment

by:dangerwill
ID: 12324050
Worked like a charm!  Thank you sooo much!
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

This article will show, step by step, how to integrate R code into a R Sweave document
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

770 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