How to program SerialComPort Number (PortName) by using ComboBox1?
Hi, I'm using Visual Basic 2005. Try ' Open the serial port SerialPort1.Open() ' Set the event handler for data reception AddHandler SerialPort1.DataReceived,AddressOf SerialPort_DataReceived Catch exAs System.Exception MessageBox.Show(ex.Message) EndTry Label2.Text = SerialPort1.PortName
Please help me: How toselect ComPort No. by using ComboBox1? Thanks Regards Wanxi |
[3091 byte] By [
Wanxi] at [2008-1-10]
Hmm. The small sample program in the link I gave you previously does exactly that so maybe it is anyway a good idea to study it a little more carefully?
Private Sub MaxiTesterLoad(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
For Each COMString As String In My.Computer.Ports.SerialPortNames ' Load all available COM ports.
COMPortsBox.Items.Add(COMString)
Next
COMPortsBox.Sorted = True
.....
If you have forgotten the link, here it is: http://www.innovatic.dk/knowledg/SerialCOM/SerialCOM.htm
The Try statement in your post just tries to open the port. If this is not possible (tested in Catch statement) the exception is displayed in a messageBox. As I have said previously, I will recommend you to get the very basic knowledge about VB from your book. It is very difficult to help you when we have to start from the very beginning each time.
Hi Carsten,
Sorry that i need to ask some question:
Ist the program above is going to load the available ComPort into ComboBox?
So my ComboBox will not need something like below ist?
ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
Dim
ComPorts() As
ComPort = {New
ComPort("Com1"
), New
ComPort("Com2"
), New
ComPort("Com3"
), New
ComPort("Com4"
), New
ComPort("Com5"
), New
ComPort("Com6"
), New
ComPort("Com7"
), New
ComPort("Com8"
), New
ComPort("Com9"
), New
ComPort("Com10"
), New
ComPort("Com11"
), New
ComPort("Com12"
)}ComboBox1.DataSource = ComPorts
ComboBox1.DisplayMember =
"longname"
Public Class ComPort
Private _longName As String '_shortName,
Public
Sub
New
(ByVal
longName As
String
) ', ByVal shortName As String)
Me
._longName = longName'Me._shortName = shortName
End
Sub
'Public ReadOnly Property ShortName() As String
'Get
'Return Me._shortName
'End Get
'End Property
Public
ReadOnly
Property
longName() As
String
Get
Return
Me
._longNameEnd
Get
End
Property
End Class
If i just want to hardcode my ComboBox and take the value that selected and treat it as the SerialPort's PortName..
Is this will be easier? Because the ComPort only 1 to 12 right?
Although it will be nicer if i program it as auto update ComPort.
By the way, Q: in my program, what is the use of long name and short name?
what is the function of doing that? Ist shortname neccessary?
Thank you.
It is not a good idea to specify the COM ports the way you do. On a normal computer, you may only have COM1 and COM2 if you have not added extra serial cards, so if the user selects e.g. COM6, (s)he will get an error. My program only takes the actually available COM ports, so the user cannot select a port, which does not excist.
How can you write code and then ask me, what your program is doing? It seems that you really need some basic knowledge about VB and your program looks indeed very strange to me! What do you want to achive?
Sorry about that..
I am a college student, study Electronic Computer course
I study C programming, C# and Mobile computing before.. (each i study for half year only)
We only learn some programming stuff and do some application.
Purpose is just for us to know the basic.. eg. the tools, some application programming
And i now doing my Final Year Project which need me to do Visual Basic 2005
That's all..
Good luck with your project. If you need further advise - just ask, but please study any previous answer carefully before you do. The solution with the ComboBox was right in front of your eyes in the link I gave you, but you didn't bother to read it. I have spend several weeks writing my description. If you would just use 1/10 of this time trying to understand it, it could bring you very far and give you a knowledge even some MVP's with many stars on their shoulders don't have.
I think that the basic problem is that you try to move forward too fast. If you don't understand something within 30 seconds, you just move on, but you will not learn anything from that. Take it easy step by step! Programming is like building a house. You start with the basement. If you don't make it strong, the whole building may collapse. I use at least half of my programming time trying to dig down below the surface of Windows and try to find out what goes on below the hood, because this knowledge is the key to reliable programming. For example, it is absolutely impossible to decide whether to use control.BeginInvoke or control.Invoke in SerialPort if you don't know exactly how the DataReceived event is handled - and this is not described anywhere - except now for my description!