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

📄 form1.vb

📁 通过串口实现PC机和下位机的通信
💻 VB
📖 第 1 页 / 共 3 页
字号:
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(7, 120)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(82, 14)
        Me.Label2.TabIndex = 5
        Me.Label2.Text = "发送数据"
        '
        'optCom1
        '
        Me.optCom1.Checked = True
        Me.optCom1.Location = New System.Drawing.Point(10, 18)
        Me.optCom1.Name = "optCom1"
        Me.optCom1.Size = New System.Drawing.Size(64, 26)
        Me.optCom1.TabIndex = 0
        Me.optCom1.TabStop = True
        Me.optCom1.Text = "COM &1"
        '
        'btnCloseCom
        '
        Me.btnCloseCom.Enabled = False
        Me.btnCloseCom.Location = New System.Drawing.Point(211, 51)
        Me.btnCloseCom.Name = "btnCloseCom"
        Me.btnCloseCom.Size = New System.Drawing.Size(95, 27)
        Me.btnCloseCom.TabIndex = 2
        Me.btnCloseCom.Text = "关闭端口"
        '
        'optCom2
        '
        Me.optCom2.Location = New System.Drawing.Point(10, 38)
        Me.optCom2.Name = "optCom2"
        Me.optCom2.Size = New System.Drawing.Size(66, 26)
        Me.optCom2.TabIndex = 1
        Me.optCom2.Text = "COM &2"
        '
        'GroupBox1
        '
        Me.GroupBox1.Controls.Add(Me.Label8)
        Me.GroupBox1.Controls.Add(Me.btnTest)
        Me.GroupBox1.Controls.Add(Me.txtPortNum)
        Me.GroupBox1.Controls.Add(Me.txtTimeout)
        Me.GroupBox1.Controls.Add(Me.Label4)
        Me.GroupBox1.Controls.Add(Me.txtBaudrate)
        Me.GroupBox1.Controls.Add(Me.Label1)
        Me.GroupBox1.Controls.Add(Me.optCom2)
        Me.GroupBox1.Controls.Add(Me.optCom1)
        Me.GroupBox1.Location = New System.Drawing.Point(7, 11)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(198, 99)
        Me.GroupBox1.TabIndex = 0
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = "COM Setup"
        '
        'Label8
        '
        Me.Label8.Location = New System.Drawing.Point(132, 20)
        Me.Label8.Name = "Label8"
        Me.Label8.Size = New System.Drawing.Size(58, 14)
        Me.Label8.TabIndex = 8
        Me.Label8.Text = "Port check"
        '
        'btnExit
        '
        Me.btnExit.Anchor = CType(((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.btnExit.Location = New System.Drawing.Point(269, 355)
        Me.btnExit.Name = "btnExit"
        Me.btnExit.Size = New System.Drawing.Size(80, 23)
        Me.btnExit.TabIndex = 14
        Me.btnExit.Text = "退出"
        '
        'cboStatusLine
        '
        Me.cboStatusLine.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboStatusLine.Location = New System.Drawing.Point(83, 304)
        Me.cboStatusLine.Name = "cboStatusLine"
        Me.cboStatusLine.Size = New System.Drawing.Size(97, 21)
        Me.cboStatusLine.Sorted = True
        Me.cboStatusLine.TabIndex = 18
        '
        'btnCheck
        '
        Me.btnCheck.Enabled = False
        Me.btnCheck.Location = New System.Drawing.Point(185, 302)
        Me.btnCheck.Name = "btnCheck"
        Me.btnCheck.Size = New System.Drawing.Size(43, 23)
        Me.btnCheck.TabIndex = 19
        Me.btnCheck.Text = "检测"
        '
        'Label7
        '
        Me.Label7.Location = New System.Drawing.Point(84, 288)
        Me.Label7.Name = "Label7"
        Me.Label7.Size = New System.Drawing.Size(82, 14)
        Me.Label7.TabIndex = 20
        Me.Label7.Text = "Status line"
        '
        'TextBox1
        '
        Me.TextBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.TextBox1.Location = New System.Drawing.Point(9, 191)
        Me.TextBox1.Multiline = True
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(259, 68)
        Me.TextBox1.TabIndex = 32
        Me.TextBox1.Text = ""
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 14)
        Me.ClientSize = New System.Drawing.Size(360, 381)
        Me.Controls.Add(Me.TextBox1)
        Me.Controls.Add(Me.chkAddCR)
        Me.Controls.Add(Me.chkEvents)
        Me.Controls.Add(Me.txtBytes2Read)
        Me.Controls.Add(Me.Label7)
        Me.Controls.Add(Me.btnCheck)
        Me.Controls.Add(Me.cboStatusLine)
        Me.Controls.Add(Me.btnExit)
        Me.Controls.Add(Me.chkDTR)
        Me.Controls.Add(Me.chkRTS)
        Me.Controls.Add(Me.chkAutorx)
        Me.Controls.Add(Me.GroupBox1)
        Me.Controls.Add(Me.Label5)
        Me.Controls.Add(Me.Label3)
        Me.Controls.Add(Me.btnRx)
        Me.Controls.Add(Me.txtTx)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.btnTx)
        Me.Controls.Add(Me.btnCloseCom)
        Me.Controls.Add(Me.btnOpenCom)
        Me.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
        Me.MaximizeBox = False
        Me.Name = "Form1"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "180"
        Me.GroupBox1.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region




    Private Sub btnOpenCom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenCom.Click
        moRS232 = New Rs232
        Try
            '// Setup parameters
            With moRS232
                .Port = miComPort
                .BaudRate = Int32.Parse(txtBaudrate.Text)
                .DataBit = 8
                .StopBit = Rs232.DataStopBit.StopBit_2
                .Parity = Rs232.DataParity.Parity_None
                .Timeout = Int32.Parse(txtTimeout.Text)
            End With
            '// Initializes port
            moRS232.Open()
            '// Set state of RTS / DTS
            moRS232.Dtr = (chkDTR.CheckState = CheckState.Checked)
            moRS232.Rts = (chkRTS.CheckState = CheckState.Checked)
            If chkEvents.Checked Then moRS232.EnableEvents()
            chkEvents.Enabled = True
        Catch Ex As Exception
            MessageBox.Show(Ex.Message, "Connection Error", MessageBoxButtons.OK)
        Finally
            btnCloseCom.Enabled = moRS232.IsOpen
            btnOpenCom.Enabled = Not moRS232.IsOpen
            btnTx.Enabled = moRS232.IsOpen
            btnRx.Enabled = moRS232.IsOpen
            btnCheck.Enabled = moRS232.IsOpen
        End Try
    End Sub

    Private Sub btnCloseCom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCloseCom.Click
        chkEvents.Enabled = False
        moRS232.Close()
        btnCloseCom.Enabled = moRS232.IsOpen
        btnOpenCom.Enabled = Not moRS232.IsOpen
        btnTx.Enabled = moRS232.IsOpen
        btnRx.Enabled = moRS232.IsOpen
        btnCheck.Enabled = moRS232.IsOpen
    End Sub

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTx.Click
        Dim sTx As String
        Dim i, m As Integer
        Dim k As Byte
        Dim buff() As Byte


        '----------------------
        '// Clear Tx/Rx Buffers
        moRS232.PurgeBuffer(Rs232.PurgeBuffers.TxClear Or Rs232.PurgeBuffers.RXClear)

        sTx = txtTx.Text
        buff = System.Text.Encoding.Default.GetBytes(sTx)
        'buff(0) = buff(0) - &H30

        Dim buff3(4) As Byte
        buff3(0) = &H30
        buff3(1) = &H37
        buff3(2) = &H57
        buff3(4) = &H10

        For i = 0 To buff.Length - 1

            If (buff(i) >= &H30) And (buff(i) <= &H40) Then

                buff(i) = buff(i) - buff3(0)



            ElseIf (buff(i) >= &H41) And (buff(i) <= &H46) Then

                buff(i) = buff(i) - buff3(1)


            ElseIf (buff(i) >= &H61) And (buff(i) <= &H66) Then
                buff(i) = buff(i) - buff3(2)

            End If
        Next i
        'buff2(0) = &H12
        Dim buff2(5) As Byte


        buff2(0) = buff(0) * buff3(4) + buff(1)
        buff2(1) = buff(2) * buff3(4) + buff(3)
        buff2(2) = buff(4) * buff3(4) + buff(5)
        buff2(3) = buff(6) * buff3(4) + buff(7)
        buff2(4) = buff(8) * buff3(4) + buff(9)
        buff(0) = buff2(0)
        buff(1) = buff2(1)
        buff(2) = buff2(2)
        buff(3) = buff2(3)
        buff(4) = buff2(4)


        'Dim str As String
        'str = buff(0).ToString
        'MsgBox(str)

        ''mgsbox(buff2(0).ToString)
        ''''MsgBox("buff2")
        'buff(0) = 35



        If chkAddCR.Checked Then sTx += ControlChars.Cr
        moRS232.Write(buff)
        'moRS232.Write(buff)


        'moRS232.Write(Chr(2) & Chr(2) & Chr(73) & Chr(48) & Chr(121) & Chr(3))
        '// Clears Rx textbox
        'txtRx.Text = String.Empty
        'txtRx.Refresh()
        'lbHex.Items.Clear()
        If chkAutorx.Checked Then Button1_Click(Nothing, Nothing)
    End Sub

    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        If Not moRS232 Is Nothing Then
            '// Disables Events if active
            moRS232.DisableEvents()
            If moRS232.IsOpen Then moRS232.Close()
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRx.Click
        Dim cbytes(100) As Byte
        Dim BBYTES(100) As Byte
        Dim i As Integer
        Dim key As Integer

        Try

⌨️ 快捷键说明

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