Click here to Skip to main content
15,885,944 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
I am trying to edit open source code i found, there is allot of bugs there, anyway i solve everything but i still have this one: i put a combo drop-down menu to chose between serial ports to communicate with arduino, but it seems always the program chose COM5 if i chose other port or not. this is the error i have when i press start button to communicate
SQL
An unhandled exception of type 'System.IO.IOException' occurred in System.dll
Additional information: The port 'COM5' does not exist.


and this is the combo drop-down menu code
SQL
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
End Sub


and this code for the start button
VB
Private Sub Button7_Click(sender As System.Object, e As System.EventArgs) Handles Button7.Click
        SerialPort1.Open()
        SerialPort1.Write("~")
        SerialPort1.Write("LCD is working!")
        Timer2.Start()
        SerialPort1.Close()
    End Sub
Posted

I added this line to combobox and now work :) thanks
VB
SerialPort1.PortName = ComboBox1.SelectedItem

that's the final editing
VB
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        SerialPort1.PortName = ComboBox1.SelectedItem
    End Sub
End Class
 
Share this answer
 
Comments
OriginalGriff 1-May-15 12:06pm    
See! Isn't it better when you do it on your own? :laugh:

I'd suggest that you check the value of the SelectedIndex, and only do that code if it's greater than or equal to zero - otherwise you may get run time errors later.
Perhaps if you added some code to your SelecetedIndexChanged event to actually do something with the new value, it might help? Like use it to change the SerialPort1 port selection before you open it in the button click event handler?
 
Share this answer
 
Comments
OriginalGriff 1-May-15 11:38am    
Well...what do you think might be helpful? :laugh:

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