1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- using System;
- using System.Runtime.InteropServices;
- namespace Uestc.Auto6.Dso.Hardware.Calibration.Data.Base
- {
- public class Helper
- {
- #region Marshal
- internal static byte[] StructToBytes(object structObj)
- {
- int size = Marshal.SizeOf(structObj);
- IntPtr buffer = Marshal.AllocHGlobal(size);
- try
- {
- Marshal.StructureToPtr(structObj, buffer, false);
- byte[] bytes = new byte[size];
- Marshal.Copy(buffer, bytes, 0, size);
- return bytes;
- }
- finally
- {
- Marshal.FreeHGlobal(buffer);
- }
- }
- internal static T? BytesToStruct<T>(byte[] bytes, int startIndex, Type strcutType)
- {
- int size = Marshal.SizeOf(strcutType);
- T? data;
- IntPtr buffer = Marshal.AllocHGlobal(size);
- try
- {
- Marshal.Copy(bytes, startIndex, buffer, size);
- data=(T?)Marshal.PtrToStructure(buffer, strcutType);
- }
- catch
- {
- data = (T?)Activator.CreateInstance(strcutType, new object[] { });
- }
- finally
- {
- Marshal.FreeHGlobal(buffer);
- }
- return data;
- }
- #endregion
- public static ICaliData? GetICaliData(CaliDataType caliDataType)
- {
- return caliDataType switch
- {
- CaliDataType.PhyChannel => ChannelParams.Default,
- CaliDataType.TiAdc_SyncSampleClock => TiAdc_SyncSampleClock.Default,
- CaliDataType.TiAdc_PhaseOffsetGain => TiAdc_PhaseOffsetGain.Default,
- CaliDataType.CoefficientsTable_AFC => CoefficientsTable_AFC.Default,
- CaliDataType.CoefficientsTable_Interpolation => CoefficientsTable_Interpolation.Default,
- CaliDataType.CoefficientsTable_TiAdc => CoefficientsTable_TiAdc.Default,
- CaliDataType.Misc =>MiscData.Default,
- _ => null
- };
- }
- public static ICoefficientsTable? GetICoefficientsTable(CaliDataType caliDataType)
- {
- return caliDataType switch
- {
- CaliDataType.CoefficientsTable_AFC => CoefficientsTable_AFC.Default,
- CaliDataType.CoefficientsTable_Interpolation => CoefficientsTable_Interpolation.Default,
- CaliDataType.CoefficientsTable_TiAdc => CoefficientsTable_TiAdc.Default,
- _ => null
- };
- }
- }
- }
|