forked from NModbus/NModbus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSparsePointStorage.cs
More file actions
156 lines (125 loc) · 4.32 KB
/
SparsePointStorage.cs
File metadata and controls
156 lines (125 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using System;
using System.Collections.Generic;
using NModbus;
namespace Samples
{
public class SlaveStorage : ISlaveDataStore
{
private readonly SparsePointSource<bool> _coilDiscretes;
private readonly SparsePointSource<bool> _coilInputs;
private readonly SparsePointSource<ushort> _holdingRegisters;
private readonly SparsePointSource<ushort> _inputRegisters;
public SlaveStorage()
{
_coilDiscretes = new SparsePointSource<bool>();
_coilInputs = new SparsePointSource<bool>();
_holdingRegisters = new SparsePointSource<ushort>();
_inputRegisters = new SparsePointSource<ushort>();
}
public SparsePointSource<bool> CoilDiscretes
{
get { return _coilDiscretes; }
}
public SparsePointSource<bool> CoilInputs
{
get { return _coilInputs; }
}
public SparsePointSource<ushort> HoldingRegisters
{
get { return _holdingRegisters; }
}
public SparsePointSource<ushort> InputRegisters
{
get { return _inputRegisters; }
}
IPointSource<bool> ISlaveDataStore.CoilDiscretes
{
get { return _coilDiscretes; }
}
IPointSource<bool> ISlaveDataStore.CoilInputs
{
get { return _coilInputs; }
}
IPointSource<ushort> ISlaveDataStore.HoldingRegisters
{
get { return _holdingRegisters; }
}
IPointSource<ushort> ISlaveDataStore.InputRegisters
{
get { return _inputRegisters; }
}
}
/// <summary>
/// Sparse storage for points.
/// </summary>
public class SparsePointSource<TPoint> : IPointSource<TPoint>
{
private readonly Dictionary<ushort, TPoint> _values = new Dictionary<ushort, TPoint>();
public event EventHandler<StorageEventArgs<TPoint>> StorageOperationOccurred;
/// <summary>
/// Gets or sets the value of an individual point wih tout
/// </summary>
/// <param name="registerIndex"></param>
/// <returns></returns>
public TPoint this[ushort registerIndex]
{
get
{
TPoint value;
if (_values.TryGetValue(registerIndex, out value))
return value;
return default(TPoint);
}
set { _values[registerIndex] = value; }
}
public TPoint[] ReadPoints(ushort startAddress, ushort numberOfPoints)
{
var points = new TPoint[numberOfPoints];
for (ushort index = 0; index < numberOfPoints; index++)
{
points[index] = this[(ushort) (index + startAddress)];
}
StorageOperationOccurred?.Invoke(this,
new StorageEventArgs<TPoint>(PointOperation.Read, startAddress, points));
return points;
}
public void WritePoints(ushort startAddress, TPoint[] points)
{
for (ushort index = 0; index < points.Length; index++)
{
this[(ushort) (index + startAddress)] = points[index];
}
StorageOperationOccurred?.Invoke(this,
new StorageEventArgs<TPoint>(PointOperation.Write, startAddress, points));
}
}
public class StorageEventArgs<TPoint> : EventArgs
{
private readonly PointOperation _pointOperation;
private readonly ushort _startingAddress;
private readonly TPoint[] _points;
public StorageEventArgs(PointOperation pointOperation, ushort startingAddress, TPoint[] points)
{
_pointOperation = pointOperation;
_startingAddress = startingAddress;
_points = points;
}
public ushort StartingAddress
{
get { return _startingAddress; }
}
public TPoint[] Points
{
get { return _points; }
}
public PointOperation Operation
{
get { return _pointOperation; }
}
}
public enum PointOperation
{
Read,
Write
}
}