串行端口数据接收的事件处理程序仅在调试模式下工作

时间:2013-08-07 14:38:09

标签: .net vb.net

这很奇怪。我的串口的事件处理程序似乎只在调试模式下工作。问题是,在硬件方面,RX和TX引脚是连接的,这意味着它们共享相同的数据线。在这种情况下,每次发送后都会触发2个DataReceived事件。第一个是命令本身,第二个是我需要的答案。这是代码部分:

Private Sub mspSerialPort_DataReceived(ByVal sender As Object, _
                                       ByVal e As SerialDataReceivedEventArgs) _
                                 Handles mspSerialPort.DataReceived
    'handles serial data received events
    Dim iNum As Integer = mspSerialPort.BytesToRead
    UpdateFormDelegate1 = New UpdateFormDelegate(AddressOf UpdateDisplay)

    If iNum > 0 Then
        ReDim mbReceiveBuffer(iNum - 1)
        mspSerialPort.Read(mbReceiveBuffer, 0, iNum)

        If iNum > 1 Then
            Me.Invoke(UpdateFormDelegate1)
        End If
    End If

End Sub

If条件“iNum> 1”仅用于忽略第一个接收字节,即命令本身。在调试模式下一切正常,但一旦程序在正常模式下运行,第二个事件中的接收字节将不会显示。 UpdateDisplay函数如下所示:

Private Sub UpdateDisplay()
    'display received data
    msReceiveBufferString = ""
    For i = 0 To UBound(mbReceiveBuffer)
        msReceiveBufferString &= (mbReceiveBuffer(i).ToString("X2") & " ")
    Next

    Select Case mbTestIDFlag
        Case gbTEST_ID_02      'SW-ID
            txtSWID.Text = msReceiveBufferString
        Case gbTEST_ID_2F      'Trim value
            txtTrim.Text = msReceiveBufferString
        Case Else
            'do nothing
    End Select

End Sub

有没有人有任何想法为什么不起作用?是因为委托函数,还是事件处理程序,还是其他?非常感谢提前!

0 个答案:

没有答案