How to Add Available COM Ports to Select From
How to Add Available COM Ports to Select From
How to Add Available COM Ports to Select From
How to Add Available COM Ports to Select From
How to Add Available COM Ports to Select From
How to Add Available COM Ports to Select From How to Add Available COM Ports to Select From How to Add Available COM Ports to Select From How to Add Available COM Ports to Select From How to Add Available COM Ports to Select From How to Add Available COM Ports to Select From How to Add Available COM Ports to Select From How to Add Available COM Ports to Select From
How to Add Available COM Ports to Select From How to Add Available COM Ports to Select From
How to Add Available COM Ports to Select From
Go Back  Xtreme Visual Basic Talk > > > How to Add Available COM Ports to Select From


Reply
 
Thread Tools Display Modes
  #1  
Old 07-21-2008, 02:30 AM
abelchoo abelchoo is offline
Newcomer
 
Join Date: Jun 2008
Posts: 20
Default How to Add Available COM Ports to Select From


Dear all,

I'm working on a project and would like to know how i can add the list of avabilable serial ports to the combobox.

Currently, have done the code such as follows:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Try
Dim iBaud, iDataBits As Integer

Integer.TryParse(BaudRate_TextBox.Text, iBaud)
Integer.TryParse(DataBits_TextBox.Text, iDataBits)

mainform.SerialPort1.BaudRate = iBaud
mainform.SerialPort1.DataBits = iDataBits
mainform.SerialPort1.PortName = ComboBox1.Text

'DataBits_TextBox.Text = mainform.SerialPort1.PortName

If mainform.SerialPort1.IsOpen() = True Then
MsgBox("Please disconnect before changing settings")
Me.Close()
Else
mainform.SerialPort1.PortName = ComboBox1.Text

End If


The same goes for the data bits and the baud rate.

Kindly assist.

Many thanks!
Reply With Quote
  #2  
Old 07-21-2008, 02:53 AM
Qua's Avatar
QuaHow to Add Available COM Ports to Select From Qua is offline
Impetuous & volatile

* Expert *
 
Join Date: Apr 2005
Posts: 2,177
Default

I think you can find a list of available COM-ports somwhere in the registry. Let me check to see if I can found where it was.
__________________
Reading is the foundation for all knowledge - Unknown.
Reply With Quote
  #3  
Old 07-21-2008, 06:50 AM
AtmaWeapon's Avatar
AtmaWeaponHow to Add Available COM Ports to Select From AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

I google searched for this and though there were very few relevant results, I did happen to find that the SerialPort.GetPortNames is probably what you want. It returns an array of strings that represent the serial ports on the current computer.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #4  
Old 07-21-2008, 08:07 PM
abelchoo abelchoo is offline
Newcomer
 
Join Date: Jun 2008
Posts: 20
Default

Hi Atama Weapon!

Thanks very much..

Hmm, i would like to know, how do i display the list of ports available in visual basic?

Have done the following:

Private Sub portform_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ports As String() = mainform.SerialPort1.GetPortNames()
Dim port As String
For Each port In ports
ComboBox1.Text = port
Next
ComboBox1.SelectedText = mainform.SerialPort1.PortName
End Sub

Can't seem to assign the combobox1.text to the list of ports available.

Kindly assist once again

Thanks so much for your help!
Reply With Quote
  #5  
Old 07-21-2008, 09:10 PM
abelchoo abelchoo is offline
Newcomer
 
Join Date: Jun 2008
Posts: 20
Default

Dear all,

I've just found how to add the range of ports available. Many thanks for your help.
Here is the code:

'Add Range of Ports Available from Registry
Dim ports As String() = mainform.SerialPort1.GetPortNames()
ComboBox1.Items.AddRange(ports)
ComboBox1.SelectedText = mainform.SerialPort1.PortName
ComboBox1.Sorted = True
Reply With Quote
  #6  
Old 08-01-2008, 09:01 AM
gpraceman's Avatar
gpracemanHow to Add Available COM Ports to Select From gpraceman is offline
Contributor

* Expert *
 
Join Date: Sep 2002
Location: Highlands Ranch, CO
Posts: 560
Default

Also, I don't know if this really applies to what you are doing, but if there are bluetooth ports on your computer, it is something to be aware of. Extra characters can be returned at the end of the port name and if you then try to set the serial port with it, you will get an exception.

See https://connect.microsoft.com/Visual...dbackID=236183

I filter out these extra characters like this:

Code:
sPortName = System.Text.RegularExpressions.Regex.Match(sPortName, "COM\d+").Value().Trim()
Now, if the extra junk at the end is numerical (some people were getting things like COM70, instead of COM7) this won't help.
__________________
Awana Grand Prix and Pinewood Derby racing - Where a child, an adult and a small block of wood combine for a lot of fun and memories.

Last edited by gpraceman; 08-01-2008 at 09:07 AM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
How to Add Available COM Ports to Select From
How to Add Available COM Ports to Select From
How to Add Available COM Ports to Select From How to Add Available COM Ports to Select From
How to Add Available COM Ports to Select From
How to Add Available COM Ports to Select From
How to Add Available COM Ports to Select From How to Add Available COM Ports to Select From How to Add Available COM Ports to Select From How to Add Available COM Ports to Select From How to Add Available COM Ports to Select From How to Add Available COM Ports to Select From How to Add Available COM Ports to Select From
How to Add Available COM Ports to Select From
How to Add Available COM Ports to Select From
 
How to Add Available COM Ports to Select From
How to Add Available COM Ports to Select From
 
-->