从串行数据vb.net循环文本框中的数据

时间:2018-11-24 12:37:55

标签: vb.net

我是VB.net的新手。我的情况是,我需要显示从串行端口到文本框的数据。在连续循环中。我该怎么办?

我写了一个简单的程序来显示数据。我可以在文本框中显示一次数据,但不能连续显示。

serialport1是我从端口1获取数据的位置。serialport2是来自端口2的数据。

SerialPort2.Write("!12,F" & vbCr)

数据“!11,F”和“!12,F”是我2台设备的命令。

TextBox2.Text和TextBox5.Text是我要连续显示数据的地方。

这是代码;

Imports System
Imports System.Threading
Imports System.IO.Ports
Imports System.ComponentModel

Public Class Form2

    Dim myPort As Array
    Dim myport1 As Array
    Delegate Sub SetTextCallback(ByVal [text] As String) 'Added to prevent threading errors during receiveing of data
    '------------------------------------------------
    Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        myPort = IO.Ports.SerialPort.GetPortNames()
        myport1 = IO.Ports.SerialPort.GetPortNames()
        ComboBox1.Items.AddRange(myPort)
        ComboBox2.Items.AddRange(myport1)

        btnwrite.Enabled = False

    End Sub
    '------------------------------------------------
    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles btninit.Click


        SerialPort1.Encoding = System.Text.Encoding.GetEncoding(28591) ' convert to ascii
        SerialPort1.PortName = ComboBox1.Text
        SerialPort1.BaudRate = TextBox3.Text
        SerialPort1.Open()

        SerialPort2.Encoding = System.Text.Encoding.GetEncoding(28591) ' convert to ascii
        SerialPort2.PortName = ComboBox2.Text
        SerialPort2.BaudRate = TextBox3.Text
        SerialPort2.Open()


        btninit.Enabled = False
        btnwrite.Enabled = True
        btnwrite2.Enabled = True
        btnclose.Enabled = True

    End Sub

    '------------------------------------------------
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles btnwrite.Click

            SerialPort1.Write("!11,F" & vbCr) 'concatenate with \n

    End Sub


    Private Sub btnwrite2_Click(sender As Object, e As EventArgs) Handles btnwrite2.Click

        SerialPort2.Write("!12,F" & vbCr) 'concatenate with \n

    End Sub

    Private Sub SerialPort1_DataReceived(sender As System.Object, e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

        ReceivedText(SerialPort1.ReadExisting())

    End Sub

    Private Sub SerialPort2_DataReceived(sender As System.Object, e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort2.DataReceived

        ReceivedText1(SerialPort2.ReadExisting())


    End Sub

    Private Sub ReceivedText(ByVal [text] As String) 'input from ReadExisting


        If Me.TextBox2.InvokeRequired Then
            Dim x As New SetTextCallback(AddressOf ReceivedText)
            Me.Invoke(x, New Object() {(text)})
        Else
            Me.TextBox2.Text &= [text] 'append text

        End If

    End Sub

    Private Sub ReceivedText1(ByVal [text] As String) 'input from ReadExisting


        If Me.TextBox5.InvokeRequired Then
            Dim x As New SetTextCallback(AddressOf ReceivedText1)
            Me.Invoke(x, New Object() {(text)})
        Else
            Me.TextBox5.Text &= [text] 'append text
        End If

    End Sub

    Private Sub btnclose_click(sender As System.Object, e As System.EventArgs) Handles btnclose.Click

        SerialPort1.Close()
        SerialPort2.Close()

        btninit.Enabled = True
        btnwrite.Enabled = False
        btnwrite2.Enabled = False
        btnclose.Enabled = False

    End Sub

End Class

0 个答案:

没有答案