Dreamine.PLC.Omron.Fins 1.0.1
Dreamine.PLC.Omron.Fins 산업 자동화 및 I/O 기능을 제공합니다.
로딩중...
검색중...
일치하는것 없음
OmronFinsResponseParser.cs
이 파일의 문서화 페이지로 가기
1using Dreamine.PLC.Abstractions.Results;
2
4
13public sealed class OmronFinsResponseParser
14{
47 public PlcResult<byte[]> ExtractPayload(IReadOnlyList<byte> frame)
48 {
49 ArgumentNullException.ThrowIfNull(frame);
50
51 if (frame.Count < 14)
52 {
53 return PlcResult<byte[]>.Failure($"The FINS response frame is too short. Length={frame.Count}.");
54 }
55
56 var buffer = frame as byte[] ?? frame.ToArray();
57 var endCode = OmronFinsEndian.ReadUInt16(buffer, 12);
58 if (endCode != 0)
59 {
60 return PlcResult<byte[]>.Failure($"FINS end code indicates failure: 0x{endCode:X4}.", endCode);
61 }
62
63 return PlcResult<byte[]>.Success(buffer[14..]);
64 }
65
106 public PlcResult<bool[]> ParseBits(IReadOnlyList<byte> payload, int count)
107 {
108 ArgumentNullException.ThrowIfNull(payload);
109
110 if (payload.Count < count)
111 {
112 return PlcResult<bool[]>.Failure($"The FINS bit payload is too short. Expected={count}, Actual={payload.Count}.");
113 }
114
115 var values = new bool[count];
116 for (var index = 0; index < count; index++)
117 {
118 values[index] = payload[index] != 0;
119 }
120
121 return PlcResult<bool[]>.Success(values);
122 }
123
172 public PlcResult<short[]> ParseWords(IReadOnlyList<byte> payload, int count)
173 {
174 ArgumentNullException.ThrowIfNull(payload);
175
176 var expectedLength = checked(count * 2);
177 if (payload.Count < expectedLength)
178 {
179 return PlcResult<short[]>.Failure($"The FINS word payload is too short. Expected={expectedLength}, Actual={payload.Count}.");
180 }
181
182 var buffer = payload as byte[] ?? payload.ToArray();
183 var values = new short[count];
184 for (var index = 0; index < count; index++)
185 {
186 values[index] = OmronFinsEndian.ReadInt16(buffer, index * 2);
187 }
188
189 return PlcResult<short[]>.Success(values);
190 }
191}
static short ReadInt16(ReadOnlySpan< byte > buffer, int offset)
static ushort ReadUInt16(ReadOnlySpan< byte > buffer, int offset)
PlcResult< byte[]> ExtractPayload(IReadOnlyList< byte > frame)
PlcResult< bool[]> ParseBits(IReadOnlyList< byte > payload, int count)
PlcResult< short[]> ParseWords(IReadOnlyList< byte > payload, int count)