FPGAArrayFXPWriteProperty.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. _Session.CheckResult(Read(_Session.Session, Indicator, ref tempbuffer[0], (uint)tempbuffer.Length));
  28. string s = string.Empty;
  29. for(int i=0;i<tempbuffer.Length;i++)
  30. {
  31. s += tempbuffer[i].ToString("b").PadLeft(8, '0');
  32. }
  33. for(int i=0;i<tempvalue.Length;i++)
  34. {
  35. tempvalue[i] = System.Convert.ToUInt64(s.Substring(i * _TypeInfo.wordLength, _TypeInfo.wordLength), 2);
  36. }
  37. _Convert.FxpConvertToDouble(ref tempvalue[0], _TypeInfo, ref doubles[0], Count);
  38. return doubles;
  39. }
  40. private void SetData(double[] values)
  41. {
  42. if (values.Length != Count) return;
  43. _Convert.DoubleConvertToDxp(ref values[0], _TypeInfo, ref tempvalue[0], Count);
  44. string s = string.Empty;
  45. for(int i=0;i<tempvalue.Length;i++)
  46. {
  47. s += tempvalue[i].ToString("b").PadLeft(64, '0').Substring(64 - _TypeInfo.wordLength, _TypeInfo.wordLength);
  48. }
  49. for(int i=0;i< tempbuffer.Length;i++)
  50. {
  51. tempbuffer[i] = System.Convert.ToByte(s.Substring(i * 8, Math.Min(8, s.Length - i * 8)),2);
  52. }
  53. _Session.CheckResult(Write(_Session.Session,Indicator, ref tempbuffer[0], (uint)tempbuffer.Length));
  54. }
  55. public uint Count { get; }
  56. public double[] Value { get => GetData(); set => SetData(value); }
  57. }
  58. }