Parsing Buffer Data

R

RG

I am having trouble parsing the data I need from a Serial Port Buffer.

I am sending info to a microcontroller that is being echoed back that
I need to remove before I start the actual important data reading.

For instance this is my buffer string:
012301234FFFFFFxFFFFFFxFFFFFFx

Where the FFFFFF is my Hex data I need to read. I am using the "x" as
a separater as I was having problems using the VbCrLf. But I think
that was because the actual stream of data I need has to be filtered
on the front end.

The 012301234 intergers are values being sent to the microcontroller
and echoed back to VB serialport buffer. The 4 is the number I use to
start the HEX data stream I need. I am using a Select Case with "4"
being the case to start the stream. This is probably not a good idea
becuase it could also be found in the Hex Data Stream using the Instr
command.

This is what I have so far but it is not working correctly

Dim TerminatorPos As Integer
TerminatorPos = InStr(Buffer1, "x")

If vboutput = 4 Then
Buffer1 = Buffer1.Remove(0, TerminatorPos) ' Trying to
remove everything before the "x"
End If

If TerminatorPos > 0 Then
hexbuffer1 = Mid(Buffer1, 1, TerminatorPos - 1)
buffer1_Dec = Convert.ToInt32(hexbuffer1, 16)' This is
where the errors occur because I amnot getting a true hex value to
convert based on the data being parsed when the program starts.
Buffer1 = Mid(Buffer1, TerminatorPos + 1)
End If
 
M

Marco Trapanese

RG said:
I am having trouble parsing the data I need from a Serial Port Buffer.

I am sending info to a microcontroller that is being echoed back that
I need to remove before I start the actual important data reading.

For instance this is my buffer string:
012301234FFFFFFxFFFFFFxFFFFFFx

Where the FFFFFF is my Hex data I need to read. I am using the "x" as
a separater as I was having problems using the VbCrLf. But I think
that was because the actual stream of data I need has to be filtered
on the front end.

The 012301234 intergers are values being sent to the microcontroller
and echoed back to VB serialport buffer. The 4 is the number I use to
start the HEX data stream I need. I am using a Select Case with "4"
being the case to start the stream. This is probably not a good idea
becuase it could also be found in the Hex Data Stream using the Instr
command.

You might do the following:

define three chars: start, separator and terminator. Use the first to
start receiving data frame and stop when receive the terminator. Then
just do a split with the terminator char.

This can be done if you send out hex values as string (of course choose
control chars out of 0-9/A-F). If you transmit actual values
(chr$(value)) you need another way to achieve this, such as a data
lenght field.

I hope I gave you some ideas... and sorry for my poor English.

Bye!
Marco / iw2nzm
 
D

Dick Grier

Hi,

It is strange that you had trouble with vbCrLf. I use this all the time as
a delimiter. I do use ReadExisting, not ReadLine, and then parse the data.
It appears that you are using ASCII (two characters per byte) to send your
data, so a standard ASCII (non-printing character) that might be used is
RS - RecordSeparator (Chr(30)). For example if you sent this stream:

012301234FFFFFFxFFFFFFxFFFFFFx

where x = Chr(30), then received it like this (I am assuming the
DataReceived event):

Static Buffer As String
Dim RS As String = Chr(30)
Buffer += SerialPort.ReadExisting()
If InStr(Buffer, RS) Then
Dim SplitBuffer() As String
SplitBuffer = Split(Buffer, RS) 'Split removes the terminator for
you
Buffer = ""
'now, SplitBuffer is an array that contains each field sent
'so process it here
End If

However, there is a potential design issue with this code fragment. The
last array entry (record) may be partial. That is, the way that Split works
is the last item in the array contains any leftover data, even if that data
didn't have the required terminator. To solve this issue (and to keep the
code quite simple, you could replace the RS character with two characters
(RSRS, for example). Then, the data sent would look like this:

012301234FFFFFFxyFFFFFFxyFFFFFFxy

where x = GS and y = RS

The parsing code would be similar:

Static Buffer As String
Dim RS As String = Chr(30)
Dim US As String = Chr(31)
Buffer += SerialPort.ReadExisting()
If InStr(Buffer, RS) Then
Dim SplitBuffer() As String
SplitBuffer = Split(Buffer,RS) 'Split removes the terminator for
you
For I = 0 to SplitBuffer.Length - 1
If InStr(SplitBuffer(I), GS) Then
Replace(SplitBuffer(I), GS, "") 'remove the GS delimiter
Process (SplitBuffer(I)) 'you write this routine ot use
each record
'in the array
Else
Buffer = SplitBuffer(I) 'retain any left-over data
End If
Next
If InStr(Buffer, GS) Then Buffer = "" 'some more cleanup
End If

All of this is "air code." That is, I wrote it, but didn't do any testing.
That part is up to you

There are lots of possible ways to create a "regular" data stream that lends
itself to parsing. This is only one. The really critcial thing to remember
is that the data that you are attempting to parse may not be complete.
Data takes time to be sent and received, and your process will be much
faster than the data arrival. So, you must retain any unprocessed data
between cycles so that it isn't lost.

Dick

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top