SampleSmart
1.0.0.0
SampleSmart 사용 방법을 보여 주는 예제 프로젝트입니다.
Toggle main menu visibility
로딩중...
검색중...
일치하는것 없음
SampleFastech16PointProtocol.cs
이 파일의 문서화 페이지로 가기
1
using
System.Text;
2
using
Dreamine.IO.Abstractions.Models;
3
using
Dreamine.IO.Abstractions.Results;
4
using
Dreamine.IO.Fastech.Ethernet.Protocol;
5
6
namespace
SampleSmart.Pages.PageSub.IoTabs
;
7
24
public
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
}
SampleSmart.Pages.PageSub.IoTabs
Definition
IoPointState.cs:3
SampleSmart.Pages.PageSub.IoTabs.SampleFastech16PointProtocol
Definition
SampleFastech16PointProtocol.cs:25
SampleSmart.Pages.PageSub.IoTabs.SampleFastech16PointProtocol.ParseAnalogOutputs
IoResult< double[]> ParseAnalogOutputs(IReadOnlyList< byte > responseFrame, int count)
Definition
SampleFastech16PointProtocol.cs:378
SampleSmart.Pages.PageSub.IoTabs.SampleFastech16PointProtocol.ParseBits
static IoResult< bool[]> ParseBits(IReadOnlyList< byte > responseFrame, string prefix, int count)
Definition
SampleFastech16PointProtocol.cs:492
SampleSmart.Pages.PageSub.IoTabs.SampleFastech16PointProtocol.BuildReadAnalogInputs
byte[] BuildReadAnalogInputs(IReadOnlyList< AnalogIoPoint > points)
Definition
SampleFastech16PointProtocol.cs:267
SampleSmart.Pages.PageSub.IoTabs.SampleFastech16PointProtocol.ReadDigitalOutputsCommand
const string ReadDigitalOutputsCommand
Definition
SampleFastech16PointProtocol.cs:43
SampleSmart.Pages.PageSub.IoTabs.SampleFastech16PointProtocol.BuildReadAnalogOutputs
byte[] BuildReadAnalogOutputs(IReadOnlyList< AnalogIoPoint > points)
Definition
SampleFastech16PointProtocol.cs:341
SampleSmart.Pages.PageSub.IoTabs.SampleFastech16PointProtocol.BuildReadDigitalOutputs
byte[] BuildReadDigitalOutputs(IReadOnlyList< IoPoint > points)
Definition
SampleFastech16PointProtocol.cs:155
SampleSmart.Pages.PageSub.IoTabs.SampleFastech16PointProtocol.ParseDigitalInputs
IoResult< bool[]> ParseDigitalInputs(IReadOnlyList< byte > responseFrame, int count)
Definition
SampleFastech16PointProtocol.cs:126
SampleSmart.Pages.PageSub.IoTabs.SampleFastech16PointProtocol.Decode
static string Decode(IReadOnlyList< byte > bytes)
Definition
SampleFastech16PointProtocol.cs:569
SampleSmart.Pages.PageSub.IoTabs.SampleFastech16PointProtocol.ParseAnalogInputs
IoResult< double[]> ParseAnalogInputs(IReadOnlyList< byte > responseFrame, int count)
Definition
SampleFastech16PointProtocol.cs:304
SampleSmart.Pages.PageSub.IoTabs.SampleFastech16PointProtocol.BuildReadDigitalInputs
byte[] BuildReadDigitalInputs(IReadOnlyList< IoPoint > points)
Definition
SampleFastech16PointProtocol.cs:87
SampleSmart.Pages.PageSub.IoTabs.SampleFastech16PointProtocol.WriteDigitalOutputsCommand
const string WriteDigitalOutputsCommand
Definition
SampleFastech16PointProtocol.cs:52
SampleSmart.Pages.PageSub.IoTabs.SampleFastech16PointProtocol.Encode
static byte[] Encode(string text)
Definition
SampleFastech16PointProtocol.cs:540
SampleSmart.Pages.PageSub.IoTabs.SampleFastech16PointProtocol.BuildWriteAnalogOutputs
byte[] BuildWriteAnalogOutputs(IReadOnlyDictionary< AnalogIoPoint, double > values)
Definition
SampleFastech16PointProtocol.cs:415
SampleSmart.Pages.PageSub.IoTabs.SampleFastech16PointProtocol.ParseWriteResponse
IoResult ParseWriteResponse(IReadOnlyList< byte > responseFrame)
Definition
SampleFastech16PointProtocol.cs:444
SampleSmart.Pages.PageSub.IoTabs.SampleFastech16PointProtocol.ParseDigitalOutputs
IoResult< bool[]> ParseDigitalOutputs(IReadOnlyList< byte > responseFrame, int count)
Definition
SampleFastech16PointProtocol.cs:194
SampleSmart.Pages.PageSub.IoTabs.SampleFastech16PointProtocol.AckResponse
const string AckResponse
Definition
SampleFastech16PointProtocol.cs:61
SampleSmart.Pages.PageSub.IoTabs.SampleFastech16PointProtocol.BuildWriteDigitalOutputs
byte[] BuildWriteDigitalOutputs(IReadOnlyDictionary< IoPoint, bool > values)
Definition
SampleFastech16PointProtocol.cs:223
SampleSmart.Pages.PageSub.IoTabs.SampleFastech16PointProtocol.ReadDigitalInputsCommand
const string ReadDigitalInputsCommand
Definition
SampleFastech16PointProtocol.cs:34
Pages
PageSub
IoTabs
SampleFastech16PointProtocol.cs
다음에 의해 생성됨 :
1.17.0