-
Nov 7th, 2011, 07:40 PM
#1
Thread Starter
New Member
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
-
Nov 8th, 2011, 07:36 AM
#2
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!
-
Nov 8th, 2011, 07:40 AM
#3
Re: problems whit memory and structs
Originally Posted by opus
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.
-
Nov 8th, 2011, 06:15 PM
#4
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...)
-
Nov 8th, 2011, 08:09 PM
#5
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.
-
Nov 26th, 2011, 05:58 PM
#6
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|