DefaultPointSource.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System;
  2. using System.Linq;
  3. using NModbus.Unme.Common;
  4. namespace NModbus.Data
  5. {
  6. /// <summary>
  7. /// A simple implementation of the point source. All registers are
  8. /// </summary>
  9. /// <typeparam name="TPoint"></typeparam>
  10. internal class DefaultPointSource<TPoint> : IPointSource<TPoint>
  11. {
  12. //Only create this if referenced.
  13. private readonly Lazy<TPoint[]> _points;
  14. private readonly object _syncRoot = new object();
  15. public DefaultPointSource()
  16. {
  17. _points = new Lazy<TPoint[]>(() => new TPoint[ushort.MaxValue+1]);
  18. }
  19. public TPoint[] ReadPoints(ushort startAddress, ushort numberOfPoints)
  20. {
  21. lock (_syncRoot)
  22. {
  23. return _points.Value
  24. .Slice(startAddress, numberOfPoints)
  25. .ToArray();
  26. }
  27. }
  28. public void WritePoints(ushort startAddress, TPoint[] points)
  29. {
  30. lock (_syncRoot)
  31. {
  32. for (ushort index = 0; index < points.Length; index++)
  33. {
  34. _points.Value[startAddress + index] = points[index];
  35. }
  36. }
  37. }
  38. }
  39. }