PDA

View Full Version : parsing a simple integer on serial



JazzD
01-11-2009, 01:55 AM
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 :)

Paul Rowntree
01-11-2009, 02:01 AM
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 Green
01-11-2009, 02:12 AM
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

JazzD
01-11-2009, 02:34 AM
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 Rowntree
01-11-2009, 02:38 AM
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

JazzD
01-11-2009, 02:40 AM
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!

JazzD
01-11-2009, 02:58 AM
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 Rowntree
01-11-2009, 03:08 AM
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 Green
01-11-2009, 03:50 AM
Thanks Paul. I've updated the code in the previous message.

JazzD
01-11-2009, 05:01 AM
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 :).

JazzD
01-11-2009, 05:35 PM
thanks a lot it works now :)