View Full Version : parsing a simple integer on serial
01-11-2009, 12: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:
so the full 10bit output would look like:
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 :)
01-11-2009, 01: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?
01-11-2009, 01: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
c := ser.rx ' Get next character
if c < "0" or c > "9" ' Quit on 1st non-digit
result := result * 10 + (c - "0")
Post Edited (Mike Green) : 1/10/2009 7:52:35 PM GMT
01-11-2009, 01: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
i := parseData
all it prints out now is -405? Whats wrong? :/
01-11-2009, 01: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.
01-11-2009, 01: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!
01-11-2009, 01: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....
01-11-2009, 02: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.
01-11-2009, 02:50 AM
Thanks Paul. I've updated the code in the previous message.
01-11-2009, 04: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 :).
01-11-2009, 04:35 PM
thanks a lot it works now :)