Solved

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

Posted on 2004-10-15
3
355 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maven disable workspace resolution 1 77
backup program with robocopy 6 91
Adding a countdown to HTA 12 114
C++ statement T∗ begin(Vector<T>& x) 5 43
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
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.

739 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