PDA

View Full Version : serial string parsing



billybobbins
- 8th September 2004, 17:50
Hi all.
I have trawled the net and indeed the documentation for any information on how to get data to concatonate

I am using Hserin receive a single character (on an interupt) in the background
which works fine, but I need to get each set of characters to be recognised as a number. E.g

the computer sends a string S1234567E
S representing the start E end bits
If I wanted to put 123 in variable B0 and 4567 in B1 how would I go about this?

I am trying to avoid using any blocking on the part of the serial in as my application is time sensitive. (i.e no Hserin [WAIT("S"),DEC3 B0, DEC4 B1])

looking at some example code, my application is similar to the interupt driver example serbufx.bas

Maybe like a backwards DIG command?

I hope this makes some sort of sense.

Baz

Dwayne
- 8th September 2004, 19:23
Hello Billy,

Billy>>the computer sends a string S1234567E
S representing the start E end bits
If I wanted to put 123 in variable B0 and 4567 in B1 how would I go about this<<

is the stringlength always going to be the same?

a Start . 3char . 4char . End situation?

You could read it in a Array of Character and convert when the array is through...

Something like..


counter=0;
initalize Array[counter] to null.
while counter<>7 and Array[counter]<>"E"
Serin Pin, Array[counter]
counter ++
Wend

if Array[7]!="E" then bad data.
process your data....of good data.

Dwayne

Bruce
- 8th September 2004, 19:51
myVar VAR byte[32] ' Array for inbound serial data
X VAR BYTE ' Holds "S" start position
Y VAR BYTE ' Holds "E" end position
Index VAR BYTE ' Index pointer
B0 VAR WORD
B1 VAR WORD

Begin:
HSERIN [STR myVar\32\13] ' Terminate with CR

Get_Start: ' X will hold the start "S" position
FOR X = 0 TO 31
IF myVar[X] = "S" THEN Get_End
NEXT X
GOTO BEGIN

Get_End: ' Y will hold the end "E" position
FOR Y = X+1 TO 31 ' Find "E"
IF myVar[Y] = "E" THEN Done
NEXT Y
GOTO BEGIN

Done: ' Display start/end positions
HSEROUT ["Start = position ",DEC X,13,10]
HSEROUT ["End = position ", DEC Y,13,10]

Now you can do whatever you need to with the remaining serial data. You know where it starts & ends in the array.

There are many ways to do this. Here's one simple example.

' myVar[X+1] to myVar[Y-1] should be holding valid characters

' Loose_Decimals just subtracts ASCII 0 from each
' ASCII value & returns the decimal result

Loose_Decimals:
FOR Index = X+1 to Y-1 ' Increment X to 1st character
myVar[Index] = (myVar[Index]-"0") ' Convert to decimal
NEXT Index

Sort_B0:
B0 = myVar[X+1] * 100 ' 1st character after "S"
B0 = B0 + myVar[X+2] * 10
B0 = B0 + myVar[X+3]
HSEROUT ["B0 = ",DEC B0,13,10]

Sort_B1:
B1 = myVar[X+4] * 1000 ' 4th character after "S"
B1 = B1 + myVar[X+5] * 100
B1 = B1 + myVar[X+6] * 10
B1 = B1 + myVar[X+7]
HSEROUT ["B1 = ", DEC B1,13,10]
GOTO Begin

END

I sent the following serial data to the PIC with MicroCode Studio's terminal program;

Note: Be sure you have the "parse control characters" button selected in the MicroCode Studio terminal program. This sends the #13 or CR which terminates waiting for the entire 32 byte string, and allows program flow to fall through to your parsing routines.

Hello S1234567E #13

Here's the return data from the PIC;

Start = position 6 ' <-- S position
End = position 14 ' <-- E position
B0 = 123
B1 = 4567

Start position 6 + 1 or array element myVar[7] is your 1st valid character. End position 14 - 1 myVar[13] is your last. 6 holds the S, and 14 holds the E so everything in between 6 to 14 or myVar[7] to myVar[13] is assumed valid data with your paticular scenario.

This should be enough to get you started. If you know the beginning & ending ASCII characters, it's simple to extract whatever data is in between.

Melanie
- 8th September 2004, 21:18
Bet you didn't do a search on the word "parsing" here on this forum...

See Code Examples... Communications Example : PC to PIC bi-directional dialogue. Example reads in a serial string (byte at a time) and acts on it...

billybobbins
- 8th September 2004, 21:34
Thanks Bruce, this should do nicely. I didn't realise it would be so simple!

I'll give it a go when I get to work tomorrow.

Thanks very much.

I did search for parsing; pretty much one of the first text strings I did search, but could not parse the text well enough to see the offending lines of code:
DataA=DataA*10+CounterB

In my stupidity and google related exhaustion I missed that (been trawling the net for hours). I dismissed it as I did many other snippets of code. Sorry!

Again thanks Melanie for all of the insightful snippets on this forum!
Baz