moving floats between {C# on PC} and {C++ on ARM7 MCU}

J

Jon

Hi

I need to move "floats" (single precision) between a C# application in
a PC and a C++ program running on an embedded system. The C++ program
has been compiled with Rowley Associates CrossStudio 1.7 build 4,
which uses GNU C/C++ compiler GCC, and runs on an NXP LPC2103 with
ARM7 core.

On the C++ side, I have no problems sending/receiving floats. It is on
the C# side, where I have problems trying to imitate what in C++ is a
union.

I have tried the following:
// ...................................................
// These "using" are the C# equivalent ones to "typedef" in C++.
using u08 =System.Byte;
using u16 =System.UInt16;
using u32 =System.UInt32;
using u64 =System.UInt64;

using i08 =System.SByte;
using i16 =System.Int16;
using i32 =System.Int32;
using i64 =System.Int64;
// ...................................................
[StructLayout(LayoutKind.Explicit)]
public struct S_u08_flo
{
[FieldOffset(0)]
public float flo;
[FieldOffset(0)]
public u08[] u08_array;
}
// ...................................................
// float = single precision IEEE floating point (4 bytes)
public float ReadData_flo()
{
S_u08_flo u;
u.u08_array =new u08[4];
u.flo =0.0F;
for (int i=0;i<4;i++)
{
u.u08_array=u08_data[u16_idx++];
} // for
return(u.flo);
}
// ...................................................
// float = single precision IEEE floating point (4 bytes)
public void WriteData_flo(float flo_data)
{
S_u08_flo u;
u.u08_array =new u08[4];
u.flo =flo_data;
for (int i=0;i<4;i++)
{
u08_data[u16_idx++]=u.u08_array;
} // for
}
// ...................................................


and it compiles ok but, at run time, it says


// ...................................................
System.TypeLoadException was unhandled
Message="Could not load type 'S_u08_flo' from assembly 'Pyramid_USB,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' because it
contains an object field at offset 0 that is incorrectly aligned or
overlapped by a non-object field."
Source="Pyramid_USB"
TypeName="S_u08_flo"
StackTrace:
at N_Pyramid.C_PKT2.ReadData_flo()
// ...................................................



Any hint on what may I be doing wrong? It looks like it is complaining
about the overlapping between the two fields in the structure, but I
do need that overlapping to simulate a union.

Or, is there any other way in C# to convert {4 bytes} <---> {1 float}
?

Thank you very much,
Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top