Checking Com Ports

How do you check to see if a comm port is already in use ,and if so what application is using it.
cheers T
Who is Participating?
OTveitConnect With a Mentor Commented:
I use the ScanCom sub to find available com ports on my machine:

'Table of free COM ports
Public nPortFree(0 To MAXPORTS - 1) As Integer

'Make table over available COM Ports
Public Sub ScanCom(ByRef comObj As MSComm)
    Dim i As Integer
    For i = 0 To MAXPORTS - 1
       If (bolOpenSer(comObj, i + 1, 9600, 0, 8, 1)) Then
            nPortFree(i) = True
            comObj.PortOpen = False
            nPortFree(i) = False
        End If
    Next i
End Sub

Public Function bolOpenSer(ByRef comObj As MSComm, _
                        ByVal lngPno As Long, _
                        ByVal lngBaud As Long, _
                        ByVal lngParity As Long, _
                        ByVal lngDataBits As Long, _
                        ByVal lngStopBits As Long) As Boolean
    'Set Error Handling
    On Error GoTo ErrorHandler
    ' Open the serial port
    comObj.CommPort = lngPno
    comObj.Settings = strNumbersToSettings(lngBaud, lngParity,lngDataBits, lngStopBits)
    comObj.PortOpen = True    
    bolOpenSer = True
Exit Function
    bolOpenSer = False
End Function
Hi OTVeit!

Interesting Answer!

I am working on a Comms Routine to Update an Access Database at a Remote Location and this is useful even if I didnt ask the question!!



P.S. If anybody is using Serial Comms - try Greenleaf CommX.OCX - excellent tool!

visualbasicAuthor Commented:
Hello OTveit
could you post the function strNumbersToSettings
Also is there a way of checking the number of ports before you check are they in use.
Regards T
Here is the code for the funtion strNumbersToSettings (it's really simple).

To find the number of physical serial ports on your machine you could read the registry (use GetSetting, GetAllSettings):

' Function: strNumbersToSettings
' Description: Converts Port Settings to String
' Input:    1. Baud Rate
'               2. Parity
'               3. Data Bits
'               4. Stop Bits
' Returns: Port settings as string
Private Function strNumbersToSettings(ByVal lngBaud As Long, _
                                        ByVal lngParity As Long, _
                                        ByVal lngDataBits As Long, _
                                        ByVal lngStopBits As Long) As String
    'Error handling
    On Error GoTo ErrorHandler
    'Set default value
    strNumbersToSettings = "Not defined"
    'Settings Value
    strNumbersToSettings = lngBaud & ","
    strNumbersToSettings = strNumbersToSettings & Switch(lngParity = 0, "N", lngParity = 1, "E", lngParity = 2, "O") & ","
    strNumbersToSettings = strNumbersToSettings & lngDataBits & ","
    strNumbersToSettings = strNumbersToSettings & lngStopBits
Exit Function
    strNumbersToSettings = "Not defined"
End Function

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.