|
-
Jun 27th, 2000, 03:00 PM
#1
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!
-
Jun 29th, 2000, 08:17 AM
#2
Lively Member
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
-
Jun 25th, 2007, 01:30 AM
#3
Fanatic Member
Re: How many com port in my computer?
Where do I put the
vb Code:
'// 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
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
-
Jun 25th, 2007, 07:40 AM
#4
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:
Dim K As Long
For K = 1 To 16
Debug.Print K, COMAvailable(K)
Next K
And I got "True" on comm port 1...
Last edited by CVMichael; Jun 25th, 2007 at 12:31 PM.
-
Jun 25th, 2007, 05:35 PM
#5
Fanatic Member
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?
-
Jun 25th, 2007, 06:16 PM
#6
Re: How many com port in my computer?
Did you name the combo box "cboComm" ?
-
Jun 25th, 2007, 11:39 PM
#7
Fanatic Member
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.
-
Jun 26th, 2007, 10:53 PM
#8
Fanatic Member
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.
-
Jun 27th, 2007, 08:18 PM
#9
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|