简体   繁体   English

vb.net中的串行端口数据损坏

[英]Corruption in serial port data in vb.net

I have created an application in vb.net that sends and receives data over serial port continuously. 我在vb.net中创建了一个应用程序,该应用程序通过串行端口连续发送和接收数据。 I form a frame with a few custom parameters needed such as frame number, frame length, actual string. 我形成一个带有一些自定义参数的框架,例如帧号,帧长,实际字符串。 They are separated by special characters like '@', '#', '$' etc. eg @#3$21%Hello.There!!& 它们之间用特殊字符分隔,例如“ @”,“#”,“ $”等,例如@#3 $ 21%Hello.There !!&

I have to test this with minimum 1 ms gap between two consecutive frames. 我必须以两个连续帧之间的最小1毫秒间隔进行测试。 The problem I am facing is, when I receive data in a loop back test, a few characters usually go missing. 我面临的问题是,当我在回送测试中接收数据时,通常会丢失一些字符。 It is counted as a corrupt frame but I am unable to understand why it occurs in loop back test on windows. 它被视为损坏的框架,但我无法理解为什么它在Windows的回送测试中发生。 This frame appears like, @#3$21Hello.The!!& . 该框架看起来像@#3 $ 21Hello.The !!&。 The receive function is like this. 接收功能是这样的。

Private Sub DataReceivedHandler(sender As Object, e As IO.Ports.SerialDataReceivedEventArgs) Handles RXCOMPort.DataReceived

        If e.EventType = SerialData.Chars Then
            If checkFlag = False Then
                '---receive data 
                str &= RXCOMPort.ReadExisting()
                checkFlag = True ' for the thread of processing received data
            End If
        End If
End Sub

Later there will be some micro-controller based hardware which will receive and send data from UART to UART. 稍后将有一些基于微控制器的硬件,它们将从UART接收数据并向UART发送数据。

 If checkFlag = False Then
            '---receive data 
            str &= RXCOMPort.ReadExisting()
            checkFlag = True ' for the thread of processing received data
        End If

What happens if your Handler is executed before you set the checkFlag in your other processing function? 如果在设置其他处理函数中的checkFlag之前执行了Handler,会发生什么情况?

For example, the first Handler callback collects @#3$21%Hello.The 例如,第一个Handler回调收集@#3 $ 21%Hello.The

Then you go process. 然后,您进行处理。 What happens if the Handler is executed BEFORE your processing function is finished with the first batch of data? 如果在处理函数完成第一批数据之前执行处理程序,会发生什么情况? Will the new serial port data be dropped? 新的串口数据会被丢弃吗?

Typically you'd want to put the received data into a circular buffer. 通常,您希望将接收到的数据放入循环缓冲区中。 Do this operation within the receive handler and only this operation. 只能在接收处理程序中执行此操作。

In another function or thread do work on the circular buffer. 在另一个函数或线程中,可以在循环缓冲区上工作。 This way, the handler's job is to ONLY put data in the circular buffer and you won't have dropped data. 这样,处理程序的工作就是仅将数据放入循环缓冲区中,而不会丢失数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM