Reading USB Serial Port

I have a device attached to a USB Port that randomly sends out data from 2 LDR sensors. All data is terminated with CRLF and is formatted as UTF8

In a DataAvailable Serial Event Handler I have this code

[code] Dim my_str As String
Dim x As Integer

my_str = “”

While TestSerial.BytesAvailable > 0
my_str = TestSerial.Read(1, Encodings.UTF8)
Select Case AscB(my_str)
Case 13 ’ OD = CR
Case 10 ’ 0A = LF
TextArea1.AppendText EndOfLine
TextArea1.AppendText Str(my_str)
End Select

Here is a response as taken from TextArea1 when both sensors detect a beam break

The Elapsed Time: is currently calculated on the device connected to the USB Port in fact everything that is in Text Area1 is placed there as a result of the Event Handler via data from the external USB connected device

For data collection and time calculation I would like to grab the value after 'First Line tripped - Reading = ', 'First Line tripped - Time = ', 'Second Line tripped - Reading = ', 'Second Line tripped - Time = ’ for data collection and the calculation.

I have tried to do this inside the Event Handler however I have observed that not all characters have been received in any Event Handler sequence that will be terminated by CRLF

So I am looking at how to grab the information in a manner that will enable to data to come in as it does and then after ‘READY’ is received do the calculations etc that I need to do.


I’d recommend processing each line individually and storing the pertinent values in properties. Append the data as you receive it in a string buffer, then look through the buffer for end of lines. (Split works nicely for this.) Strip off each line as you process it and leave any partial data in the buffer. When you reach a “READY” line, process all the data.

dim s, arr() as string

buffer = buffer + me.ReadAll(Encodings.UTF8)   // buffer is a "global" variable

arr() = Split(buffer, EndOfLine)
for i as integer = 0 to ubound(arr)-1
    s = arr(i)
    // figure out what's in s
buffer = arr(ubound(arr))    // the leftovers - blank if we got a complete line


Thanks for the advise.

This definitely got me going in the right direction