Available Com Ports List (1 Viewer)

OlcayM

Registered User.
Local time
Today, 18:37
Joined
Oct 30, 2007
Messages
47
Hi All,

I need to know the available com port numbers for a certain project. Just the prot numbers, like seen in device manager screen in system. And naturally I don't want to pay for an activex. Any ideas?

TIA
OlcayM
 
Questions like this fall and out of the "run of the mill" type of MS Access questions, and in the past if I have had a similar problem myself I have usually found something I could use in a VB6 forum.
 
Thanks a lot UncleGizmo,

You are absolutely right. I found the solution. May be it will help someone else also. The answer is API. It is below;

OlcayM

Author:Andrea Tincani

Option Explicit

'API Declarations
Public 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
Public Declare Function CloseHandle Lib "kernel32.dll" (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

'Usage:
Sub Command1_Click()
Dim i As Integer

For i = 1 To 16
If COMAvailable(i) Then
MsgBox "COM" & i & " OK", vbInformation
Else
MsgBox "COM" & i & " Not OK", vbExclamation
End If
Next
End Sub
 

Users who are viewing this thread

Back
Top Bottom