1

我有一个 VB.NET 4 应用程序,它通过 UART 从 PIC24 微控制器接收 10 个字节的 ASCII 数据。这是我的接收功能:

Private Sub mySerialPort_DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)
        'Handles serial port data received events 
        UpdateFormDelegate1 = New UpdateFormDelegate(AddressOf UpdateDisplay)
        Dim n As Integer = mySerialPort.BytesToRead 'find number of bytes in buf 
        comBuffer = New Byte(n - 1) {} 're dimension storage buffer 
        mySerialPort.Read(comBuffer, 0, n) 'read data from the buffer 
        Me.Invoke(UpdateFormDelegate1) 'call the delegate
    End Sub  

这是我得到这段代码的地方

我使用以下代码解析并将接收到的数据分配到它需要去的地方:

    Private Sub UpdateDisplay()
        Dim i As Integer
        Dim RX_String(15) As String
        Dim RX_String_Hex(15) As String
        Dim Message As String

        For i = 0 To comBuffer.Length - 1
            RX_String(i) = Chr(comBuffer(i))
        Next
        Message = String.Join("", RX_String)
        TextBox1.Text = Message
    End Sub

当我启动我的应用程序时,我打开了串行端口。然后我告诉微控制器发送数据,即1234567890在我的 VB 应用程序上,它会显示诸如124567890等之类的内容,直到最终在微控制器进行随机数传输后显示完整的数据字符串。我以前从未在 VB 中进行过串行操作,所以我一定会错过显而易见的事情。我是否遗漏了一些阻止我每次接收全部 10 个字节的东西?

4

1 回答 1

1

这是 DataReceived 事件处理程序的一些通用代码

Dim dataByts As New List(Of Byte)
Dim dataLock As New Object
Dim datarcvd As New Threading.AutoResetEvent(False)

Private Sub SerialPort1_DataReceived(sender As Object, _
                                     e As IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
    Dim br As Integer = SerialPort1.BytesToRead '# of bytes to read
    If br > 0 Then
        Dim b(br - 1) As Byte 'create buffer to read into
        Try
            br = SerialPort1.Read(b, 0, b.Length) 'read the bytes
            If br < b.Length Then 'adjust length if required
                Array.Resize(b, br)
            End If
            'add bytes just read to list
            Threading.Monitor.Enter(dataLock)
            dataByts.AddRange(b)
            Threading.Monitor.Exit(dataLock)
            datarcvd.Set() 'signal event fired
            '
            'check for a condition
            '
            If dataByts.Count >= 10 Then
                'condition met <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            End If
            '
            'fine tune exception handling
            '
        Catch ex As Exception
            Debug.WriteLine(ex.Message)
        End Try
    End If
End Sub

Private Sub SerialPort1_ErrorReceived(sender As Object, _
                                      e As IO.Ports.SerialErrorReceivedEventArgs) _
                                  Handles SerialPort1.ErrorReceived
    Debug.WriteLine(e.EventType)
End Sub

当您有 10 个字节(或更多)时,调用委托并解码字符串。将字节放入缓冲区时请注意锁定。代表应该是这样的

    Dim s As String = ""
    Threading.Monitor.Enter(dataLock) 'yes
    s = SerialPort1.Encoding.GetChars(dataByts.ToArray, 0, 10)
    dataByts.RemoveRange(0, 10) 'remove the bytes processed
    Threading.Monitor.Exit(dataLock)
    TextBox1.Text = s
于 2013-06-16T13:54:07.810 回答