473,320 Members | 1,845 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Parsing Buffer Data

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

Apr 3 '07 #1
2 4820
RG wrote:
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

Apr 3 '07 #2
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.
Apr 3 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: MK | last post by:
I have a Win32 console application (SNMPUTIL.EXE) which listens to incoming SNMP messages: C:\>snmputil trap snmputil: listening for traps... When a trap is generated on a remote server, it...
12
by: BGP | last post by:
I am working on a WIN32 API app using devc++4992 that will accept Dow Jones/NASDAQ/etc. stock prices as input, parse them, and do things with it. The user can just cut and paste back prices into a...
9
by: Mantorok Redgormor | last post by:
If I am parsing a config file that uses '#' for comments and the config file itself is 1640 bytes, and the format is VARIABLE=VALUE, is it recommended to use a) fgetc (parse a character at a...
5
by: jwang | last post by:
I'm currently writing some C code that uses libxml. I've seen several example of parsing xml when the xml are in files. However, I would like to parse the xml from a char buffer. Currently I am ...
9
by: Stargate4004 | last post by:
Hi, I have am converting a VB6 application to VB.net and have run into a problem. The application connects to a TCP port on a Linux box and receives a data buffer. The data buffer contains...
8
by: Eric Anderson | last post by:
I have some files that sit on a FTP server. These files contain data stored in a tab-separated format. I need to download these files and insert/update them in a MySQL database. My current basic...
3
by: Cuong.Tong | last post by:
Greeting, I am writing my own web server and having some problme parsing the the mulitpart/form-data stream that is sent from the browsers. I have a form looks something like this <form...
3
by: toton | last post by:
Hi, I have some ascii files, which are having some formatted text. I want to read some section only from the total file. For that what I am doing is indexing the sections (denoted by .START in...
3
by: Benny the Guard | last post by:
I have a comma delimited file to parse up. So I figured I will go line by line and parse them up. Now efficiency is the top priority here. So I figure I can use the old-school C approach or use...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.