|
-
Jul 7th, 2005, 03:12 AM
#1
Thread Starter
Member
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
-
Jul 8th, 2005, 06:18 AM
#2
Re: Find Comm port problem
Try this
VB Code:
Private Declare Function CreateFile _
Lib "kernel32.dll" 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
Private Declare Function CloseHandle _
Lib "kernel32.dll" ( _
ByVal hObject As Long) As Long
Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Private Const FILE_SHARE_READ = &H1
Private Const FILE_SHARE_WRITE = &H2
Private Const OPEN_EXISTING = 3
Private Const FILE_ATTRIBUTE_NORMAL = &H80
Public Function COMAvailable(ByVal iPortNum 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" & iPortNum & "", 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
Private Sub Command1_Click()
Dim i As Long
For i = 1 To 16
If COMAvailable(i) Then
List1.AddItem "COM" & i & " is available"
Else
List1.AddItem "COM" & i & " is not available"
End If
Next
End Sub
-
Jul 11th, 2005, 12:54 AM
#3
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|