123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- using FxpConvert.Common;
- using System.Reflection;
- namespace NIFPGA
- {
- public sealed class FPGAArrayFXPWriteProperty : FPGABaseProperty
- {
- Interop.NiFpgaDll_ReadArrayU8 Read;
- Interop.NiFpgaDll_WriteArrayU8 Write;
- private NiFpga_FxpTypeInfo _TypeInfo;
- private IFxpConvert _Convert;
- private byte[] tempbuffer = new byte[0];
- private ulong[] tempvalue = new ulong[0];
- private double[] doubles = new double[0];
- internal FPGAArrayFXPWriteProperty(FPGASession session, uint indicator, uint count,NiFpga_FxpTypeInfo typeInfo, IFxpConvert convert) : base(session, indicator,false)
- {
- _Convert = convert;
- Count = count;
- tempvalue = new ulong[count];
- doubles = new double[count];
- _TypeInfo = typeInfo;
- tempbuffer = new byte[(int)Math.Ceiling(typeInfo.wordLength*count / 8.0)];
- Read = _Session.GetDelegate<Interop.NiFpgaDll_ReadArrayU8>();
- Write = _Session.GetDelegate<Interop.NiFpgaDll_WriteArrayU8>();
- }
- private double[] GetData()
- {
- _Session.CheckResult(Read(_Session.Session, Indicator, ref tempbuffer[0], (uint)tempbuffer.Length));
- string s = string.Empty;
- for(int i=0;i<tempbuffer.Length;i++)
- {
- s += tempbuffer[i].ToString("b").PadLeft(8, '0');
- }
- for(int i=0;i<tempvalue.Length;i++)
- {
- tempvalue[i] = System.Convert.ToUInt64(s.Substring(i * _TypeInfo.wordLength, _TypeInfo.wordLength), 2);
- }
- _Convert.FxpConvertToDouble(ref tempvalue[0], _TypeInfo, ref doubles[0], Count);
- return doubles;
- }
- private void SetData(double[] values)
- {
- if (values.Length != Count) return;
- _Convert.DoubleConvertToDxp(ref values[0], _TypeInfo, ref tempvalue[0], Count);
- string s = string.Empty;
- for(int i=0;i<tempvalue.Length;i++)
- {
- s += tempvalue[i].ToString("b").PadLeft(64, '0').Substring(64 - _TypeInfo.wordLength, _TypeInfo.wordLength);
- }
- for(int i=0;i< tempbuffer.Length;i++)
- {
- tempbuffer[i] = System.Convert.ToByte(s.Substring(i * 8, Math.Min(8, s.Length - i * 8)),2);
- }
- _Session.CheckResult(Write(_Session.Session,Indicator, ref tempbuffer[0], (uint)tempbuffer.Length));
- }
- public uint Count { get; }
- public double[] Value { get => GetData(); set => SetData(value); }
- }
- }
|