View Full Version : get float ?

Tesla trooper
05-01-2008, 08:09 PM

I have been trying to read values from a digital mutimeter. The values comes in scientific form, eg. 3,2E+3. Cant find any suitable way to do this...
Is there a "getfloat" command, or something similar?

agi.get ???(agiVal)

agiVal := (flo.FloatToString(agiVal))
agiVal := (vfd.StrToDec(agiVal))


Thanks in advance!

Paul Baker
05-01-2008, 11:58 PM
Hello, when you say you receive values in scientific form, do you mean you get a 32 (or 64) bit value that is IEEE single (double) floating point, or are you receiving the ASCII version of the number ("3" "," "2" "E" "+" "3")?

Paul Baker (mailto:pbaker@parallax.com)
Propeller Applications Engineer
[/url][url=http://www.parallax.com] (http://www.parallax.com)
Parallax, Inc. (http://www.parallax.com)

Tesla trooper
05-02-2008, 03:13 AM
Hi! I can read the values in hyperterminal, so it should be ASCII

Paul Baker
05-02-2008, 06:07 AM
Ok, do you do any mathmatical operations on the data, or are you only looking to display the value?

Paul Baker (mailto:pbaker@parallax.com)
Propeller Applications Engineer
[/url][url=http://www.parallax.com] (http://www.parallax.com)
Parallax, Inc. (http://www.parallax.com)

Tesla trooper
05-02-2008, 03:55 PM
Yes. We collect the data from a digital multimeter. The instrument is sending a value every ~.5 seconds. The idea is that I store the floating point number in a variable ei. "agiVal" in the code I posted. I then use the command "floattostring" to transform the floating point number to a string, which I in turn transform into a decimal number using the command "strtodec". I then intend to use this variable in some simple mathematical operations and display the result on a VFD display.

However, I can't seem to find any command to collect this floating point number from the multimeter.

Paul Baker
05-07-2008, 01:15 AM
You will need to implement a str2float command. To do this you will need to perform the following psuedo-code:

Str2Float(str_ptr) : float_n
'test BYTE[str_ptr]
'if == '-' set negative flag for mantissa then increment str_ptr
'set mantissa equal to BYTE[str_ptr] - "0" then increment str_ptr by 2 (skipping decimal place)
'repeat until BYTE[str_ptr] == "E"
'mantissa := (mantissa * 10) + BYTE[str_ptr] - "0" (shift mantissa and add next decimal place)
'increment str_ptr
'str_ptr++ (skip past E)
'if BYTE[str_ptr] == '-' set negative flag for exponent, increment str_ptr
'exponent := BYTE[str_ptr] - "0"
'if BYTE[str_ptr] <> 0
'exponent := (exponent * 10) + BYTE[str_ptr] - "0"
'Now you have 4 variables filled: mantissa, exponent and the negative flags for each
'if a negative flag is set, perform the two's compliment of the corresponding value
'pack a 32 bit integer (variable float_n) with the exponent and mantissa fields according to IEEE-754 standard

After the routine is performed you will have a floating point representation of the string

Paul Baker (mailto:pbaker@parallax.com)
Propeller Applications Engineer
[/url][url=http://www.parallax.com] (http://www.parallax.com)
Parallax, Inc. (http://www.parallax.com)

Post Edited (Paul Baker (Parallax)) : 5/6/2008 6:20:58 PM GMT

Tesla trooper
05-21-2008, 06:07 PM
I actually found a similar program to yours in an other post which I modified to serve my purposes. It turned out that the problem was that the digital multimeter sent a linefeed in the end of each string, however, rxflush made it all work out the way it it was supposed to. But of course, by solving one problem a new one appeared. This is an extraction of my current code:


stf.StringToFloat(agiVal, @vfdVal)


vfdVal := fmath.FSub(vfdVal, tara)



Problem is, "tara" is supposed to be a "calibration-value". I am writing this program for someone, the idea is that he can tune the sufficient calibration and then turn the propeller off. And when he switches it on again the tara-value is still the same as it was when he turned the chip off, and hence he doesn't have to recalibrate it every time he starts up propeller chip. My spontaneous idea was to store this tara-value in the EEPROM, it's just that I can't seem to find any simple way to do that... Is there a way to achieve this?

Mike Green
05-21-2008, 07:58 PM
Download "Simple_I2C_Driver" from the Propeller Object Exchange and read the object's comments. If you're using a Protoboard or other board with an EEPROM > 32K, you can store the value at a location above 32K. If there's only 32K of EEPROM, you can still store the value at the "end" of EEPROM which normally isn't used, but, when you download a new program, the value will be erased.

Peter Verkaik
05-21-2008, 08:28 PM
If you receive values in the form "x,yEz" where x is the integral part, y is the fractional part, z is exponent,
you can use the Format object function bscanf

· fmt: "Format"
k var long
buf var byte[10] 'assumed to hold received value string
i var long
f var long
e var long

k := fmt.bscanf(@buf,0,string("%d"),@i)· 'convert integer part, reads up to the decimal point (comma)
k := fmt.bscanf(@buf,k,string(",%d"),@f)· 'convert fractional part, skips the decimal point·(comma)
k := fmt.bscanf(@buf,k,string("E%d"),@e)· 'convert exponent, skips the E

Both i and e can be positive, zero or negative, f can be zero or positive.
Now you just need to combine i, f and e into a floating point number.

You will find the Format object in the object exchange under Tools.

regards peter