My program reads a serialport data packet stream from a remote device. The serialport read is event driven:
The data packet received is fixed length string where each character ascii value represents a range of device state conditions, e.g. Power level ranging from ASC(#) to ASC(z) and then my code processes and displays the interpreted values onto a form as a first step in a polynomial regression analysis of the data. The remote device sends new data packets only when some device internal state has changed or is changing in real-time. So it may send out one packet as a device 'state' snapshot and then nothing for few seconds, or at other times a stream of dozen packets within a fraction of a second can appear. With each of these events, I need some method to wait a minimum time, e.g. 0.3 seconds, after the first packet is received to see if others will follow immediately and if not then to just use the 1 or few packets received and average them.
I have been using the following function (on Module level) for fractions of a second pause but sometimes it misses and clips a cluster of packets too soon. I suspect this is because the Wait function is not on the same thread as the serialport. Can someone advise the best coding method for my circumstance??
Code:
Private Sub SerialPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort.DataReceived
I have been using the following function (on Module level) for fractions of a second pause but sometimes it misses and clips a cluster of packets too soon. I suspect this is because the Wait function is not on the same thread as the serialport. Can someone advise the best coding method for my circumstance??
Code:
Public Sub Wait(ByVal Seconds As Double, Optional ByRef BreakCondition As Boolean = False)
Dim l_WaitUntil As Date
l_WaitUntil = Now.AddSeconds(Seconds)
Do Until Now > l_WaitUntil
If BreakCondition Then Exit Do
System.Windows.Forms.Application.DoEvents()
Loop
End Sub