Results 1 to 6 of 6

Thread: problems whit memory and structs

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2011
    Posts
    2

    problems whit memory and structs

    hello, C++ programer here.
    i'm having some problems with a deivce i made with a microcontroller.
    the thing is that the device sends the settings and sensors' values over serial to the pc, and i need to be able to understand those values which are a dump from the memory. uint8_t to uint32_t are fine to convert by hand, troublesome but can be done but when it gets to the floats and doubles it's imposible.
    I've done some VB.Net before, i'm a little rusty but still remember it. Anyway, i never got to do this kind of stuff.
    So, how should i do to copy the memory from the serial port buffer to the strutc so that i can have all the variables in place?
    the strutc is like
    Code:
    struct blablablah
    {
        uint8_t  a;
        uint16_t b
        uint8_t  c;
        float     d;
    };
    i googled it but coulnd't find something like this....any idea?
    thanks

  2. #2
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: problems whit memory and structs

    a structure in vb.net would look like this:
    Code:
     Public Structure blablablah
            Dim a As UInt16 'I don't know if there is a unit8 var in Vb.net
            Dim b As UInt16
            Dim c As UInt16
            Dim d As Double
        End Structure
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: problems whit memory and structs

    Quote Originally Posted by opus View Post
    a structure in vb.net would look like this:
    Code:
     Public Structure blablablah
            Dim a As UInt16 'I don't know if there is a unit8 var in Vb.net
            Dim b As UInt16
            Dim c As UInt16
            Dim d As Double
        End Structure
    'I don't know if there is a unit8 var in Vb.net - yes, it is Byte.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: problems whit memory and structs

    Don't you need to throw in some LayoutKind.Sequential attributes on that struct if you're trying to match the memory layout to a certain set of memory? (Disclaimer: I've never had to do this, so may be way off here...)

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: problems whit memory and structs

    I think you'll find that 'float' actually maps to 'Single' rather than 'Double'. I haven't used C++ for a long time but that's the case in C# so I expect it would be.

  6. #6

    Thread Starter
    New Member
    Join Date
    Nov 2011
    Posts
    2

    Re: problems whit memory and structs

    sorry for the BIG delay, i was busy with life and then just had some vacations.

    thanks for the help, that and some stuff i found in internet helped me but i now have another problem with the struct, and that is that i cant copy it to and from a byte array

    now it looks like c++ but does not work the same way.
    It works with non array variables but arrays are just messed up and throw weird runtime errors, or simply don work

    Code:
     <StructLayout(LayoutKind.Explicit, CharSet:=CharSet.Ansi)> _
        Public Structure Test
            <FieldOffset(0)> Dim Var1 As Byte
            <FieldOffset(1)> Dim Var2 As Byte
            <FieldOffset(2), MarshalAs(UnmanagedType.ByValArray, SizeConst:=10)> Dim Var3() As Byte
    
        End Structure
    Var3 just wont load
    this inside a botton
    Code:
            'dies before entering ranting about not being able to load a tipe because of misalignment and overlapping at position 2 (var3)
    
            Dim Tst As Test ' Stuctuer variable
            Dim ByteArray() As Byte
            
            Tst.Var1 = 255
            Tst.Var2 = 7
            bytecpy(Tst.Var3, StrToByteArray("0123456789"), 10)
            ' function of mine, works the same as memcpy() from string.h, dst,src,n
    
            Dim Ptr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(Tst))
            ReDim ByteArray(Marshal.SizeOf(Tst) - 1)
    
            Marshal.StructureToPtr(Tst, Ptr, False)
            Marshal.Copy(Ptr, ByteArray, 0, Marshal.SizeOf(Tst))
    
            Marshal.FreeHGlobal(Ptr)
    it's incredible that i cant find anyone in the whole internet that has done something similar to me

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width