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

📄 form1.vb

📁 通过串口实现PC机和下位机的通信
💻 VB
📖 第 1 页 / 共 3 页
字号:
            moRS232.Read(Int32.Parse(txtBytes2Read.Text))
            'moRS232.Read(20)

            key = Int32.Parse(txtBytes2Read.Text)
            'txtRx.Text = moRS232.InputStreamString
            cbytes = moRS232.InputStream
            TextBox1.Text = Nothing
            'TextBox1.Text = cbytes(0).ToString("X")
            'TextBox1.Text = TextBox1.Text & cbytes(1).ToString("X")
            'TextBox1.Text = TextBox1.Text & cbytes(2).ToString("X")
            'TextBox1.Text = TextBox1.Text & cbytes(3).ToString("X")
            'TextBox1.Text = TextBox1.Text & cbytes(4).ToString("X")

            For i = 0 To key - 1


                TextBox1.Text = TextBox1.Text & cbytes(i).ToString("X")
            Next i



            'BBYTES(0) = cbytes(1)
            'txtRx.ForeColor = Color.Black
            'txtRx.BackColor = Color.White


        Catch Ex As Exception
            'txtRx.BackColor = Color.Red
            'txtRx.ForeColor = Color.White
            '  txtRx.Text = moRS232.InputStreamString
            cbytes = moRS232.InputStream
            TextBox1.Text = Nothing

            key = Int32.Parse(txtBytes2Read.Text)

            'TextBox1.Text = cbytes(0).ToString("X")
            'TextBox1.Text = TextBox1.Text & cbytes(1).ToString("X")
            'TextBox1.Text = TextBox1.Text & cbytes(2).ToString("X")
            'TextBox1.Text = TextBox1.Text & cbytes(3).ToString("X")
            'TextBox1.Text = TextBox1.Text & cbytes(4).ToString("X")

            For i = 0 To key - 1


                TextBox1.Text = TextBox1.Text & cbytes(i).ToString("X")
            Next i

            'TextBox1.Text = cbytes(0).ToString("X")

        End Try
    End Sub



    Private Sub chkDTR_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkDTR.Click
        If Not (moRS232 Is Nothing) Then
            moRS232.Dtr = chkDTR.CheckState = CheckState.Checked
        End If
    End Sub

    Private Sub chkRTS_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkRTS.Click
        If Not (moRS232 Is Nothing) Then
            moRS232.Rts = chkRTS.CheckState = CheckState.Checked
        End If

    End Sub

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


    Private Sub optCom1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optCom1.CheckedChanged, optCom2.CheckedChanged
        If (sender Is optCom1) Then
            miComPort = 1
        Else
            miComPort = 2
        End If
    End Sub

    Private Sub linkAuthor_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs)
        System.Diagnostics.Process.Start("mailto:corrado@mvps.org")
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '===================================================
        '
        '	Description	:	Fills form items
        '	Created			:	28/02/2002 - 10:33:20
        '
        '						*Parameters Info*
        '
        '	Notes				:
        '===================================================
        cboStatusLine.Items.Add("CTS")
        cboStatusLine.Items.Add("DSR")
        cboStatusLine.Items.Add("RI")
        cboStatusLine.Items.Add("CD")
    End Sub

    Private Sub btnCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheck.Click
        '===================================================
        '												
        '	Description	:	Check passed status line
        '	Created			:	28/02/2002 - 10:35:54
        '
        '						*Parameters Info*
        '
        '	Notes				:
        '===================================================
        If Not moRS232 Is Nothing Then
            Dim bState As Boolean
            Select Case cboStatusLine.Text
                Case "CTS"
                    bState = moRS232.CheckLineStatus(Rs232.ModemStatusBits.ClearToSendOn)
                Case "DSR"
                    bState = moRS232.CheckLineStatus(Rs232.ModemStatusBits.DataSetReadyOn)
                Case "RI"
                    bState = moRS232.CheckLineStatus(Rs232.ModemStatusBits.RingIndicatorOn)
                Case "CD"
                    bState = moRS232.CheckLineStatus(Rs232.ModemStatusBits.CarrierDetect)
            End Select
            MessageBox.Show("Selected line is " & IIf(bState, "On", "Off").ToString, "Check line status")
        End If
    End Sub

    Private Sub btnAsyncTx_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        '// Clears Rx textbox
        'lbAsync.Items.Clear()
        'txtRx.Text = String.Empty
        'txtRx.Refresh()
        'lbHex.Items.Clear()
        mlTicks = DateTime.Now.Ticks
        'moRS232.AsyncWrite(Chr(4) & Chr(3) & Chr(0) & Chr(0) & Chr(0) & Chr(16) & Chr(68) & Chr(83))
        'lbAsync.Items.Add("Tx Started at ticks: " & mlTicks.ToString)
        If chkAutorx.Checked Then btnAsync_Click(Nothing, Nothing)
    End Sub

    Private Sub btnAsync_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Try
            'lbHex.Text = String.Empty
            'lbHex.Refresh()
            Dim lTicks As Long = DateTime.Now.Ticks
            'lbAsync.Items.Add("Rx Started at ticks: " & lTicks.ToString & " (" & (lTicks - mlTicks).ToString & ")")
        Catch Ex As Exception
            'txtRx.BackColor = Color.Red
            'txtRx.ForeColor = Color.White
            'txtRx.Text = "Error occurred " & Ex.Message & "  data fetched: " & moRS232.InputStreamString
        End Try
    End Sub

    Private Sub moRS232_CommEvent(ByVal source As Rs232, ByVal Mask As Rs232.EventMasks) Handles moRS232.CommEvent
        '===================================================
        '												?003 www.codeworks.it All rights reserved
        '
        '	Description	:	Events raised when a comunication event occurs
        '	Created			:	15/07/03 - 15:13:46
        '	Author			:	Corrado Cavalli
        '
        '						*Parameters Info*
        '
        '	Notes				:	*IMPORTANT!* this event is raised on another thread
        '								if you need to access UI use invoke method as example below
        '===================================================
        Dim oUIUpdater As New CommEventUpdate(AddressOf pUICommEventUpdate)
        Dim args(1) As Object
        args(0) = source
        args(1) = Mask
        'lblAsync.Invoke(oUIUpdater, args)
    End Sub

#Region "UI update routine"
    Private Sub pUICommEventUpdate(ByVal source As Rs232, ByVal mask As Rs232.EventMasks)
        '===================================================
        '												?003 www.codeworks.it All rights reserved
        '
        '	Description	:	Updates UI in Main UI thread
        '	Created			:	15/07/03 - 15:16:08
        '	Author			:	Corrado Cavalli
        '
        '						*Parameters Info*
        '
        '	Notes				:
        '===================================================
        Dim iPnt As Int32, sBuf As String, Buffer() As Byte
        '---------------------------------------------------------
        Debug.Assert(Me.InvokeRequired = False)
        ' lbAsync.Items.Add("Mask: " & mask.ToString)
        If (mask And Rs232.EventMasks.RxChar) > 0 Then
            'lbHex.Items.Add("Received data: " & source.InputStreamString)
            Buffer = source.InputStream
            For iPnt = 0 To Buffer.Length - 1
                'lbHex.Items.Add(iPnt.ToString & ControlChars.Tab & String.Format("0x{0}", Buffer(iPnt).ToString("X")))
            Next
            'lbHex.SelectedIndex = lbHex.Items.Count - 1
        End If
        'lbAsync.SelectedIndex = lbAsync.Items.Count - 1
    End Sub
#End Region

    Private Sub chkEvents_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkEvents.CheckedChanged
        '===================================================
        '												?003 www.codeworks.it All rights reserved
        '
        '	Description	:	Set state of notification events
        '	Created			:	16/07/03 - 9:44:05
        '	Author			:	Corrado Cavalli
        '
        '						*Parameters Info*
        '
        '	Notes				:
        '===================================================
        If Not moRS232 Is Nothing Then
            If txtBytes2Read.Text.Length = 0 Then
                moRS232.RxBufferThreshold = 1
            Else
                moRS232.RxBufferThreshold = Int32.Parse(txtBytes2Read.Text)
            End If
            If chkEvents.Checked Then
                moRS232.EnableEvents()
            Else
                moRS232.DisableEvents()
            End If
        End If
    End Sub

    Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs)
        System.Diagnostics.Process.Start("http://www.codeworks.it/net/index.htm")
    End Sub


    Private Sub lbAsync_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub lbAsync_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
        ' lbAsync.Items.Clear()
    End Sub


    Private Sub lbHex_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'lbHex.Items.Clear()
    End Sub

    Private Sub txtRx_BackColorChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub txtRx_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'txtRx.Text = String.Empty
    End Sub


    Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click
        Dim rs As New Rs232
        Try
            If rs.IsPortAvailable(Int32.Parse(txtPortNum.Text)) Then
                MessageBox.Show("Port available", "Port test", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                MessageBox.Show("Port NOT available", "Port test", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Catch ex As Exception
            MessageBox.Show("Port test failed", "Port test", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub chkRTS_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkRTS.CheckedChanged

    End Sub

    Private Sub cboStatusLine_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboStatusLine.SelectedIndexChanged

    End Sub

    Private Sub Button1_Click_2(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'lbHex.Text = Nothing

    End Sub
End Class

⌨️ 快捷键说明

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