SlaveStorage.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using System.Collections.Generic;
  3. using NModbus;
  4. namespace Team.Utility
  5. {
  6. public class SlaveStorage : ISlaveDataStore
  7. {
  8. private readonly SparsePointSource<bool> _coilDiscretes;
  9. private readonly SparsePointSource<bool> _coilInputs;
  10. private readonly SparsePointSource<ushort> _holdingRegisters;
  11. private readonly SparsePointSource<ushort> _inputRegisters;
  12. public SlaveStorage()
  13. {
  14. _coilDiscretes = new SparsePointSource<bool>();
  15. _coilInputs = new SparsePointSource<bool>();
  16. _holdingRegisters = new SparsePointSource<ushort>();
  17. _inputRegisters = new SparsePointSource<ushort>();
  18. }
  19. public SparsePointSource<bool> CoilDiscretes
  20. {
  21. get { return _coilDiscretes; }
  22. }
  23. public SparsePointSource<bool> CoilInputs
  24. {
  25. get { return _coilInputs; }
  26. }
  27. public SparsePointSource<ushort> HoldingRegisters
  28. {
  29. get { return _holdingRegisters; }
  30. }
  31. public SparsePointSource<ushort> InputRegisters
  32. {
  33. get { return _inputRegisters; }
  34. }
  35. IPointSource<bool> ISlaveDataStore.CoilDiscretes
  36. {
  37. get { return _coilDiscretes; }
  38. }
  39. IPointSource<bool> ISlaveDataStore.CoilInputs
  40. {
  41. get { return _coilInputs; }
  42. }
  43. IPointSource<ushort> ISlaveDataStore.HoldingRegisters
  44. {
  45. get { return _holdingRegisters; }
  46. }
  47. IPointSource<ushort> ISlaveDataStore.InputRegisters
  48. {
  49. get { return _inputRegisters; }
  50. }
  51. }
  52. public class SparsePointSource<TPoint> : IPointSource<TPoint>
  53. {
  54. private readonly Dictionary<ushort, TPoint> _values = new();
  55. public event EventHandler<StorageEventArgs<TPoint>> StorageOperationOccurred;
  56. /// <summary>
  57. /// Gets or sets the value of an individual point wih tout
  58. /// </summary>
  59. /// <param name="registerIndex"></param>
  60. /// <returns></returns>
  61. public TPoint this[ushort registerIndex]
  62. {
  63. get => _values.TryGetValue(registerIndex, out var value) ? value : default;
  64. set => _values[registerIndex] = value;
  65. }
  66. public TPoint[] ReadPoints(ushort startAddress, ushort numberOfPoints)
  67. {
  68. var points = new TPoint[numberOfPoints];
  69. for (ushort index = 0; index < numberOfPoints; index++)
  70. {
  71. points[index] = this[(ushort)(index + startAddress)];
  72. }
  73. StorageOperationOccurred?.Invoke(this,
  74. new StorageEventArgs<TPoint>(PointOperation.Read, startAddress, points));
  75. return points;
  76. }
  77. public void WritePoints(ushort startAddress, TPoint[] points)
  78. {
  79. for (ushort index = 0; index < points.Length; index++)
  80. {
  81. this[(ushort)(index + startAddress)] = points[index];
  82. }
  83. StorageOperationOccurred?.Invoke(this,
  84. new StorageEventArgs<TPoint>(PointOperation.Write, startAddress, points));
  85. }
  86. }
  87. public class StorageEventArgs<TPoint> : EventArgs
  88. {
  89. private readonly TPoint[] _points;
  90. public StorageEventArgs(PointOperation pointOperation, ushort startingAddress, TPoint[] points)
  91. {
  92. Operation = pointOperation;
  93. StartingAddress = startingAddress;
  94. _points = points;
  95. }
  96. public ushort StartingAddress { get; }
  97. public TPoint[] Points => _points;
  98. public PointOperation Operation { get; }
  99. }
  100. public enum PointOperation
  101. {
  102. Read,
  103. Write
  104. }
  105. }