FPGAArrayFXPWriteProperty.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using FxpConvert.Common;
  2. using System.Reflection;
  3. namespace NIFPGA
  4. {
  5. public sealed class FPGAArrayFXPWriteProperty : FPGABaseProperty
  6. {
  7. Interop.NiFpgaDll_ReadArrayU8 Read;
  8. Interop.NiFpgaDll_WriteArrayU8 Write;
  9. private NiFpga_FxpTypeInfo _TypeInfo;
  10. private IFxpConvert _Convert;
  11. private byte[] tempbuffer = new byte[0];
  12. private ulong[] tempvalue = new ulong[0];
  13. private double[] doubles = new double[0];
  14. internal FPGAArrayFXPWriteProperty(FPGASession session, uint indicator, uint count,NiFpga_FxpTypeInfo typeInfo, IFxpConvert convert) : base(session, indicator,false)
  15. {
  16. _Convert = convert;
  17. Count = count;
  18. tempvalue = new ulong[count];
  19. doubles = new double[count];
  20. _TypeInfo = typeInfo;
  21. tempbuffer = new byte[(int)Math.Ceiling(typeInfo.wordLength*count / 8.0)];
  22. Read = _Session.GetDelegate<Interop.NiFpgaDll_ReadArrayU8>();
  23. Write = _Session.GetDelegate<Interop.NiFpgaDll_WriteArrayU8>();
  24. }
  25. private double[] GetData()
  26. {
  27. lock (Read)
  28. {
  29. _Session.CheckResult(Read(_Session.Session, Indicator, ref tempbuffer[0], (uint)tempbuffer.Length));
  30. string s = string.Empty;
  31. for (int i = 0; i < tempbuffer.Length; i++)
  32. {
  33. s += tempbuffer[i].ToString("b").PadLeft(8, '0');
  34. }
  35. for (int i = 0; i < tempvalue.Length; i++)
  36. {
  37. tempvalue[i] = System.Convert.ToUInt64(s.Substring(i * _TypeInfo.wordLength, _TypeInfo.wordLength), 2);
  38. }
  39. _Convert.FxpConvertToDouble(ref tempvalue[0], _TypeInfo, ref doubles[0], Count);
  40. return doubles;
  41. }
  42. }
  43. private void SetData(double[] values)
  44. {
  45. lock (Read)
  46. {
  47. if (values.Length != Count) return;
  48. _Convert.DoubleConvertToDxp(ref values[0], _TypeInfo, ref tempvalue[0], Count);
  49. string s = string.Empty;
  50. for (int i = 0; i < tempvalue.Length; i++)
  51. {
  52. s += tempvalue[i].ToString("b").PadLeft(64, '0').Substring(64 - _TypeInfo.wordLength, _TypeInfo.wordLength);
  53. }
  54. for (int i = 0; i < tempbuffer.Length; i++)
  55. {
  56. tempbuffer[i] = System.Convert.ToByte(s.Substring(i * 8, Math.Min(8, s.Length - i * 8)), 2);
  57. }
  58. _Session.CheckResult(Write(_Session.Session, Indicator, ref tempbuffer[0], (uint)tempbuffer.Length));
  59. }
  60. }
  61. public uint Count { get; }
  62. public double[] Value { get => GetData(); set => SetData(value); }
  63. }
  64. }