Checking if a COM port Exists | CodeGuru

Checking if a COM port Exists

In many programs, there is an option for selecting the serial port. Instead of providing selection for all possible ports and building an error handling routine for those non-existed in hardware, here is an easy way to enumerate the serial ports of a computer, using Visual Basic 5 or higher (I am not sure about […]

Written By
CodeGuru Staff
CodeGuru Staff
Feb 5, 2004
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

In many programs, there is an option for selecting the serial port. Instead of providing selection for all possible ports and building an error handling routine for those non-existed in hardware, here is an easy way to enumerate the serial ports of a computer, using Visual Basic 5 or higher (I am not sure about the older versions). It works on both Win95-98 and Win NT 4.

option Explicit
'*************************************************************
' Module         : EnumPorts
' FileName       : EnumPorts.BAS
' Author         : Dimitrios Papadopoulos
' date Created   : 10/08/00 22:07:10
'
' Copyright      : 2000, Dimitrios Papadopoulos.
'                  All Rights Reserved.
'
' Description    : Enumerates Existance of COM Ports
'
' Change History :
' 1.0       10 August 2000
'           Dimitrios Papadopoulos
'           Initial Version
'
'*************************************************************
Type DCB
    DCBlength as Long
    BaudRate as Long
    fBitFields as Long
    wReserved as Integer
    XonLim as Integer
    XoffLim as Integer
    ByteSize as Byte
    Parity as Byte
    StopBits as Byte
    XonChar as Byte
    XoffChar as Byte
    ErrorChar as Byte
    EofChar as Byte
    EvtChar as Byte
    wReserved1 as Integer
End Type

Type COMMCONFIG
    dwSize as Long
    wVersion as Integer
    wReserved as Integer
    dcbx as DCB
    dwProviderSubType as Long
    dwProviderOffset as Long
    dwProviderSize as Long
    wcProviderData as Byte
End Type
'
Declare Function GetDefaultCommConfig Lib "kernel32" _
    Alias "GetDefaultCommConfigA" (byval lpszName as string, _
             lpCC as COMMCONFIG, lpdwSize as Long) as Long
'
public Function EnumSerPorts(port as Integer) as Long
    'this function returns non-zero value if the port exists
    Dim cc as COMMCONFIG, ccsize as Long
'
    ccsize = LenB(cc)     'gets the size of COMMCONFIG structure
'
    EnumSerPorts = GetDefaultCommConfig("COM" + Trim(Str(port)) + _
                                        Chr(0), cc, ccsize)
'
End Function
CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.