Results 1 to 3 of 3

Thread: Find Comm port problem

  1. #1

    Thread Starter
    Member
    Join Date
    May 2005
    Posts
    59

    Question Find Comm port problem

    hi.
    I have a device connected via comm - usb adaptor to computer.
    (comm in device and usb in computer)

    When i connect the device, Windows recognizes it as COM6.(it may be any other port name, but for examples i'll take com6)

    Then i run my code for finding the device by going from com1 to com16, trying to open current port , in case it opens i send "hello" to the device and wait some time for the response, say "hello, i'm ..".

    The code works fine and finds the device port when it can open the port (COM6).
    BUT
    There are cases when other application takes COM6, even if in physical usb(comm) exit no device is connected. Then when i connect my device, Windows again recognizes it as COM6, and in the code i fail to open it, since it in use.

    SO, what relationship is between the pysical usb (comm adaptor) and logical (1 - 256) ports?
    When pysical comm exit gets it's name (COM6 and not 7 or 180)?
    How can i find free logical comm name and "connect" / relate it to the physical exit?
    (The fact that i can open the port doesn't define that this port is free to use, there is a case that other devise connected via this port, but right on this time it didn't took it.)
    Where can i find any information on this ?

    Thanks a lot,
    anastacia

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Find Comm port problem

    Try this
    VB Code:
    1. Private Declare Function CreateFile _
    2.  Lib "kernel32.dll" Alias "CreateFileA" ( _
    3.  ByVal lpFileName As String, _
    4.  ByVal dwDesiredAccess As Long, _
    5.  ByVal dwShareMode As Long, _
    6.  lpSecurityAttributes As SECURITY_ATTRIBUTES, _
    7.  ByVal dwCreationDisposition As Long, _
    8.  ByVal dwFlagsAndAttributes As Long, _
    9.  ByVal hTemplateFile As Long) As Long
    10.  
    11. Private Declare Function CloseHandle _
    12.  Lib "kernel32.dll" ( _
    13.  ByVal hObject As Long) As Long
    14.  
    15. Private Type SECURITY_ATTRIBUTES
    16.     nLength As Long
    17.     lpSecurityDescriptor As Long
    18.     bInheritHandle As Long
    19. End Type
    20.  
    21. Private Const FILE_SHARE_READ = &H1
    22. Private Const FILE_SHARE_WRITE = &H2
    23. Private Const OPEN_EXISTING = 3
    24. Private Const FILE_ATTRIBUTE_NORMAL = &H80
    25.  
    26. Public Function COMAvailable(ByVal iPortNum As Integer) As Boolean
    27.     Dim hCOM As Long
    28.     Dim ret As Long
    29.     Dim sec As SECURITY_ATTRIBUTES
    30.  
    31.     'try to open the COM port
    32.     hCOM = CreateFile("COM" & iPortNum & "", 0, FILE_SHARE_READ + FILE_SHARE_WRITE, _
    33.      sec, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
    34.     If hCOM = -1 Then
    35.         COMAvailable = False
    36.     Else
    37.         COMAvailable = True
    38.         'close the COM port
    39.         ret = CloseHandle(hCOM)
    40.     End If
    41. End Function
    42.  
    43. Private Sub Command1_Click()
    44. Dim i As Long
    45. For i = 1 To 16
    46.     If COMAvailable(i) Then
    47.         List1.AddItem "COM" & i & " is available"
    48.     Else
    49.         List1.AddItem "COM" & i & " is not available"
    50.     End If
    51. Next
    52. End Sub

  3. #3

    Thread Starter
    Member
    Join Date
    May 2005
    Posts
    59

    Re: Find Comm port problem

    Thanks, but it doesn't solve my problem.
    I can find available ports, but the problem is that my code, so as yours, can only define is the port currently open for another program, and it's not enough.

    Say, some device X has physical connection to comm port1, but currently it doesn't open the comm1. I run the code it returns that comm1 is available, i take it, then the device tries to open the port and fails.

    The question is what relationship is between physical exits and logical 256 ports !!

    Say, i found that comm12 is available, and physical exit is related to com6 (by windows) , how can i programmically rename/rerelate the physical exit be comm12??

    if i rename the physical exit ,Is it'll make other programms to crash (like in exampe i gave above ,device X) ?

    The final destination is :
    1. to find out what logical comm port is my device related to.
    2. to find available logical comm port.
    3. to relate the physical exit with available logical comm port.


    Thanks,
    anastacia
    Last edited by nastya; Jul 11th, 2005 at 01:04 AM.

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