Dreamine.PLC.Omron.Fins 1.0.1
Dreamine.PLC.Omron.Fins 산업 자동화 및 I/O 기능을 제공합니다.
로딩중...
검색중...
일치하는것 없음
OmronFinsFrameBuilder.cs
이 파일의 문서화 페이지로 가기
1using Dreamine.PLC.Abstractions.Devices;
4
6
15public sealed class OmronFinsFrameBuilder
16{
25 private int _sid;
26
91 public byte[] BuildMemoryAreaRead(
93 PlcAddress address,
94 int count,
95 bool bitAccess)
96 {
97 var command = BuildMemoryCommandBody(OmronFinsCommand.MemoryAreaRead, address, count, bitAccess, null);
98 return BuildFrame(options, command);
99 }
100
167 PlcAddress address,
168 IReadOnlyList<short> values)
169 {
170 var payload = new byte[values.Count * 2];
171 for (var i = 0; i < values.Count; i++)
172 {
173 OmronFinsEndian.WriteInt16(payload, i * 2, values[i]);
174 }
175
176 var command = BuildMemoryCommandBody(OmronFinsCommand.MemoryAreaWrite, address, values.Count, false, payload);
177 return BuildFrame(options, command);
178 }
179
238 PlcAddress address,
239 IReadOnlyList<bool> values)
240 {
241 var payload = values.Select(value => value ? (byte)1 : (byte)0).ToArray();
242 var command = BuildMemoryCommandBody(OmronFinsCommand.MemoryAreaWrite, address, values.Count, true, payload);
243 return BuildFrame(options, command);
244 }
245
286 private byte[] BuildFrame(OmronFinsConnectionOptions options, byte[] command)
287 {
288 var frame = new byte[10 + command.Length];
289 frame[0] = 0x80;
290 frame[1] = 0x00;
291 frame[2] = 0x02;
292 frame[3] = options.DestinationNetwork;
293 frame[4] = options.DestinationNode;
294 frame[5] = options.DestinationUnit;
295 frame[6] = options.SourceNetwork;
296 frame[7] = options.SourceNode;
297 frame[8] = options.SourceUnit;
298 frame[9] = unchecked((byte)Interlocked.Increment(ref _sid));
299 Buffer.BlockCopy(command, 0, frame, 10, command.Length);
300 return frame;
301 }
302
375 private static byte[] BuildMemoryCommandBody(
376 OmronFinsCommand command,
377 PlcAddress address,
378 int count,
379 bool bitAccess,
380 byte[]? payload)
381 {
382 if (count <= 0)
383 {
384 throw new ArgumentOutOfRangeException(nameof(count), count, "The FINS point count must be greater than zero.");
385 }
386
387 if (address.Offset is < 0 or > ushort.MaxValue)
388 {
389 throw new ArgumentOutOfRangeException(
390 nameof(address),
391 address.Offset,
392 "The FINS address offset must be between 0 and 65535.");
393 }
394
395 if (count > ushort.MaxValue)
396 {
397 throw new ArgumentOutOfRangeException(
398 nameof(count),
399 count,
400 "The FINS point count must be between 1 and 65535.");
401 }
402
403 var areaCode = OmronFinsMemoryAreaMapper.Map(address, bitAccess);
404 var bodyLength = 8 + (payload?.Length ?? 0);
405 var body = new byte[bodyLength];
406 var commandValue = (ushort)command;
407
408 OmronFinsEndian.WriteUInt16(body, 0, commandValue);
409 body[2] = areaCode;
410 OmronFinsEndian.WriteUInt16(body, 3, (ushort)address.Offset);
411 body[5] = (byte)(address.BitOffset ?? 0);
412 OmronFinsEndian.WriteUInt16(body, 6, (ushort)count);
413
414 if (payload is not null)
415 {
416 Buffer.BlockCopy(payload, 0, body, 8, payload.Length);
417 }
418
419 return body;
420 }
421}
static byte Map(PlcAddress address, bool bitAccess)
static void WriteUInt16(Span< byte > buffer, int offset, ushort value)
static void WriteInt16(Span< byte > buffer, int offset, short value)
byte[] BuildMemoryAreaWriteWords(OmronFinsConnectionOptions options, PlcAddress address, IReadOnlyList< short > values)
static byte[] BuildMemoryCommandBody(OmronFinsCommand command, PlcAddress address, int count, bool bitAccess, byte[]? payload)
byte[] BuildMemoryAreaRead(OmronFinsConnectionOptions options, PlcAddress address, int count, bool bitAccess)
byte[] BuildMemoryAreaWriteBits(OmronFinsConnectionOptions options, PlcAddress address, IReadOnlyList< bool > values)
byte[] BuildFrame(OmronFinsConnectionOptions options, byte[] command)