I’m trying to create a desktop application that receives a stream of serial data in the form of lines of CSV ASCII characters, whereby each line ends with CR+LF. The datastream is generated by an Arduino connected to the serial port. I want to use the data for realtime calculations and displaying on a graph whilst the data continues to come in on the serial port.
I have managed to be able to display the stream of data correctly in a TextArea, including dealing with the CR+LF so that the CSV lines are correctly displayed in the TextArea, one underneath the other. What I cannot seem to do is to now actually use the data for something useful in my program. I need some help with a piece of code that allows me to take these individual lines of serial data, to split them using the comma in the CSV lines, and then to do calculations, etc. with the data.
I have a DataAvailable event handler on my Serial1 Control, which contains (as per tutorial):
if instr(me.LookAhead(encodings.ASCII), chr(13)+chr(10)) > 0 then
SerialDataArea.Text=SerialDataArea.Text+Me.ReadAll(encodings.ASCII)
end if
This results in the SerialDataArea happily displaying each line of CSV ASCII characters as they come in.
How can I now (presumably within the same IF - end IF loop ?) split the individual lines, and then assign each string of characters between commas to a variable that I can perform calculations on ? I have tried to define an array and populate the array with each line, as follows:
if instr(me.LookAhead(encodings.ASCII), chr(13)+chr(10)) > 0 then
SerialDataArea.Text=SerialDataArea.Text+Me.ReadAll(encodings.ASCII)
Dim myArray() As String = split(Me.ReadAll(Encodings.ASCII),",") // Should result in the array containing a set of ASCII characters between the commas ?
Dim PF As String = myArray(0) // Should assign the characters before the first comma to variable PF ?
end if
Unfortunately, the above code leads to an OutOfBoundsException with myArray returning String(-1).
I’d really appreciate a little help with this.