Results 1 to 9 of 9

Thread: How many com port in my computer?

  1. #1
    Guest
    In some computer, it have two com port, but some have only one! How do I know how many com port my computer have in VB Code! it is because I want allow the user to select different com port, so I need to know the max. available com port. Thank!

  2. #2
    Lively Member
    Join Date
    Oct 1999
    Posts
    66
    Here ya go:

    '// API Declarations
    Public Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
    Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

    '// API Structures
    Public Type SECURITY_ATTRIBUTES
    nLength As Long
    lpSecurityDescriptor As Long
    bInheritHandle As Long
    End Type

    '// API constants
    Public Const FILE_SHARE_READ = &H1
    Public Const FILE_SHARE_WRITE = &H2
    Public Const OPEN_EXISTING = 3
    Public Const FILE_ATTRIBUTE_NORMAL = &H80


    '// Return TRUE if the COM exists, FALSE if the COM does not exist
    Public Function COMAvailable(COMNum As Integer) As Boolean
    Dim hCOM As Long
    Dim ret As Long
    Dim sec As SECURITY_ATTRIBUTES

    '// try to open the COM port
    hCOM = CreateFile("COM" & COMNum & "", 0, FILE_SHARE_READ + _
    FILE_SHARE_WRITE, sec, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
    If hCOM = -1 Then
    COMAvailable = False
    Else
    COMAvailable = True
    '// close the COM port
    ret = CloseHandle(hCOM)
    End If
    End Function

    Then, use the code below to go through all the possible COM ports, and find if they are available or not. This code presumes that you have a Combo box named cboComm.

    Private Sub ListComPorts()
    Dim i As Integer

    cboComm.Clear
    For i = 1 To 16
    If COMAvailable(i) Then
    cboComm.AddItem i
    End If
    Next
    cboComm.ListIndex = 0
    End Sub

  3. #3
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    651

    Re: How many com port in my computer?

    Where do I put the
    vb Code:
    1. '// API Declarations
    2. Public Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
    3. Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    4.  
    5. '// API Structures
    6. Public Type SECURITY_ATTRIBUTES
    7. nLength As Long
    8. lpSecurityDescriptor As Long
    9. bInheritHandle As Long
    10. End Type
    11.  
    12. '// API constants
    13. Public Const FILE_SHARE_READ = &H1
    14. Public Const FILE_SHARE_WRITE = &H2
    15. Public Const OPEN_EXISTING = 3
    16. Public Const FILE_ATTRIBUTE_NORMAL = &H80
    17.  
    18.  
    19. '// Return TRUE if the COM exists, FALSE if the COM does not exist
    20. Public Function COMAvailable(COMNum As Integer) As Boolean
    21. Dim hCOM As Long
    22. Dim ret As Long
    23. Dim sec As SECURITY_ATTRIBUTES
    24.  
    25. '// try to open the COM port
    26. hCOM = CreateFile("COM" & COMNum & "", 0, FILE_SHARE_READ + _
    27. FILE_SHARE_WRITE, sec, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
    28. If hCOM = -1 Then
    29. COMAvailable = False
    30. Else
    31. COMAvailable = True
    32. '// close the COM port
    33. ret = CloseHandle(hCOM)
    34. End If
    35. End Function
    I put in a new module, didnt work, I tried it on the child form where the combo box is and that did not work either?
    Thanx

  4. #4
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: How many com port in my computer?

    Just FYI... this thread is 7 year old...

    I tried the code, and it works for me....

    I just tried it like this (with that code in a module):
    vb Code:
    1. Dim K As Long
    2.    
    3.     For K = 1 To 16
    4.         Debug.Print K, COMAvailable(K)
    5.     Next K
    And I got "True" on comm port 1...
    Last edited by CVMichael; Jun 25th, 2007 at 12:31 PM.

  5. #5
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    651

    Re: How many com port in my computer?

    Ok, I put above code in a module called modCom.bas
    then put
    Private Sub ListComPorts()
    Dim i As Integer

    cboComm.Clear
    For i = 1 To 16
    If COMAvailable(i) Then
    cboComm.AddItem i
    End If
    Next
    cboComm.ListIndex = 0
    End Sub
    on my frmConnection (which is a midi child)
    even added a MSCom control to form (didnt know if UI had to or not)
    still didnt work.
    added the snippet you put the so it looked like
    Private Sub ListComPorts()
    Dim i As Integer
    Dim K As Long
    For K = 1 To 16
    Debug.Print K, COMAvailable(K)
    Next K


    cboComm.Clear
    For i = 1 To 16
    If COMAvailable(i) Then
    cboComm.AddItem i
    End If
    still when I run app, there is nothing in the combo box?
    I must be missing something simple here?

  6. #6
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: How many com port in my computer?

    Did you name the combo box "cboComm" ?

  7. #7
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    651

    Re: How many com port in my computer?

    Yes it is
    I have attached my files, It must be something simple I have overlooked.
    Last edited by planethax; Jun 26th, 2007 at 10:48 PM.

  8. #8
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    651

    Re: How many com port in my computer?

    I know tjis thread is very Old, but I thought I would post here rather than start a new one as it is on the same issue.

    I have tried everything I can think of, and yet still can not get this to work, and I have tried on 4 different Pc's

    I have completely started fresh and ree did whole app. but no go.
    I deleted file from prev thread and am posting my latest.
    Please some one look at this and help me out, I know I have just overlooked something simple.
    Last edited by planethax; Jun 27th, 2007 at 08:17 PM.

  9. #9
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    651

    Re: How many com port in my computer?

    Thanx.
    I had to add
    Show
    DoEvents
    ListComPorts
    to the Form_load event

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width