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