Shop OBEX P1 Docs P2 Docs Learn Events
parsing a simple integer on serial — Parallax Forums

parsing a simple integer on serial

JazzDJazzD Posts: 13
edited 2009-01-11 09:35 in Propeller 1
this is probably very simple again but i did not manage to get it working properly:

I have an atmega sending me ADC values every ~300ms. I'm using the FullDuplexSerial object to receive the data:

the string is constructed like this:

"$VAL%d\n",adcvalue

so the full 10bit output would look like:

$VAL1023(newline)

i would like to receive the data, parse the integer and store the integer in a variable, then do some calculations with the integer (this ADC is used to get a temperature value from an NTC resistor!).

whats a good way of doing this is in spin? thank you [noparse]:)[/noparse]

Comments

  • Paul RowntreePaul Rowntree Posts: 49
    edited 2009-01-10 18:01
    The easiest way is to manually parse off the bits of the string that are not the quantity ($VAL and the newline), then pass the remaining string to the routines of the SimpleNumbers code (in ObEx) which will return a long. Does this code do what you require?

    Cheers!

    Paul Rowntree
  • Mike GreenMike Green Posts: 23,101
    edited 2009-01-10 18:12
    This routine will go through the serial data discarding characters until a "$" is seen.
    It will return a -1 if the "$" is followed by an invalid value (not followed by "VAL" or
    no digits following the "VAL") or it will return the numeric value following the "$VAL".
    There's no checking for overflow of the 32-bit result. Any non-digit will terminate
    the numeric portion of the value.
    pub parseData | c
       repeat until ser.rx == "$"   ' Ignore everything until a new value comes in
       result := -1
       if ser.rx == "V"
          if ser.rx == "A"
             if ser.rx == "L"   ' Make sure next text is VAL
                result := 0
                repeat
                   c := ser.rx   ' Get next character
                   if c < "0" or c > "9"   ' Quit on 1st non-digit
                      quit
                   result := result * 10 + (c - "0")
    

    Post Edited (Mike Green) : 1/10/2009 7:52:35 PM GMT
  • JazzDJazzD Posts: 13
    edited 2009-01-10 18:34
    thanks for the fast reply. right now im using the vga_text demo to display the received data. my main loop looks like this:

    PUB start | i

    text.start(16)
    serial2.start(12,11,0,4800)

    i:= 10

    repeat
    i := parseData
    text.setpos(10,5)
    text.dec(i)

    all it prints out now is -405? Whats wrong? :/
  • Paul RowntreePaul Rowntree Posts: 49
    edited 2009-01-10 18:38
    I suggest you also echo the input text to the vga to ensure that the information is coming in as expected, the serial is properly configured, etc.

    Plus, please use the code paste button to preserve indenting ... you can check if it looks right with previews.

    Cheers!
    Paul Rowntree
  • JazzDJazzD Posts: 13
    edited 2009-01-10 18:40
    okay thanks, well the incoming data is okay, the value should be arround 595. when i print the whole serial2.rx string it shows up correctly!
  • JazzDJazzD Posts: 13
    edited 2009-01-10 18:58
    when i connect the full 5 volts to the ADC pin it receivs $VAL1023 and the parsed value is -8077, so it basically IS parsing something....
  • Paul RowntreePaul Rowntree Posts: 49
    edited 2009-01-10 19:08
    Mike's code sets result to -1 as an error flag, but does not reset it once the lead $VAL information has been received. Once the 'L' has been read, set result to 0, then go into the loop that reads the digits information.

    Cheers!
    Paul Rowntree
  • Mike GreenMike Green Posts: 23,101
    edited 2009-01-10 19:50
    Thanks Paul. I've updated the code in the previous message.
  • JazzDJazzD Posts: 13
    edited 2009-01-10 21:01
    good call, i dont have access to my board right now so i will have another try tomorrow! Thanks guys, spin is giving me a hard time learning it [noparse]:)[/noparse].
  • JazzDJazzD Posts: 13
    edited 2009-01-11 09:35
    thanks a lot it works now [noparse]:)[/noparse]
Sign In or Register to comment.