As Yan said, it's not guaranteed to be portable, but it is very
practical to directly move floats back and forth between a PC and an
embedded system.
The good news is that nowadays most systems use IEEE standard floating
point representation. I routinely move floats from PCs to AVR and ARM
embedded systems without translation.
Using the following code (compiled under MS Visual Studio 2005 as a
WIN32 Console application) and on a TS-7200 NetBSD 3.0/GCC I get
identical results:
Int: 44 33 22 11
1.5: 00 00 c0 3f
3.3: 33 33 53 40
10.0: 00 00 20 41
This says to me that the systems use the same floating point formats and
endian-ness.
As for alignment, the Microsoft C Compiler has pragmas that allow
practically any structure packing. At the worst, you might end up
putting pad bytes into a structure on one side or the other. In C and
C++ there are almost always some way to control structure packing -
historically it was common to define a structure to actually match the
register layout of arbitrary hardware devices which often had
"unfriendly" layouts.
In any case, when you are unsure of structure packing or layout, get
creative and print out the memory contents and see what you actually
have. There is nothing "magic" in a float, it's a pile of bits like
anything else.
Enjoy,
Bill Landolina
------------ sample detective program ---------------
// FloatInfo.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
struct interop
{
int i;
float x;
float y;
float z;
};
void print4(char *tag, void *p)
{
unsigned char *cp = (unsigned char *) p;
printf("%s: %02x %02x %02x %02x\n",
tag, cp[0], cp[1], cp[2], cp[3]);
}
int _tmain(int argc, _TCHAR* argv[]) // I changed this to main() etc on
ARM/GCC
{
interop s;
s.i = 0x11223344;
s.x = (float) 1.5;
s.y = (float) 3.3;
s.z = (float) 10;
print4("Int: ", &s.i);
print4("1.5: ", &s.x);
print4("3.3: ", &s.y);
print4("10.0: ", &s.z);
getchar();
return 0;
}
-----Original Message-----
From: On Behalf
Of Yan Seiner
Sent: Sunday, February 18, 2007 10:47 AM
To:
Subject: [ts-7000] Re: transferring floating point data from PC to
TS7300
--- In "Oguz Dilmac" <> wrote:
>
> Hi all,
>
> I'm trying to pass some data from my pc to ts7300 via Ethernet.
> It seems floating point formats of EP9302 and PC are different from
> each other.
>
> In my program, I have a struct with both floating point and integer
> variables. I simply send this data to ethernet from PC. On the arm
> side, a user program gets the data from socket and put it to a FIFO to
> pass to a kernel module. Then I print them with rt_printk.
>
> When I dmesg, Integer parts are OK. But float parts are garbage.
>
I looked at doing this. I even had a discussion of this on either the
gcc list or the general C list; I forget.
The gist of the issue is this:
While in theory you could get it to work, in practice it is
unmaintainable.
Structs are created by the compiler, and as such, the various byte
padding schemes and memory alignment schemes can vary from compiler to
compiler, between compiler versions, and possibly even between
compiler runs, so there's no guarantee that it will work even on the
same hardware platform.
Now throw in the problem of endiannes, and the different float
formats, and the fact that on some architectures ints are 2 bytes, on
some 4 bytes, and if you're really using off-the-wall stuff, 36 bits.
In other words, it cannot be done.
I ended up normalizing everything into 2 byte big-endian ints, and
writing routines that pack and unpack the data. (See PHP's
pack/unpack functions.)
Look into the modbus spec for a truly portable (but not easy to
implement) way to transfer data between arbitrary architectures.
--Yan
Yahoo! Groups Links
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/ts-7000/
<*> Your email settings:
Individual Email | Traditional
<*> To change settings online go to:
http://groups.yahoo.com/group/ts-7000/join
(Yahoo! ID required)
<*> To change settings via email:
<*> To unsubscribe from this group, send an email to:
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
|