FPGAArrayFXPReadProperty.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using FxpConvert.Common;
  2. namespace NIFPGA
  3. {
  4. public sealed class FPGAArrayFXPReadProperty : FPGABaseProperty
  5. {
  6. Interop.NiFpgaDll_ReadArrayU8 Read;
  7. private NiFpga_FxpTypeInfo _TypeInfo;
  8. private IFxpConvert _Convert;
  9. private byte[] tempbuffer = new byte[0];
  10. private ulong[] tempvalue = new ulong[0];
  11. private double[] doubles = new double[0];
  12. internal FPGAArrayFXPReadProperty(FPGASession session, uint indicator, uint count, NiFpga_FxpTypeInfo typeInfo, IFxpConvert convert) : base(session, indicator, true)
  13. {
  14. _Convert = convert;
  15. Count = count;
  16. tempvalue = new ulong[count];
  17. doubles = new double[count];
  18. _TypeInfo = typeInfo;
  19. tempbuffer = new byte[(int)Math.Ceiling(typeInfo.wordLength * count / 8.0)];
  20. Read = _Session.GetDelegate<Interop.NiFpgaDll_ReadArrayU8>();
  21. }
  22. private double[] GetData()
  23. {
  24. _Session.CheckResult(Read(_Session.Session, Indicator, ref tempbuffer[0], (uint)tempbuffer.Length));
  25. string s = string.Empty;
  26. for (int i = 0; i < tempbuffer.Length; i++)
  27. {
  28. s += tempbuffer[i].ToString("b").PadLeft(8, '0');
  29. }
  30. for (int i = 0; i < tempvalue.Length; i++)
  31. {
  32. tempvalue[i] = System.Convert.ToUInt64(s.Substring(i * _TypeInfo.wordLength, _TypeInfo.wordLength), 2);
  33. }
  34. _Convert.FxpConvertToDouble(ref tempvalue[0], _TypeInfo, ref doubles[0], Count);
  35. return doubles;
  36. }
  37. public uint Count { get; }
  38. public double[] Value { get => GetData(); }
  39. }
  40. }