I am trying to send python float's over UART to an Embedded c processor, the MKE14 from NXP. In python I am using the Struct library to make a 32 bit float and send this over UART. I checked both float impelementations and there both "IEEE-754".
I send Decimal: 5.490715 Hex: 0x40, 0xAF, 0xB3, 0xF0
I receive in "Longitude" Decimal: 1085256688 Hex: 0x40, 0xAF, 0xB3, 0xF0
After float conv Decimal: 1085256704 Hex: 0x40, 0xAF, 0xB4, 0x00
It must be some conversion problem, I receive the right packet but the representation is tottaly different.
The used code on both systems are in the attached file.
You cannot convert an integer bitpattern to a float
by means of a cast. That just converts the integer to the nearest floating-point number, it does not re-interpret the bits as being a float
. If it worked like you think, this:
const float x = 512;
would set x
to 7.175E-43
, which would not be very convenient.
The best way is probably to just copy the bits, so replace
SUS_Data.Longitude = ((float)Longitude);
by:
memcpy(&SUS_Data.Longitude, &Longitude, sizeof SUS_Data.Longitude);