欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 家装 > VB.NET实现上位机自动识别可用串口

VB.NET实现上位机自动识别可用串口

2025/3/12 22:19:26 来源:https://blog.csdn.net/m0_73500130/article/details/139621714  浏览:    关键词:VB.NET实现上位机自动识别可用串口

在实际应用中有时会牵扯到挑选可用串口,比如上位机和从站设备使用Modbus RTU协议进行通讯时需要选择COM串口,每次启动连接前都在设备管理器查看较为麻烦,可以设置一个串口自动识别功能,如果选择了错误的串口还可以提示串口选择错误。

在Visual Studio中点击新建项目,选择Visual Basic语言,先新建一个Windows窗体应用

 随后会出现一个空白的窗体应用,按 F4 键可以在右侧的属性界面对窗体的标题及格式进行更改

双击窗体即可进入到代码编辑界面(编辑Form1.vb文件)

通常不要随意删除Form1.vb中的类,因为这些类是由设计器自动生成,删除后可能会报错。

随后导入识别串口所需要的库:

Imports System.IO.Ports

 如果需要Modbus通讯功能,需要点击 项目 栏,随后点击管理NuGet程序包 下载NModbus库和NModbus.Serial库

 在工具箱中搜索:ComboBox ,这是一个可供选择的下拉列表,拖入到窗体中

 双击ComboBox,在From1类下新建一个私有类:

Private currentPortName As String = ""

在From1_Load类(窗口加载时类)中补全功能:

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.LoadDim portNames() As String = SerialPort.GetPortNames()' 将端口名添加到ComboBox中  For Each portName As String In portNamesComboBox1.Items.Add(portName)NextEnd Sub

在ComboBox1_SelectedIndexChanged(串口改变时类)补全:

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChangedIf ComboBox1.SelectedItem IsNot Nothing ThencurrentPortName = ComboBox1.SelectedItem.ToString() ' 更新currentPortName的值  TryUsing testPort As New SerialPort(currentPortName)testPort.Open()End UsingCatch ex As UnauthorizedAccessExceptionMessageBox.Show("所选串口已被占用,请重新选择一个串口。", "串口占用提示", MessageBoxButtons.OK, MessageBoxIcon.Warning)' 清除当前选择,用户可以重新选择    ComboBox1.SelectedIndex = -1currentPortName = "" ' 清除currentPortName的值  Catch ex As Exception' 捕获其他可能的异常,并进行处理    MessageBox.Show("无法打开串口:" & ex.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error)ComboBox1.SelectedIndex = -1currentPortName = ""End TryElseMessageBox.Show("请先选择一个串口。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)' 可以选择设置一个默认的串口或者不做任何操作    currentPortName = ""End IfEnd Sub

完整版代码如下:

Imports System.IO.PortsPublic Class Form1Private currentPortName As String = ""Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.LoadDim portNames() As String = SerialPort.GetPortNames()' 将端口名添加到ComboBox中  For Each portName As String In portNamesComboBox1.Items.Add(portName)NextEnd SubPrivate Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChangedIf ComboBox1.SelectedItem IsNot Nothing ThencurrentPortName = ComboBox1.SelectedItem.ToString() ' 更新currentPortName的值  TryUsing testPort As New SerialPort(currentPortName)testPort.Open()End UsingCatch ex As UnauthorizedAccessExceptionMessageBox.Show("所选串口已被占用,请重新选择一个串口。", "串口占用提示", MessageBoxButtons.OK, MessageBoxIcon.Warning)' 清除当前选择,用户可以重新选择    ComboBox1.SelectedIndex = -1currentPortName = "" ' 清除currentPortName的值  Catch ex As Exception' 捕获其他可能的异常,并进行处理    MessageBox.Show("无法打开串口:" & ex.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error)ComboBox1.SelectedIndex = -1currentPortName = ""End TryElseMessageBox.Show("请先选择一个串口。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)' 可以选择设置一个默认的串口或者不做任何操作    currentPortName = ""End IfEnd Sub
End Class

代码运行后(COM1串口已被占用,选择后会提示错误,随后提示从新选择COM串口):

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词