CoefficientsTable_Interpolation.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Runtime.InteropServices;
  7. using System.Threading.Tasks;
  8. using Uestc.Auto6.Dso.ComModel;
  9. namespace Uestc.Auto6.Dso.Hardware.Calibration.Data.Base
  10. {
  11. public class CoefficientsTable_Interpolation : ICaliData, ICoefficientsTable
  12. {
  13. public static CoefficientsTable_Interpolation Default = new CoefficientsTable_Interpolation();
  14. public const Int32 PerChannelDataCount = 600;
  15. private CoefficientsTable_Interpolation()
  16. {
  17. for (int channelID = 0; channelID < CaliConstants.Fixed_MaxPhysicsChannelCount; channelID++)
  18. data[channelID] = new Int32[PerChannelDataCount];
  19. }
  20. public CaliDataType DataType { get => CaliDataType.CoefficientsTable_Interpolation; }
  21. private Int32[/*channel*/][/*data*/] data = new Int32[CaliConstants.Fixed_MaxPhysicsChannelCount][];
  22. public Int32 TotalBytes
  23. {
  24. get
  25. {
  26. int totalBytes = CaliConstants.Fixed_MaxPhysicsChannelCount * PerChannelDataCount * sizeof(Int32);
  27. return totalBytes;
  28. }
  29. }
  30. /// <summary>
  31. ///
  32. /// </summary>
  33. /// <param name="channelIndex"></param>
  34. /// <returns></returns>
  35. public Int32[] this[int channelIndex]
  36. {
  37. get => data[channelIndex];
  38. }
  39. /// <summary>
  40. /// [int channelIndex,int index]
  41. /// </summary>
  42. /// <param name="channelIndex"></param>
  43. /// <param name="index"></param>
  44. /// <returns></returns>
  45. public Int32 this[int channelIndex, int index]
  46. {
  47. get => data[channelIndex][index];
  48. set => data[channelIndex][index] = value;
  49. }
  50. public byte[] Serialize()
  51. {
  52. System.IO.MemoryStream memoryStream = new MemoryStream();
  53. for (int channelIndex = 0; channelIndex < CaliConstants.Fixed_MaxPhysicsChannelCount; channelIndex++)
  54. {
  55. for (int i = 0; i < PerChannelDataCount; i++)
  56. {
  57. memoryStream.Write(Helper.StructToBytes(data[channelIndex][i]));
  58. }
  59. }
  60. byte[] result = memoryStream.ToArray();
  61. memoryStream.Close();
  62. return result;
  63. }
  64. public byte[] SerializeByFpgaFormat(int channelID)
  65. {
  66. byte[] bytes = new byte[PerChannelDataCount * (24 / 8)];//24=24bits,8=byte's bits
  67. for (int i = 0; i < PerChannelDataCount; i++)
  68. {
  69. bytes[i * 3 + 0] = (byte)(data[channelID][i] & 0xff);
  70. bytes[i * 3 + 1] = (byte)(data[channelID][i] >> 8 & 0xff);
  71. bytes[i * 3 + 2] = (byte)(data[channelID][i] >> 16 & 0xff);
  72. }
  73. return bytes;
  74. }
  75. public void Deserialize(byte[] content)
  76. {
  77. if (content.Length < TotalBytes)
  78. return;
  79. for (int channelID = 0; channelID < CaliConstants.Fixed_MaxPhysicsChannelCount; channelID++)
  80. {
  81. for (int index = 0; index < PerChannelDataCount; index++)
  82. {
  83. data[channelID][index] = Helper.BytesToStruct<Int32>(content, channelID * PerChannelDataCount * sizeof(Int32) + index * sizeof(Int32), typeof(Int32));
  84. }
  85. }
  86. }
  87. }
  88. }