VB.net serialport.readline 对比数据接收对比线程

VB.net serialport.readline vs datareceived vs threading

新手(非常新手)程序员,在 VB.net 工作...我读过很多关于这个的不同观点,seams threading 将是最好的解决方案,但在这一点上我有点头疼。 我想要完成的是从串行端口(蓝牙)读取一行数据。数据的长度始终相同,并且总是以 CRLF 结尾(数据为 ASCII)。我正在读取数字扭矩扳手的数据,用户必须用扳手拧紧6个螺栓,每个螺栓通过串口发送一串数据。我读取了这个原始数据,并从字符串中提取了实际扭矩值并将其保存到数据库中。我的数据库连接正常,我可以读取数据并提取我需要的值。问题是如果用户出于某种原因必须退出阅读,我将无法退出活动。我尝试添加一个将变量设置为 true 的按钮(Dim BoolEscape as Boolean 为“True”)。然后添加了一个 Application.DoEvents() 调用,然后检查是否 BoolEscape = True 然后退出子...从我读过的内容来看,这是行不通的,永远不会。

收到的数据字符串格式为:"RE,001,100.0,16/08/20,12:45:10CRLF"

下面是我的 BtnStartRead_Click 事件中的一些代码...我只显示了 2 个螺栓的价值,因为它只是重复....我相信线程是答案,但我还是不明白它是如何实现的有效....

 Private Sub btnStartRead_Click(sender As Object, e As EventArgs) Handles btnStartRead.Click
    On Error GoTo err_handle
    boolEscape = False
    txtBolt1.BackColor = Color.FromArgb(255, 255, 128)
    txtBolt2.BackColor = Color.FromArgb(255, 255, 128)
    txtBolt3.BackColor = Color.FromArgb(255, 255, 128)
    txtBolt4.BackColor = Color.FromArgb(255, 255, 128)
    txtBolt5.BackColor = Color.FromArgb(255, 255, 128)
    txtBolt6.BackColor = Color.FromArgb(255, 255, 128)
    lblA1.Visible = True
    ShowRXon()
    txtRaw1.Text = mySerialPort.ReadLine
    ShowRXoff()
    txtBolt1.Text = txtRaw1.Text.Substring(7, 4)
    If checkValue(txtBolt1.Text) = False Then
        txtBolt1.BackColor = Color.Red
    End If
    lblA1.Visible = False
    lblA2.Visible = True

    Application.DoEvents()
    If boolEscape = True Then
        MsgBox("You have STOPPED the measurment logging...", vbOKOnly)
        boolEscape = False
        Exit Sub
    End If

    ShowRXon()
    txtRaw2.Text = mySerialPort.ReadLine
    ShowRXoff()
    txtBolt2.Text = txtRaw2.Text.Substring(7, 4)
    If checkValue(txtBolt2.Text) = False Then
        txtBolt2.BackColor = Color.Red
    End If
    lblA2.Visible = False
    lblA3.Visible = True

    Application.DoEvents()
    If boolEscape = True Then
        MsgBox("You have STOPPED the measurment logging...", vbOKOnly)
        boolEscape = False
        Exit Sub
    End If

我如何将它添加到一个单独的线程,或者将 datareceived 事件标识为更好的方法,因为该行总是相同的长度并且总是以 CRLF 结尾......如果是这样,我修改的有多冷它改为使用它。

一般来说,您应该处理 SerialPortDataReceived 事件。您可以在 ButtonClick 事件中启动任何您喜欢的过程,但这只是启动。 DataReceived 事件将在您的 SerialPort 对象接收到数据然后您处理该数据时引发。如果您希望收到一行数据六次,那么您可以在事件处理程序中调用 ReadLine,该事件可能会引发六次。

不需要启动任何多线程,因为它已经为您完成,即 DataReceived 事件是在辅助线程上引发的,因此您的事件处理程序是在辅助线程上执行的。这意味着您不能直接访问该事件处理程序中的任何控件或从该事件处理程序调用的任何方法。您将需要编组对 UI 线程的调用以更新 UI。这是一个向 SerialPort 发送多个命令并显示响应的简单示例:

Private commands As Queue(Of String)

Private Sub SendNextCommand()
    If commands.Any() Then
        SerialPort1.WriteLine(commands.Dequeue())
    End If
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    commands = New Queue(Of String)({"First", "Second", "Third"})

    SendNextCommand()
End Sub

Private Sub SerialPort1_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
    Dim response = SerialPort1.ReadLine()

    'This event is raised on a secondary thread so we must marshal to the UI thread to update the UI.
    TextBox1.BeginInvoke(Sub() TextBox1.AppendText(response & Environment.NewLine))

    SendNextCommand()
End Sub