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

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


Comments

  • Checking if a COM port Exists

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

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

    Reply
  • 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
    suresh.s

    Reply
  • 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.
    

    Reply
  • Read the Printer Port

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

    Originally posted by: Rohit

    Hi
    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

    Reply
  • 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 http://www.codeguru.com/vb/comments/1743.shtml) 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.

    Reply
  • how to use mscomm

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

    Originally posted by: galih

    dear sir
    I'M VERY HAPPY IF U WOULD LIKE TO HELP ME FOR MY FINAL TASK IN UNIVERSITY. HOW TO CONNECT VISUAL BASIC WITH MICROCONTROLLER BOARD ( MINIMUM SYSTEM BOARD )ATMEL 89C51. I WOULD LIKE TO MAKE A WATER RECHARGE TO THE BOTTLE SIMULATION WITH VISUAL BASIC ,USING THREE MOTOR STEPPER.
    THANK VERY MUCH FOR YOUR HELP. I'M WAITING FOR YOUR E-MAIL.THANX


    GALIH

    Reply
  • saving data from com port

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

    Originally posted by: Arif

    gear sir,
    
    hi..
    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.

    Reply
  • 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
    Loop

    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
    Loop

    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

    Reply
  • 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...

    Reply
  • want To Know about PABX and Com port data Exchage

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

    Originally posted by: rakib

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

    Reply
  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • VMware vCloud® Government Service provided by Carpathia® is an enterprise-class hybrid cloud service that delivers the tried and tested VMware capabilities widely used by government organizations today, with the added security and compliance assurance of FedRAMP authorization. The hybrid cloud is becoming more and more prevalent – in fact, nearly three-fourths of large enterprises expect to have hybrid deployments by 2015, according to a recent Gartner analyst report. Learn about the benefits of …

Most Popular Programming Stories

More for Developers

RSS Feeds