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 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

    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


  • Checking if a COM port Exists

    Posted by Rowlama1 on 07/14/2005 06:08pm

    Excellent code -- just what I needed for a project!

  • doubt about creating a classmodule

    Posted by Legacy on 01/11/2004 12:00am

    Originally posted by: suresh

    dear sir/madam
    I like to know about how connect a database using class module with VB.Kindly clear my doubt

  • Read OutBuffer or InBuffer that other Program is using

    Posted by Legacy on 01/11/2004 12:00am

    Originally posted by: Duong Van Tuan

    I truly thank you if you help me. I want to write a Online VB Code that can extract some characters waiting in the transmit buffer from other program. Ex: If hyperterminal program seizer Com port and transmit data, my VB code can read any characters in Buffer that hyperterminal transmit.

  • Read the Printer Port

    Posted by Legacy on 12/28/2003 12:00am

    Originally posted by: Rohit

    I have a query that i want to calculate the no of pages that i have printer in a day, ie if i printed 5 DOC pages and 5 text pages then my code should display 10 printouts in a day and the other query is that i want to capture the the no of documents scanned in a day, it would be a great help for me

    Thank You

  • People please don't ask generic questions

    Posted by Legacy on 11/18/2003 12:00am

    Originally posted by: Dimitrios Papadopoulos

    After posting that original source snippet about COM port enumeration, quite a few people were asking me on how to access the COM ports. I soon realized that what they wanted was a way to start with serial port programming. So I provided a half-working example (see for serial port acess in character mode. This is all the help I could offer on questions like "I don't know how to communicate with serial ports, please help". My available time is too limited for answering private questions or general guides to VB programming.

    After reading the link above, one should be able to adapt the offered code to his needs. Of course, some elementary skills in VB commands, controls and forms are required.

  • how to use mscomm

    Posted by Legacy on 11/15/2003 12:00am

    Originally posted by: galih

    dear sir


  • saving data from com port

    Posted by Legacy on 10/25/2003 12:00am

    Originally posted by: Arif

    gear sir,
    i'm arif.i want to know how to read data from com port and save it to the file.please sent me the code, if you have it.
    thank you.

  • about accessing serial ports with VB

    Posted by Legacy on 09/22/2003 12:00am

    Originally posted by: Dimitrios Papadopoulos

    Very often, people ask me how to access serial ports in VB. There are some useful examples in MSDN library cd but one might have to find his way through the complexities of the serial port interface. Novices coming from Quick Basic face quite a few difficulties because acessing the port isn't as straightforward as it was in Quick Basic. First of all, you must put a MSCOMM control in your main form (or in the form where com ports are accessed). Then, you open the port. After that, accessing is similar to that in QB, having in mind that MSCOMM is an object with properties that can be set on either program design on run-time. So, when you want to sent something, instead of assigning values to a numbers that represent a port (e.g. #1), you assign the value to a MSCOMM property. E.g.:
    MSComm1.Output = "Hello"

    Handling (sending and receiving) characters is much easier than handling bytes (which requires some awful array handling, it's a shame that MS kept that in VB too).

    Receiving with MSCOMM is similar to QB. There are two methods: event driven and polling. In the event driven method, you define a handling routine inside the form where MSCOMM control exists (similar to the way ON COMM GOTO was working in QB). See source examples in MSDN for the naming In the polling method, you simply read the port until a character appears.

    Here is a simplified example (not complete and ready for usage), where I assume the MSCOMM control is named MSComm1. Handshake method has been left to default (hardware handshake, I think) which means that one must provide the necessary hardware connections:

    Private Sub Form_Load()
    MSComm1.CommPort = 1
    MSComm1.Settings = "19200,n,8,1"
    MSComm1.PortOpen = True
    MSComm1.InputLen = 0
    End Sub
    Private Sub SerialOut(chrSerOut As string)
    'Call this routine when you want to sent chars to the port
    'provide necessary error handling here
    MSComm1.Output = chrSerOut
    End Sub
    Private ThrashInput()
    'It empties the com input buffer. Use it if needed
    Dim chrTrash
    MSComm1.InputLen = 0
    chrTrash = MSComm1.Input
    End Sub
    Private Function SerialInPolling()
    ' It returns the characters in the input buffer
    ' (using the polling method)
    Dim Timer1
    Timer1 = Timer
    Do While (Timer1 - Timer) > 0 And MSComm1.InBufferCount = 0
    DoEvents 'For preventing system freeze

    If MSComm1.InBufferCount = 0 Then
    MsgBox = "COM receive timeout"
    End If

    SerialIn = MSComm1.Input
    End Function
    Private On_Comm()
    ' It must be used when event driven method is needed
    ' for reading the receiving characters from the buffer
    Dim Timer1, SerialInBuff
    Timer1 = Timer
    Do While (Timer1 - Timer) > 0 And MSComm1.InBufferCount = 0
    SerialInBuff = MSComm1.Input
    DoEvents 'For preventing system freeze

    End Sub

    In case one needs to access the MSCOMM control from another module, it must be included in the subroutine arguments. In that case, the above SendSerialOut routine becomes as following:

    Public SendSerialOut(MSComm1 as MSComm, chrSerOut As string)
    'Call this routine when you want to sent chars to the port
    MSComm1.Output = chrSerOut
    End Sub

  • disable com port with vb

    Posted by Legacy on 09/20/2003 12:00am

    Originally posted by: ashraf

    hello to everybody,
    here i need help urgently.does any one know to disable physical comm port by using visual basic?may be someone can guide me by giving sample code.before this i just disable physical comm port manually in device manager.i hope my comment will be hear...

  • want To Know about PABX and Com port data Exchage

    Posted by Legacy on 12/09/2002 12:00am

    Originally posted by: rakib

    i am rakib.
    i have a problem.
    I want to make a software that will work with
    com and PABX system.
    I have used the ocx that needs for the communication .
    But still i have some problem.My data mak 6 or 7% losted
    every time.

    So any one please help me within short time.
    I will be greatefull to him.
    ( rakib )

  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: February 12, 2015 The evolution of systems engineering with the SysML modeling language has resulted in improved requirements specification, better architectural definition, and better hand-off to downstream engineering. Agile methods have proven successful in the software domain, but how can these methods be applied to systems engineering? Check out this webcast and join Bruce Powel Douglass, author of Real-Time Agility, as he discusses how agile methods have had a tremendous …

  • With the average cost of a data breach reaching $3.5 million, the pressure is intense to maintain the security and compliance of your data center. It only takes one breach and the cost to your organization can spiral out of control. In this white paper, you will discover how your organization may be at risk and how you can protect your data center from costly and highly visible breaches. Through the Intelligent Compliance process, you will learn how you can: Automate the discovery process for your …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date