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

📄 frmmain.vb

📁 RS232串口通信编程,也是一个好东西
💻 VB
字号:
Imports System.Text

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
    Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem3 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem4 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem5 As System.Windows.Forms.MenuItem
    Friend WithEvents ToolBar1 As System.Windows.Forms.ToolBar
    Friend WithEvents imlTB As System.Windows.Forms.ImageList
    Friend WithEvents ToolBarButton1 As System.Windows.Forms.ToolBarButton
    Friend WithEvents ToolBarButton2 As System.Windows.Forms.ToolBarButton
    Friend WithEvents ToolBarButton3 As System.Windows.Forms.ToolBarButton
    Friend WithEvents ToolBarButton4 As System.Windows.Forms.ToolBarButton
    Friend WithEvents sbr As System.Windows.Forms.StatusBar
    Friend WithEvents sbpComNo As System.Windows.Forms.StatusBarPanel
    Friend WithEvents sbpSettings As System.Windows.Forms.StatusBarPanel
    Friend WithEvents sbpStatus As System.Windows.Forms.StatusBarPanel
    Friend WithEvents tmrRead As System.Windows.Forms.Timer
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container()
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
        Me.MainMenu1 = New System.Windows.Forms.MainMenu()
        Me.MenuItem1 = New System.Windows.Forms.MenuItem()
        Me.MenuItem2 = New System.Windows.Forms.MenuItem()
        Me.MenuItem3 = New System.Windows.Forms.MenuItem()
        Me.MenuItem4 = New System.Windows.Forms.MenuItem()
        Me.MenuItem5 = New System.Windows.Forms.MenuItem()
        Me.ToolBar1 = New System.Windows.Forms.ToolBar()
        Me.ToolBarButton1 = New System.Windows.Forms.ToolBarButton()
        Me.ToolBarButton2 = New System.Windows.Forms.ToolBarButton()
        Me.ToolBarButton3 = New System.Windows.Forms.ToolBarButton()
        Me.ToolBarButton4 = New System.Windows.Forms.ToolBarButton()
        Me.imlTB = New System.Windows.Forms.ImageList(Me.components)
        Me.sbr = New System.Windows.Forms.StatusBar()
        Me.sbpComNo = New System.Windows.Forms.StatusBarPanel()
        Me.sbpSettings = New System.Windows.Forms.StatusBarPanel()
        Me.sbpStatus = New System.Windows.Forms.StatusBarPanel()
        Me.tmrRead = New System.Windows.Forms.Timer(Me.components)
        Me.TextBox1 = New System.Windows.Forms.TextBox()
        Me.Button1 = New System.Windows.Forms.Button()
        Me.TextBox2 = New System.Windows.Forms.TextBox()
        CType(Me.sbpComNo, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.sbpSettings, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.sbpStatus, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'MainMenu1
        '
        Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1})
        '
        'MenuItem1
        '
        Me.MenuItem1.Index = 0
        Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem2, Me.MenuItem3, Me.MenuItem4, Me.MenuItem5})
        Me.MenuItem1.Text = "&File"
        '
        'MenuItem2
        '
        Me.MenuItem2.Index = 0
        Me.MenuItem2.Text = "&Connect..."
        '
        'MenuItem3
        '
        Me.MenuItem3.Enabled = False
        Me.MenuItem3.Index = 1
        Me.MenuItem3.Text = "&Disconnect"
        '
        'MenuItem4
        '
        Me.MenuItem4.Index = 2
        Me.MenuItem4.Text = "-"
        '
        'MenuItem5
        '
        Me.MenuItem5.Index = 3
        Me.MenuItem5.Text = "E&xit"
        '
        'ToolBar1
        '
        Me.ToolBar1.Buttons.AddRange(New System.Windows.Forms.ToolBarButton() {Me.ToolBarButton1, Me.ToolBarButton2, Me.ToolBarButton3, Me.ToolBarButton4})
        Me.ToolBar1.DropDownArrows = True
        Me.ToolBar1.ImageList = Me.imlTB
        Me.ToolBar1.Name = "ToolBar1"
        Me.ToolBar1.ShowToolTips = True
        Me.ToolBar1.Size = New System.Drawing.Size(915, 41)
        Me.ToolBar1.TabIndex = 0
        '
        'ToolBarButton1
        '
        Me.ToolBarButton1.ImageIndex = 0
        Me.ToolBarButton1.ToolTipText = "Connect to RS232"
        '
        'ToolBarButton2
        '
        Me.ToolBarButton2.Enabled = False
        Me.ToolBarButton2.ImageIndex = 1
        Me.ToolBarButton2.ToolTipText = "Disconnect"
        '
        'ToolBarButton3
        '
        Me.ToolBarButton3.Style = System.Windows.Forms.ToolBarButtonStyle.Separator
        '
        'ToolBarButton4
        '
        Me.ToolBarButton4.ImageIndex = 2
        Me.ToolBarButton4.ToolTipText = "Exit the application"
        '
        'imlTB
        '
        Me.imlTB.ColorDepth = System.Windows.Forms.ColorDepth.Depth24Bit
        Me.imlTB.ImageSize = New System.Drawing.Size(32, 32)
        Me.imlTB.ImageStream = CType(resources.GetObject("imlTB.ImageStream"), System.Windows.Forms.ImageListStreamer)
        Me.imlTB.TransparentColor = System.Drawing.Color.Transparent
        '
        'sbr
        '
        Me.sbr.Location = New System.Drawing.Point(0, 516)
        Me.sbr.Name = "sbr"
        Me.sbr.Panels.AddRange(New System.Windows.Forms.StatusBarPanel() {Me.sbpComNo, Me.sbpSettings, Me.sbpStatus})
        Me.sbr.ShowPanels = True
        Me.sbr.Size = New System.Drawing.Size(915, 22)
        Me.sbr.TabIndex = 1
        Me.sbr.Text = "StatusBar1"
        '
        'sbpSettings
        '
        Me.sbpSettings.Width = 300
        '
        'sbpStatus
        '
        Me.sbpStatus.Width = 500
        '
        'tmrRead
        '
        '
        'TextBox1
        '
        Me.TextBox1.Enabled = False
        Me.TextBox1.Location = New System.Drawing.Point(4, 54)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(817, 20)
        Me.TextBox1.TabIndex = 2
        Me.TextBox1.Text = ""
        '
        'Button1
        '
        Me.Button1.Enabled = False
        Me.Button1.Image = CType(resources.GetObject("Button1.Image"), System.Drawing.Bitmap)
        Me.Button1.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.Button1.Location = New System.Drawing.Point(831, 48)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(81, 30)
        Me.Button1.TabIndex = 3
        Me.Button1.Text = "&Send"
        Me.Button1.TextAlign = System.Drawing.ContentAlignment.MiddleRight
        '
        'TextBox2
        '
        Me.TextBox2.Enabled = False
        Me.TextBox2.Location = New System.Drawing.Point(4, 83)
        Me.TextBox2.Multiline = True
        Me.TextBox2.Name = "TextBox2"
        Me.TextBox2.ReadOnly = True
        Me.TextBox2.ScrollBars = System.Windows.Forms.ScrollBars.Both
        Me.TextBox2.Size = New System.Drawing.Size(906, 426)
        Me.TextBox2.TabIndex = 4
        Me.TextBox2.Text = ""
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(915, 538)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.TextBox2, Me.Button1, Me.TextBox1, Me.sbr, Me.ToolBar1})
        Me.Menu = Me.MainMenu1
        Me.Name = "Form1"
        Me.Text = "My RS232 Tester"
        CType(Me.sbpComNo, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.sbpSettings, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.sbpStatus, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region
    Private oCP As New Rs232()

    Private intCommPort, intBaud, intData As Integer
    Private bytStop As Rs232.DataStopBit
    Private bytParity As Rs232.DataParity

    Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
        Dim fOpt As New Options()

        fOpt.ShowDialog()
        If fOpt.bGo Then
            sbr.Panels(0).Text = fOpt.ComboBox1.SelectedItem
            sbr.Panels(1).Text = fOpt.ComboBox2.SelectedItem & " - " & fOpt.ComboBox3.SelectedItem & " - " _
             & fOpt.ComboBox4.SelectedItem & " - " & fOpt.ComboBox5.SelectedItem
            intCommPort = CInt(Mid(fOpt.ComboBox1.SelectedItem, 4))
            intBaud = CInt(fOpt.ComboBox2.SelectedItem)
            intData = CInt(fOpt.ComboBox3.SelectedItem)
            Select Case fOpt.ComboBox4.SelectedIndex
                Case 0
                    bytParity = Rs232.DataParity.Parity_Even
                Case 1
                    bytParity = Rs232.DataParity.Pariti_Odd
                Case 2
                    bytParity = Rs232.DataParity.Parity_None
                Case Else
                    bytParity = Rs232.DataParity.Parity_Mark
            End Select
            Select Case fOpt.ComboBox5.SelectedIndex
                Case 0
                    bytStop = Rs232.DataStopBit.StopBit_1
                Case Else
                    bytStop = Rs232.DataStopBit.StopBit_2
            End Select
            oCP.Open(intCommPort, intBaud, intData, bytParity, bytStop, 4096)
            tmrRead.Enabled = True
            MenuItem2.Enabled = False
            MenuItem3.Enabled = True
            ToolBar1.Buttons(0).Enabled = False
            ToolBar1.Buttons(1).Enabled = True
            TextBox1.Enabled = True
            TextBox2.Enabled = True
            Button1.Enabled = True
        End If
        fOpt.Close()
        fOpt = Nothing
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim i As Integer

        For i = 0 To 3
            mComs(i) = IsPortAvailable(i + 1)
        Next
    End Sub

    ' This function attempts to open the passed Comm Port. If it is
    '   available, it returns True, else it returns False. To determine
    '   availability a Try-Catch block is used.
    Private Function IsPortAvailable(ByVal ComPort As Integer) As Boolean
        Try
            oCP.Open(ComPort, 115200, 8, Rs232.DataParity.Parity_None, _
                Rs232.DataStopBit.StopBit_1, 4096)
            ' If it makes it to here, then the Comm Port is available.
            oCP.Close()
            Return True
        Catch
            ' If it gets here, then the attempt to open the Comm Port
            '   was unsuccessful.
            Return False
        End Try
    End Function

    Private Sub ToolBar1_ButtonClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs) Handles ToolBar1.ButtonClick
        Select Case ToolBar1.Buttons.IndexOf(e.Button)
            Case 0
                MenuItem2_Click(MenuItem2, New System.EventArgs())
            Case 1
                MenuItem3_Click(MenuItem3, New System.EventArgs())
            Case Else
                MenuItem5_Click(MenuItem5, New System.EventArgs())
        End Select
    End Sub

    Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click
        oCP.Close()
        tmrRead.Enabled = False
        MenuItem2.Enabled = True
        MenuItem3.Enabled = False
        ToolBar1.Buttons(0).Enabled = True
        ToolBar1.Buttons(1).Enabled = False
        TextBox1.Enabled = False
        TextBox2.Enabled = False
        Button1.Enabled = False
    End Sub

    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        If MenuItem3.Enabled Then MenuItem3_Click(MenuItem3, New System.EventArgs())
    End Sub

    Private Sub MenuItem5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem5.Click
        Me.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Try
            ' Enable the timer.
            ' Write an user specified Command to the Port.
            oCP.Write(Encoding.ASCII.GetBytes(Me.TextBox1.Text & Chr(13)))
            WriteMessage(TextBox1.Text, True)

        Catch ex As Exception
            ' Warn the user.
            MessageBox.Show("Unable to write to comm port")
        Finally
            TextBox1.Text = ""
            TextBox1.Focus()
        End Try
    End Sub

    Private Sub tmrRead_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrRead.Tick
        Try
            ' As long as there is information, read one byte at a time and 
            '   output it.
            While (oCP.Read(1) <> -1)
                ' Write the output to the screen.
                WriteMessage(Chr(oCP.InputStream(0)), False)
            End While
        Catch exc As Exception
            ' An exception is raised when there is no information to read.
            '   Don't do anything here, just let the exception go.
        End Try
    End Sub

    ' This subroutine writes a message to the txtStatus TextBox.
    Private Sub WriteMessage(ByVal message As String)
        Me.TextBox2.Text += message + vbCrLf
        TextBox2.SelectionStart = TextBox2.Text.Length
    End Sub

    ' This subroutine writes a message to the txtStatus TextBox and allows
    '   the line feed to be suppressed.
    Private Sub WriteMessage(ByVal message As String, ByVal linefeed As Boolean)
        Me.TextBox2.Text += message
        If linefeed Then
            Me.TextBox2.Text += vbCrLf
        End If
        TextBox2.SelectionStart = TextBox2.Text.Length
    End Sub
End Class

⌨️ 快捷键说明

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