473,782 Members | 2,443 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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:
012301234FFFFFF xFFFFFFxFFFFFFx

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 4885
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:
012301234FFFFFF xFFFFFFxFFFFFFx

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:

012301234FFFFFF xFFFFFFxFFFFFFx

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.Read Existing()
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:

012301234FFFFFF xyFFFFFFxyFFFFF Fxy

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.Read Existing()
If InStr(Buffer, RS) Then
Dim SplitBuffer() As String
SplitBuffer = Split(Buffer,RS ) 'Split removes the terminator for
you
For I = 0 to SplitBuffer.Len gth - 1
If InStr(SplitBuff er(I), GS) Then
Replace(SplitBu ffer(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
7343
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 is being sent to the PC running SNMPUTIL.EXE, and then finally printed out to stdout like this:
12
5622
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 window and hit a button to process it. The information thus enters the program as a char array. Prices can be between $1 and $100, including cents. So we can have prices such as 3.01, 1.56, 11.57, etc. The char array is an alphanumeric...
9
23607
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 time) b) fgets (read in blocks of whatever size) c) fread (get the size of the file and fread the entire thing into memory) and when would it be appropriate to use either a, b, or c?
5
2998
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 creating xml messages and passing it to a server and server response back with xml. I am capturing this data into a buffer (type char) and dumpinging it into an xml file (which I would like to keep for the sake of history). However, instead trying...
9
11352
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 various fields, some of which are two and four byte integers. In VB6 I used the Mid function to pull data out of the buffer so I could decrypt it (do all that tedius network-order byte-swapping, etc). Quite simply, if I knew that the four bytes from...
8
2944
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 strategy is to do the following: 1) Login to the ftp server using the FTP library in PHP 2) Create a variable that acts like a file handle using Stream_Var in PEAR. 3) Use ftp_fget() to read a remote file into this variable (this is so I don't...
3
17388
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 action="process.dll>
3
4386
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 the file) with the location. And for a particular section I parse only that section. The file is something like, .... DATAS
3
2088
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 std::string. The way I see it I could (assuming infile is a valid std::ifstream) char buffer ; while (!infile.eof () && !infile.bad ()) { // Get the next line in the file infile.getline (buffer, 4095); // If fail bit is set it means we...
0
9643
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10313
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6735
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2875
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.