SampleSmart 1.0.0.0
SampleSmart 사용 방법을 보여 주는 예제 프로젝트입니다.
로딩중...
검색중...
일치하는것 없음
SampleFastech16PointProtocol.cs
이 파일의 문서화 페이지로 가기
1using System.Text;
2using Dreamine.IO.Abstractions.Models;
3using Dreamine.IO.Abstractions.Results;
4using Dreamine.IO.Fastech.Ethernet.Protocol;
5
7
24public sealed class SampleFastech16PointProtocol : IFastechEthernetIoProtocol
25{
34 private const string ReadDigitalInputsCommand = "RDI";
43 private const string ReadDigitalOutputsCommand = "RDO";
52 private const string WriteDigitalOutputsCommand = "WDO";
61 private const string AckResponse = "OK";
62
87 public byte[] BuildReadDigitalInputs(IReadOnlyList<IoPoint> points)
88 {
89 ArgumentNullException.ThrowIfNull(points);
90
91 return Encode($"{ReadDigitalInputsCommand} {points.Count}");
92 }
93
126 public IoResult<bool[]> ParseDigitalInputs(IReadOnlyList<byte> responseFrame, int count)
127 {
128 return ParseBits(responseFrame, "DI", count);
129 }
130
155 public byte[] BuildReadDigitalOutputs(IReadOnlyList<IoPoint> points)
156 {
157 ArgumentNullException.ThrowIfNull(points);
158
159 return Encode($"{ReadDigitalOutputsCommand} {points.Count}");
160 }
161
194 public IoResult<bool[]> ParseDigitalOutputs(IReadOnlyList<byte> responseFrame, int count)
195 {
196 return ParseBits(responseFrame, "DO", count);
197 }
198
223 public byte[] BuildWriteDigitalOutputs(IReadOnlyDictionary<IoPoint, bool> values)
224 {
225 ArgumentNullException.ThrowIfNull(values);
226
227 var orderedValues = values
228 .OrderBy(x => x.Key.Module)
229 .ThenBy(x => x.Key.Channel)
230 .Select(x => x.Value ? '1' : '0');
231
232 return Encode($"{WriteDigitalOutputsCommand} {new string(orderedValues.ToArray())}");
233 }
234
267 public byte[] BuildReadAnalogInputs(IReadOnlyList<AnalogIoPoint> points)
268 {
269 throw new NotSupportedException("The 16/16 sample covers digital I/O only.");
270 }
271
304 public IoResult<double[]> ParseAnalogInputs(IReadOnlyList<byte> responseFrame, int count)
305 {
306 return IoResult<double[]>.Failure("The 16/16 sample covers digital I/O only.");
307 }
308
341 public byte[] BuildReadAnalogOutputs(IReadOnlyList<AnalogIoPoint> points)
342 {
343 throw new NotSupportedException("The 16/16 sample covers digital I/O only.");
344 }
345
378 public IoResult<double[]> ParseAnalogOutputs(IReadOnlyList<byte> responseFrame, int count)
379 {
380 return IoResult<double[]>.Failure("The 16/16 sample covers digital I/O only.");
381 }
382
415 public byte[] BuildWriteAnalogOutputs(IReadOnlyDictionary<AnalogIoPoint, double> values)
416 {
417 throw new NotSupportedException("The 16/16 sample covers digital I/O only.");
418 }
419
444 public IoResult ParseWriteResponse(IReadOnlyList<byte> responseFrame)
445 {
446 var text = Decode(responseFrame);
447 return text.Equals(AckResponse, StringComparison.OrdinalIgnoreCase)
448 ? IoResult.Success()
449 : IoResult.Failure($"Unexpected sample write response: {text}");
450 }
451
492 private static IoResult<bool[]> ParseBits(IReadOnlyList<byte> responseFrame, string prefix, int count)
493 {
494 var text = Decode(responseFrame);
495 var expectedPrefix = $"{prefix} ";
496
497 if (!text.StartsWith(expectedPrefix, StringComparison.OrdinalIgnoreCase))
498 {
499 return IoResult<bool[]>.Failure($"Unexpected sample response: {text}");
500 }
501
502 var bitText = text[expectedPrefix.Length..].Trim();
503 if (bitText.Length < count)
504 {
505 return IoResult<bool[]>.Failure($"The sample response contains {bitText.Length} bits, but {count} were expected.");
506 }
507
508 var values = bitText
509 .Take(count)
510 .Select(x => x == '1')
511 .ToArray();
512
513 return IoResult<bool[]>.Success(values);
514 }
515
540 private static byte[] Encode(string text)
541 {
542 return Encoding.ASCII.GetBytes(text);
543 }
544
569 private static string Decode(IReadOnlyList<byte> bytes)
570 {
571 var buffer = bytes as byte[] ?? bytes.ToArray();
572 return Encoding.ASCII.GetString(buffer).Trim();
573 }
574}
IoResult< double[]> ParseAnalogOutputs(IReadOnlyList< byte > responseFrame, int count)
static IoResult< bool[]> ParseBits(IReadOnlyList< byte > responseFrame, string prefix, int count)
IoResult< bool[]> ParseDigitalInputs(IReadOnlyList< byte > responseFrame, int count)
IoResult< double[]> ParseAnalogInputs(IReadOnlyList< byte > responseFrame, int count)
byte[] BuildWriteAnalogOutputs(IReadOnlyDictionary< AnalogIoPoint, double > values)
IoResult< bool[]> ParseDigitalOutputs(IReadOnlyList< byte > responseFrame, int count)
byte[] BuildWriteDigitalOutputs(IReadOnlyDictionary< IoPoint, bool > values)