Keil™, An ARM® Company

Discussion Forum

How Can I sent to serial port a float?

Next Thread | Thread List | Previous Thread Start a Thread | Settings

DetailsMessage
Author
javi pks
Posted
4-Jul-2008 04:35
Toolset
C51
New! How Can I sent to serial port a float?

Hi!
In my project i sent to serial port a int :

unsigned int X;
        putchar(X);
        putcahr(X>>8);

and everything to work OK!!

but if i want change my data type and use float, i cant do

putchar(X>>8)

.
I trying it:

float X _at_ xxxxx;


and with a function i read all adress of my data but it no work.

Any idea to do it??
thanks

Author
Ulf Saß
Posted
4-Jul-2008 05:18
Toolset
C51
New! RE: How Can I sent to serial port a float?

I'm using a union for that. The PC program has to restore the data to its own float format (it is very easy if both use for example IEEE754 format).

Author
Ulf Saß
Posted
4-Jul-2008 05:37
Toolset
C51
New! RE: How Can I sent to serial port a float?

You did not mention the type of serial communication: ASCII or binary. Sending the four bytes of the IEE754 standard is "binary" type. You will have to create some kind of handshaking or protocol.
Maybe it's easier to use printf() and ASCII.

Author
javi pks
Posted
7-Jul-2008 01:49
Toolset
C51
New! RE: How Can I sent to serial port a float?

OK,
But if my data have 4 bytes, if i have for example:

      float x;
      x = Operation();


How can i send such byte?? (to break up the data).

Thanks

Author
Tamir Michael
Posted
7-Jul-2008 02:01
Toolset
C51
New! RE: How Can I sent to serial port a float?

all you need to do is read your manual to find out how floating point numbers are represented in your architecture and then pick the right pieces from your number when you log it (exponent, mantissa).

Author
kalib rahib
Posted
7-Jul-2008 02:04
Toolset
C51
New! RE: How Can I sent to serial port a float?

mikhel,,,,

i is being missing you and you're good advises,,,, yes

i is hoped you be goodly holidey????

Author
Per Westermark
Posted
7-Jul-2008 02:10
Toolset
C51
New! RE: How Can I sent to serial port a float?

Either send the floating point number as ASCII, i.e. the result of printf(), sprintf(), ...

Or make an experiment if you can transfer the four raw bytes (possibly with a byte swap) and have the PC pick up the result.

If the numbers are following the IEEE standard, then they should be stored in a compatible way so it should be possible to set a byte pointer to the address of the float and then send four bytes.

This gives the smallest code and maximum precision in the transfer but you need to define in which order the bytes arrive so the PC program knows what to do.

Author
javi pks
Posted
7-Jul-2008 02:32
Toolset
C51
New! RE: How Can I sent to serial port a float?

Thanks Per!!

Author
Tamir Michael
Posted
7-Jul-2008 02:42
Toolset
C51
New! RE: How Can I sent to serial port a float?

Hello my dear kalib rahib,
Yes it was nice but on my flight back I had to suffer the presence of a couple of ladies that kept on bitching and screaming during take-off and landing. I called one of them a twit, by the way. That was fun! :-)

Next Thread | Thread List | Previous Thread Start a Thread | Settings