VB.net选择串口System.IO.IOException [英] VB.net chosing serial port System.IO.IOException

查看:298
本文介绍了VB.net选择串口System.IO.IOException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
End Sub





和开始按钮的代码



and this code for the start button

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

推荐答案

我将此行添加到组合框和现在工作:)谢谢

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



这是最后的编辑


that's the final editing

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        SerialPort1.PortName = ComboBox1.SelectedItem
    End Sub
End Class


也许如果您在SelecetedIndexChanged事件中添加了一些代码来实际使用新值执行某些操作,那么它可能会有所帮助吗?比如在按钮单击事件处理程序中打开它之前用它来更改SerialPort1端口选择吗?
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?


这篇关于VB.net选择串口System.IO.IOException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆