Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jon - 04 Mar 2008 14:00 GMT
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[i]=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[i];
    } // 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
Jon - 04 Mar 2008 14:15 GMT
I think I found it. I'll try BitConverter.GetBytes() and
BitConverter.ToSingle().

Thanks,
christery@gmail.com - 04 Mar 2008 15:57 GMT
> Or, is there any other way in C# to convert {4 bytes} <---> {1 float}
> ?
>
> Thank you very much,
> Jon

or a cast? it will not convert anything,, but will go trough the
compiler..

//CY

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.