68 lines
1.3 KiB
C#
68 lines
1.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Runtime.InteropServices;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace DroneClient
|
|
{
|
|
internal class Drone
|
|
{
|
|
public struct DataOut
|
|
{
|
|
public float AccX, AccY, AccZ;
|
|
public float GyrX, GyrY, GyrZ;
|
|
public float PosX, PosY;
|
|
public float LaserRange;
|
|
}
|
|
|
|
public struct DataIn
|
|
{
|
|
public float MotorUL, MotorUR, MotorDL, MotorDR;
|
|
}
|
|
|
|
public static byte[] getBytes(object data)
|
|
{
|
|
int size = Marshal.SizeOf(data);
|
|
byte[] arr = new byte[size];
|
|
|
|
IntPtr ptr = IntPtr.Zero;
|
|
try
|
|
{
|
|
ptr = Marshal.AllocHGlobal(size);
|
|
Marshal.StructureToPtr(data, ptr, true);
|
|
Marshal.Copy(ptr, arr, 0, size);
|
|
}
|
|
finally
|
|
{
|
|
Marshal.FreeHGlobal(ptr);
|
|
}
|
|
return arr;
|
|
}
|
|
|
|
public static object fromBytes(byte[] arr, Type type)
|
|
{
|
|
object mem = new object();
|
|
|
|
int size = Marshal.SizeOf(type);
|
|
IntPtr ptr = IntPtr.Zero;
|
|
try
|
|
{
|
|
ptr = Marshal.AllocHGlobal(size);
|
|
|
|
Marshal.Copy(arr, 0, ptr, size);
|
|
|
|
mem = Marshal.PtrToStructure(ptr, type);
|
|
}
|
|
finally
|
|
{
|
|
Marshal.FreeHGlobal(ptr);
|
|
}
|
|
|
|
return mem;
|
|
}
|
|
|
|
}
|
|
}
|