Solved

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

Posted on 2004-10-15
3
341 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A short article about a problem I had getting the GPS LocationListener working.
Monitor input from a computer is usually nothing special.  In this instance it prevented anyone from using the computer.  This was a preconfiguration that didn't work.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

820 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