Dreamine.PLC.Core 1.0.1
Dreamine.PLC.Core 산업 자동화 및 I/O 기능을 제공합니다.
로딩중...
검색중...
일치하는것 없음
InMemoryPlcMemory.cs
이 파일의 문서화 페이지로 가기
1using Dreamine.PLC.Abstractions.Devices;
2using Dreamine.PLC.Abstractions.Results;
3
5
14public sealed class InMemoryPlcMemory
15{
24 private readonly object _syncRoot = new();
33 private readonly Dictionary<PlcDeviceType, Dictionary<int, short>> _words = new();
42 private readonly Dictionary<PlcDeviceType, Dictionary<int, bool>> _bits = new();
43
76 public PlcResult<bool[]> ReadBits(PlcAddress address, int count)
77 {
78 if (count <= 0)
79 {
80 return PlcResult<bool[]>.Failure("The read count must be greater than zero.");
81 }
82
83 lock (_syncRoot)
84 {
85 var values = new bool[count];
86 var area = GetOrCreateBitArea(address.DeviceType);
87
88 for (var i = 0; i < count; i++)
89 {
90 var offset = address.Offset + i;
91 values[i] = area.TryGetValue(offset, out var value) && value;
92 }
93
94 return PlcResult<bool[]>.Success(values);
95 }
96 }
97
130 public PlcResult<short[]> ReadWords(PlcAddress address, int count)
131 {
132 if (count <= 0)
133 {
134 return PlcResult<short[]>.Failure("The read count must be greater than zero.");
135 }
136
137 lock (_syncRoot)
138 {
139 var values = new short[count];
140 var area = GetOrCreateWordArea(address.DeviceType);
141
142 for (var i = 0; i < count; i++)
143 {
144 var offset = address.Offset + i;
145 values[i] = area.TryGetValue(offset, out var value) ? value : default;
146 }
147
148 return PlcResult<short[]>.Success(values);
149 }
150 }
151
184 public PlcResult WriteBits(PlcAddress address, IReadOnlyList<bool> values)
185 {
186 if (values.Count == 0)
187 {
188 return PlcResult.Failure("The bit value collection must not be empty.");
189 }
190
191 lock (_syncRoot)
192 {
193 var area = GetOrCreateBitArea(address.DeviceType);
194
195 for (var i = 0; i < values.Count; i++)
196 {
197 var offset = address.Offset + i;
198 area[offset] = values[i];
199 }
200
201 return PlcResult.Success();
202 }
203 }
204
237 public PlcResult WriteWords(PlcAddress address, IReadOnlyList<short> values)
238 {
239 if (values.Count == 0)
240 {
241 return PlcResult.Failure("The word value collection must not be empty.");
242 }
243
244 lock (_syncRoot)
245 {
246 var area = GetOrCreateWordArea(address.DeviceType);
247
248 for (var i = 0; i < values.Count; i++)
249 {
250 var offset = address.Offset + i;
251 area[offset] = values[i];
252 }
253
254 return PlcResult.Success();
255 }
256 }
257
266 public void Clear()
267 {
268 lock (_syncRoot)
269 {
270 _bits.Clear();
271 _words.Clear();
272 }
273 }
274
299 private Dictionary<int, short> GetOrCreateWordArea(PlcDeviceType deviceType)
300 {
301 if (!_words.TryGetValue(deviceType, out var area))
302 {
303 area = new Dictionary<int, short>();
304 _words[deviceType] = area;
305 }
306
307 return area;
308 }
309
334 private Dictionary<int, bool> GetOrCreateBitArea(PlcDeviceType deviceType)
335 {
336 if (!_bits.TryGetValue(deviceType, out var area))
337 {
338 area = new Dictionary<int, bool>();
339 _bits[deviceType] = area;
340 }
341
342 return area;
343 }
344}
Dictionary< int, bool > GetOrCreateBitArea(PlcDeviceType deviceType)
PlcResult WriteBits(PlcAddress address, IReadOnlyList< bool > values)
PlcResult< short[]> ReadWords(PlcAddress address, int count)
PlcResult WriteWords(PlcAddress address, IReadOnlyList< short > values)
readonly Dictionary< PlcDeviceType, Dictionary< int, bool > > _bits
PlcResult< bool[]> ReadBits(PlcAddress address, int count)
Dictionary< int, short > GetOrCreateWordArea(PlcDeviceType deviceType)
readonly Dictionary< PlcDeviceType, Dictionary< int, short > > _words