Solved

How to list out available Comm. Port ?

Posted on 2001-06-18
7
3,185 Views
Last Modified: 2008-02-01
I'm using MSCOMM component when writing serial comm. program. But how to list out available comm. port using VB6.
0
Comment
Question by:azsoft
7 Comments
 
LVL 5

Expert Comment

by:gbaren
ID: 6203152
The code below will add a list of valid COM ports to a list box.


Private Sub Form_Load()
Dim i As Integer

    On Error Resume Next
    For i = 1 To 8
        MSComm1.CommPort = i
        MSComm1.PortOpen = True
        If Not (Err.Number = 8002) Then     'Msg 8002 = invalid port number
            List1.AddItem "COM" & CStr(i)
        End If
        MSComm1.PortOpen = False
    Next
   
End Sub
0
 
LVL 3

Expert Comment

by:Joebob
ID: 6203571
In Windows NT and I think 9x, the following registry entry shows all serial and parallel ports installed on the PC.

HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM
0
 
LVL 43

Accepted Solution

by:
TimCottee earned 50 total points
ID: 6205600
azsoft, this example will list all ports available on a machine, including the comm ports and any printer ports (also logical ports such as network printer connections).

Private Type PORT_INFO_2
    pPortName As String
    pMonitorName As String
    pDescription As String
    fPortType As Long
    Reserved As Long
End Type
Private Type API_PORT_INFO_2
    pPortName As Long
    pMonitorName As Long
    pDescription As Long
    fPortType As Long
    Reserved As Long
End Type
Private Declare Function EnumPorts Lib "winspool.drv" Alias "EnumPortsA" (ByVal pName As String, ByVal Level As Long, ByVal lpbPorts As Long, ByVal cbBuf As Long, pcbNeeded As Long, pcReturned As Long) As Long
Private Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Long) As Long
Private Declare Sub CopyMem Lib "kernel32" Alias "RtlMoveMemory" (pTo As Any, uFrom As Any, ByVal lSize As Long)
Private Declare Function HeapAlloc Lib "kernel32" (ByVal hHeap As Long, ByVal dwFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function GetProcessHeap Lib "kernel32" () As Long
Private Declare Function HeapFree Lib "kernel32" (ByVal hHeap As Long, ByVal dwFlags As Long, lpMem As Any) As Long
Dim Ports(0 To 100) As PORT_INFO_2
Public Function TrimStr(strName As String) As String
    'Finds a null then trims the string
    Dim x As Integer
    x = InStr(strName, vbNullChar)
    If x > 0 Then TrimStr = Left(strName, x - 1) Else TrimStr = strName
End Function
Public Function LPSTRtoSTRING(ByVal lngPointer As Long) As String
    Dim lngLength As Long
    'Get number of characters in string
    lngLength = lstrlenW(lngPointer) * 2
    'Initialize string so we have something to copy the string into
    LPSTRtoSTRING = String(lngLength, 0)
    'Copy the string
    CopyMem ByVal StrPtr(LPSTRtoSTRING), ByVal lngPointer, lngLength
    'Convert to Unicode
    LPSTRtoSTRING = TrimStr(StrConv(LPSTRtoSTRING, vbUnicode))
End Function
'Use ServerName to specify the name of a Remote Workstation i.e. "//WIN95WKST"
'or leave it blank "" to get the ports of the local Machine
Public Function GetAvailablePorts(ServerName As String) As Long
    Dim ret As Long
    Dim PortsStruct(0 To 100) As API_PORT_INFO_2
    Dim pcbNeeded As Long
    Dim pcReturned As Long
    Dim TempBuff As Long
    Dim i As Integer
    'Get the amount of bytes needed to contain the data returned by the API call
    ret = EnumPorts(ServerName, 2, TempBuff, 0, pcbNeeded, pcReturned)
    'Allocate the Buffer
    TempBuff = HeapAlloc(GetProcessHeap(), 0, pcbNeeded)
    ret = EnumPorts(ServerName, 2, TempBuff, pcbNeeded, pcbNeeded, pcReturned)
    If ret Then
        'Convert the returned String Pointer Values to VB String Type
        CopyMem PortsStruct(0), ByVal TempBuff, pcbNeeded
        For i = 0 To pcReturned - 1
            Ports(i).pDescription = LPSTRtoSTRING(PortsStruct(i).pDescription)
            Ports(i).pPortName = LPSTRtoSTRING(PortsStruct(i).pPortName)
            Ports(i).pMonitorName = LPSTRtoSTRING(PortsStruct(i).pMonitorName)
            Ports(i).fPortType = PortsStruct(i).fPortType
        Next
    End If
    GetAvailablePorts = pcReturned
    'Free the Heap Space allocated for the Buffer
    If TempBuff Then HeapFree GetProcessHeap(), 0, TempBuff
End Function
Private Sub Form_Load()
    'KPD-Team 2000
    'URL: http://www.allapi.net/
    'E-Mail: KPDTeam@Allapi.net
    Dim NumPorts As Long
    Dim i As Integer
    'Get the Numbers of Ports in the System
    'and Fill the Ports Structure
    NumPorts = GetAvailablePorts("")
    'Show the available Ports
    Me.AutoRedraw = True
    For i = 0 To NumPorts - 1
        Me.Print Ports(i).pPortName
    Next
End Sub

0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:azsoft
ID: 6207178
TimCottee, error on form_load: Me.Print ports(i).pPortName
0
 

Expert Comment

by:ulischmidt
ID: 6211359
'Add the following to your Module

'DECLARATIONS:
Private Const GENERIC_READ = &H80000000
Private Const GENERIC_WRITE = &H40000000
Private Const OPEN_EXISTING = 3
Private Const INVALID_HANDLE_VALUE = -1

Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

'IMPLEMENTATION:
Public Function CheckComPort(PortNo) As Boolean
Dim Handl&           'Filehandle
Dim PortString$

  PortString = "COM" + Format$(PortNo)
 
  'Try to open the port
  Handl = CreateFile(PortString, GENERIC_READ Or GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0)
   
  'Handle ok?
  If Handl = INVALID_HANDLE_VALUE Then
    CheckComPort = False
  Else
    CloseHandle Handl
    CheckComPort = True
  End If

End Function

'Call the function
Public Sub CheckAllComs()
Dim i&, PortNo&
Dim MaxPorts&

  MaxPorts = 8 'up to 256 in NT
 
  For i = 1 To MaxPorts
    If CheckComPort(PortNo) Then
      'DO SOMETHING USEFULL...
    End If
  Next
End Sub
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 6213404
ulischmidt, you are new to EE so can be excused (once) the misuse of the answer option. Please read the guidelines on comments vs answers at the bottom of the page and do NOT post answers in this way in future. If you continue to do so your account will be referred to customer services for possible penalty.

azsoft, it seems we have come to another "jump" for points by a new "expert" please this time reject the "answer" and either close out the thread by accepting the most appropriate answer or by requesting more information.
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 6248949
azsoft, please return to this thread and deal with it as appropriate.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem to skip loop 6 56
Prevent user closing word document opened with VB6 6 73
Problem to With line 4 56
Publisher:   Unknown     VB.exe Application 1 17
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…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

813 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now