serial string parsing


Closed Thread
Results 1 to 5 of 5
  1. #1
    billybobbins's Avatar
    billybobbins Guest

    Default serial string parsing

    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

  2. #2
    Join Date
    Dec 2003
    Location
    Wichita KS
    Posts
    511


    Did you find this post helpful? Yes | No

    Default

    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
    Ability to Fly:
    Hurling yourself towards the ground, and missing.

    Engineers that Contribute to flying:
    Both optimists and pessimists contribute to the society. The optimist invents the aeroplane, the pessimist the parachute

    Pilots that are Flying:
    Those who know their limitations, and respect the green side of the grass...

  3. #3
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    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.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  4. #4
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    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...

  5. #5
    billybobbins's Avatar
    billybobbins Guest


    Did you find this post helpful? Yes | No

    Talking

    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

Similar Threads

  1. Hserin parsing string...again?
    By kevlar129bp in forum Serial
    Replies: 21
    Last Post: - 9th July 2012, 16:35
  2. Parsing Serial data stream
    By boroko in forum Serial
    Replies: 5
    Last Post: - 26th October 2008, 11:12
  3. Visual Basic 6 & Access 2000
    By Demon in forum Off Topic
    Replies: 33
    Last Post: - 7th September 2006, 04:39
  4. String Parsing
    By eoasap in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 18th February 2006, 17:20
  5. Serial String parsing
    By DynamoBen in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 29th June 2005, 18:11

Members who have read this thread : 2

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts