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

VB.net chosing serial port System.IO.IOException

我正在尝试编辑我发现的开源代码,那里有很多错误,无论如何我解决了所有问题,但我仍然有这个: 我放了一个组合下拉菜单来选择串行端口与 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

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

这段代码

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_SelectedIndexChanged 添加一些代码以更新 SerialPort1 的属性以反映用户选择的 COM 端口。