Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to select a comport from a list of ports using combobox using vb6.i'm unable to write a code on that so pls help me.

What I have tried:

VB
Private Sub form_load()

    Dim i As Long
    For i = 1 To 16
        If COMAvailable(i) Then
            Combo1.AddItem "COM" & i & ""
        Else
            Combo1.AddItem "COM" & i & ""
        End If
    Next
    
End Sub

Private Sub Command1_Click()
    
    If COMAvailable(i) = True Then
        
        Label1.Caption = "READY FOR COMMUNICATION"
    Else
        Label1.Caption = "PORT NOT OPEN PLEASE CONFIGURE"
    End If
    
End Sub
Posted
Updated 8-Dec-17 0:27am
v2
Comments
Richard MacCutchan 8-Dec-17 5:45am    
Where is the code for the COMAvailable method?
Member 13396059 8-Dec-17 5:50am    
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
Richard MacCutchan 8-Dec-17 5:57am    
OK, so are you going to tell us what the problem is?
Member 13396059 8-Dec-17 6:03am    
i am askin for help
Richard MacCutchan 8-Dec-17 6:07am    
Then tell us what the problem is. We cannot read your mind.

1 solution

If you really need to do it with VB6, search the web for solutions using something like "vb6 enumerate serial ports" as search term.

Don't try to do it yourself. There are a lot of things to observe like naming of ports with numbers above 9 and that CreateFile fails not only for not existing ports but also for existing ports that are actually opened (in use).

The only reliable method besides the .NET SerialPort.GetPortNames method is using the Windows Setup API. But I don't know if someone has used that with VB6.

[EDIT: Notes on code posted as comment]
Quote:
every time when i'm running the program when i'm selecting the com port which is available also its showing that port not open pls configure
That is because this always fails due to the wrong share mode:
VB
hCOM = CreateFile("COM" & iPortNum & "", 0, FILE_SHARE_READ + FILE_SHARE_WRITE, _
sec, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
For communications resources, the dwCreationDisposition parameter must be OPEN_EXISTING, the dwShareMode parameter must be zero (exclusive access), and the hTemplateFile parameter must be NULL. Read, write, or read/write access can be specified, and the handle can be opened for overlapped I/O.

To specify a COM port number greater than 9, use the following syntax: "\\.\COM10". This syntax works for all port numbers and hardware that allows COM port numbers to be specified.
So it should be:
VB
hCOM = CreateFile("\\.\COM" & iPortNum, GENERIC_READ + GENERIC_WRITE, 0, _
ByVal 0&, OPEN_EXISTING, 0, 0&)
[/EDIT]
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900