Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Test for available com port

Posted on 2006-05-04
4
Medium Priority
?
189 Views
Last Modified: 2012-05-05
I have an app that gathers data set through the serial port, I need to be able to check and see if the com port is open alread and if it is, move to the next com port. How would I go about this?
0
Comment
Question by:astrohelp
  • 2
2 Comments
 
LVL 6

Expert Comment

by:PhilAI
ID: 16606399
Add the component: Microsoft Comm Control 6.0

Private Function CommPort_FindAvail(ByVal iStartAt As Integer, ParamArray vExceptions()) As Integer

Dim C As Integer
Dim E As Integer
Dim bIncrement As Boolean

On Error Resume Next

' Set the CommPort number to start searching upwards from
C = iStartAt - 1

' Move onto the next CommPort number
Do
    C = C + 1
    bIncrement = False
    If C > 100 Then
        If iStartAt <> 1 Then C = IIf(iStartAt > 1, 1, 0)
    ElseIf UBound(vExceptions) > -1 Then
        For E = LBound(vExceptions) To UBound(vExceptions)
            If vExceptions(E) = C Then
                bIncrement = True
            End If
        Next E
    End If
Loop Until Not bIncrement

' Loop thru all possible CommPorts
Do While C > 0
    ' Ensure the Port is closed
    MSComm1.PortOpen = False
    Call Err.Clear ' Clear any errors in case the Port was NOT open
    ' Set the next CommPort number
    MSComm1.CommPort = C
    ' Try and open the CommPort
    MSComm1.PortOpen = True
    ' If it does NOT error, then return the valid CommPort
    If Err = 0 Then
        ' N.B. CommPort is still open, so you may want "MSComm1.PortOpen = False"
        CommPort_FindAvail = C
        Exit Function
    Else ' Display the last error and then clear it
        Debug.Print "Error " & Err.Number & ": " & Err.Description
        Call Err.Clear
    End If
    ' Move onto the next CommPort number
    Do
        C = C + 1
        bIncrement = False
        If C > 100 Then
            If iStartAt <> 1 Then C = IIf(iStartAt > 1, 1, 0)
        ElseIf UBound(vExceptions) > -1 Then
            For E = LBound(vExceptions) To UBound(vExceptions)
                If vExceptions(E) = C Then
                    bIncrement = True
                End If
            Next E
        End If
    Loop Until Not bIncrement
Loop

End Function

Use by calling CommPort_FindAvail(1)

This will start at CommPort one, and the next (includes the Port number passed) valid CommPort will be returned
0
 
LVL 6

Accepted Solution

by:
PhilAI earned 1000 total points
ID: 16606413
This also allows exceptions, so that you can not include Ports you know are in use...

e.g. CommPort_FindAvail(1, 1, 5)

This will find the next valid CommPort number that is NOT one or five
0

Featured Post

Independent Software Vendors: 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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month11 days, 8 hours left to enroll

564 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