⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainform.vb

📁 列示、检测所有串口
💻 VB
字号:
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports System.IO.Ports


Public Class MainForm
    '定义全局变量
    Public GPSPort As New SerialPort()   '定义并创建串口变量
    Public GPSRate, GPSCOMName As String
    '   Public GPSByte(256) As Byte


    Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click
        ComboBox1.Visible = True
        ComboBox2.Visible = True

        '添加所有串口名称
        If ComboBox1.Items.Count = 0 Then
            For PortNo As Integer = 0 To SerialPort.GetPortNames().Length - 1
                ComboBox1.Items.Add(SerialPort.GetPortNames(PortNo))
            Next
        End If

    End Sub


    Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem4.Click

        GPSCOMName = ComboBox1.SelectedItem
        GPSRate = ComboBox2.SelectedItem

        If GPSCOMName = Nothing Or GPSRate = Nothing Then
            Label3.Text = "请选择端口及波特率"
            Return
        End If

        'ComboBox1.Dispose()
        'ComboBox2.Dispose()


        If GPSPort.IsOpen And GPSPort.PortName = GPSCOMName Then
            GPSPort.DiscardInBuffer()
        Else
            GPSPort.Close()

            GPSPort.PortName = GPSCOMName
            GPSPort.BaudRate = GPSRate
            GPSPort.Parity = IO.Ports.Parity.None
            GPSPort.DataBits = 8
            GPSPort.StopBits = IO.Ports.StopBits.One
            GPSPort.Encoding = System.Text.Encoding.ASCII

            'GPSPort.Handshake = Handshake.RequestToSend
            GPSPort.ReadTimeout = 2500
            'GPSPort.ReadBufferSize = 256
            GPSPort.DtrEnable = True

            Try
                GPSPort.Open()
            Catch ex As Exception
                Label3.Text = GPSCOMName + ": 端口打开出错-->>:  " + ex.Message
                Return
            End Try

        End If


        Try
            If GPSPort.ReadExisting = "" Then
                Label3.Text = GPSCOMName + ": 无数据"
            Else
                Label3.Text = GPSPort.ReadExisting
            End If

        Catch ex As Exception
            Label3.Text = GPSCOMName + ": 读取数据出错-->>: " + ex.Message
            Return
        End Try

    End Sub



    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Label3_ParentChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.ParentChanged

    End Sub
End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -