从COM端口读取数据并将其写入VB中的文本框

时间:2011-10-21 18:23:31

标签: vb.net

我再次,我无法从一个COM端口读取并将该数据放入文本框中。收到的数据需要更新到我所做的文本框中。发送p字符时,我得到如下所示的数据: 一个0012 B 0251 C 0045 D 3652 这些数据需要由A B C和D分隔出来并放在适当的文本框中。这是代码:

Public Class Form2

Dim buttonOnePush As Boolean = False
Dim buttonTwoPush As Boolean = False



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    ' Send strings to a serial port.

    Timer1.Enabled = True
    Using com5 As IO.Ports.SerialPort = My.Computer.Ports.OpenSerialPort("COM5")
        com5.Write("P")
        TextBox2.Text = ""


    End Using

End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    Using com5 As IO.Ports.SerialPort = My.Computer.Ports.OpenSerialPort("COM5")
        com5.Write("P")
    End Using

End Sub

Function ReceiveComData() As String
    ' Receive strings from a serial port.
    Dim returnStr As String = ""

    Dim com5 As IO.Ports.SerialPort = Nothing


    Try
        com5 = My.Computer.Ports.OpenSerialPort("COM5")
        com5.ReadTimeout = 10000
        Do
            Dim Incoming As String = com5.ReadLine()


            If Incoming Is Nothing Then
                Exit Do
            Else
                returnStr &= Incoming & vbCrLf
            End If
        Loop
    Catch ex As TimeoutException
        returnStr = "Error: Serial Port read timed out."
    Finally
        If com5 IsNot Nothing Then com5.Close()
    End Try

    Return returnStr
End Function


Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    If IsNumeric(TextBox1.Text) AndAlso IsNumeric(TextBox2.Text) Then
        TextBox1.Text = CDec(TextBox2.Text)
    End If
End Sub

Private Sub TextBox6_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox6.TextChanged
    If IsNumeric(TextBox6.Text) AndAlso IsNumeric(TextBox3.Text) Then
        TextBox6.Text = CDec(TextBox3.Text)
    End If
End Sub

Private Sub TextBox7_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox7.TextChanged
    If IsNumeric(TextBox7.Text) AndAlso IsNumeric(TextBox4.Text) Then
        TextBox7.Text = CDec(TextBox4.Text)
    End If
End Sub

Private Sub TextBox8_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox8.TextChanged
    If IsNumeric(TextBox8.Text) AndAlso IsNumeric(TextBox5.Text) Then
        TextBox8.Text = CDec(TextBox5.Text)
    End If
End Sub

Private Sub TextBox15_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox15.TextChanged
    If IsNumeric(TextBox15.Text) AndAlso IsNumeric(TextBox16.Text) Then
        TextBox15.Text = Hex(TextBox16.Text)
    End If
End Sub

Private Sub TextBox14_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox14.TextChanged
    If IsNumeric(TextBox14.Text) AndAlso IsNumeric(TextBox11.Text) Then
        TextBox14.Text = Hex(TextBox11.Text)
    End If
End Sub

Private Sub TextBox13_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox13.TextChanged
    If IsNumeric(TextBox13.Text) AndAlso IsNumeric(TextBox10.Text) Then
        TextBox13.Text = Hex(TextBox10.Text)
    End If
End Sub

Private Sub TextBox12_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox12.TextChanged
    If IsNumeric(TextBox12.Text) AndAlso IsNumeric(TextBox9.Text) Then
        TextBox12.Text = Hex(TextBox9.Text)
    End If
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    buttonTwoPush = True
    buttonOnePush = False
    Timer1.Enabled = False

    Me.Close()
    Form1.Close()

End Sub

结束班

0 个答案:

没有答案