VB.net选择串行端口System.IO.IOException

VB.net选择串行端口System.IO.IOException,vb.net,serial-port,arduino,Vb.net,Serial Port,Arduino,我正在尝试编辑我发现的开源代码,那里有很多bug,不管怎样我解决了所有问题,但我仍然有一个: 我放了一个组合下拉菜单来选择与arduino通信的串行端口,但如果我选择或不选择其他端口,程序似乎总是选择COM5。 这是我按下开始按钮进行通信时出现的错误 An unhandled exception of type 'System.IO.IOException' occurred in System.dll Additional information: The port 'COM5' does n

我正在尝试编辑我发现的开源代码,那里有很多bug,不管怎样我解决了所有问题,但我仍然有一个: 我放了一个组合下拉菜单来选择与arduino通信的串行端口,但如果我选择或不选择其他端口,程序似乎总是选择COM5。 这是我按下开始按钮进行通信时出现的错误

An unhandled exception of type 'System.IO.IOException' occurred in System.dll
Additional information: The port 'COM5' does not exist.
这是组合下拉菜单代码

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) 
                                              Handles ComboBox1.SelectedIndexChanged
End Sub
这是开始按钮的代码

 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
但如果我选择其他端口或不选择其他端口,程序似乎总是选择COM5。这是我按下开始按钮进行通信时出现的错误

An unhandled exception of type 'System.IO.IOException' occurred in System.dll
Additional information: The port 'COM5' does not exist.
此代码

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
End Sub
用于获取用户选择的组合框索引,并设置用户选择的串行端口。但是,该处理程序的主体是空的,因此它实际上没有执行任何操作

代码

Private Sub Button7_Click(sender As System.Object, e As System.EventArgs) Handles Button7.Click
        SerialPort1.Open()
可能是引发异常的位置。您尚未在显示的代码中的任何位置更改SerialPort1的设置。您应该向ComboBox1\u SelectedIndexChanged添加一些代码,以更新SerialPort1的属性,以反映用户选择的COM端口