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

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

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

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

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

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

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

vbnet 复制代码
Imports System.IO.Ports

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

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

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

vbnet 复制代码
Private currentPortName As String = ""

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

vbnet 复制代码
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim portNames() As String = SerialPort.GetPortNames()
        ' 将端口名添加到ComboBox中  
        For Each portName As String In portNames
            ComboBox1.Items.Add(portName)
        Next
    End Sub

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

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

完整版代码如下:

vbnet 复制代码
Imports System.IO.Ports

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

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

相关推荐
SY师弟12 分钟前
51单片机——交通指示灯控制器设计
c语言·单片机·嵌入式硬件·51单片机
weixin_413920611 小时前
标签部件(lv_label)
单片机·嵌入式硬件
2301_800399721 小时前
stm32 ADC单通道转换
stm32·单片机·嵌入式硬件
高工智能汽车2 小时前
抢跑「中央计算+区域控制」市场,芯驰科技高端智控MCU“芯”升级
科技·单片机·嵌入式硬件
HHONGQI1232 小时前
STM32 之网口资源
stm32·单片机·嵌入式硬件
GodKK老神灭2 小时前
STM32 启动文件分析
stm32·单片机·嵌入式硬件
free-elcmacom2 小时前
labview硬件采集<2>——使用布尔控件控制硬件的LED
单片机·嵌入式硬件·labview
懒惰的bit11 小时前
STM32F103C8T6板子使用说明
stm32·单片机·嵌入式硬件
芯岭技术11 小时前
PY32系列单片机离线烧录器,可配置选项字节和上机台批量烧录
单片机·嵌入式硬件
阿川!14 小时前
嵌入式软件--stm32 DAY7 I2C通讯上
stm32·单片机·嵌入式硬件·mcu