Dreamine.PLC.Core 1.0.1
Dreamine.PLC.Core 산업 자동화 및 I/O 기능을 제공합니다.
로딩중...
검색중...
일치하는것 없음
PlcSimulatorTcpClient.cs
이 파일의 문서화 페이지로 가기
1using System.Net.Sockets;
2using Dreamine.PLC.Abstractions.Devices;
3using Dreamine.PLC.Abstractions.Results;
5
7
17{
35 private TcpClient? _client;
44 private StreamReader? _reader;
53 private StreamWriter? _writer;
54
80 {
81 _options = options ?? throw new ArgumentNullException(nameof(options));
82 }
83
107 protected override async Task<PlcResult> ConnectCoreAsync(CancellationToken cancellationToken)
108 {
109 var client = new TcpClient();
110 using var timeoutCts = new CancellationTokenSource(_options.ConnectTimeoutMs);
111 using var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutCts.Token);
112
113 try
114 {
115 await client.ConnectAsync(_options.Host, _options.Port, linkedCts.Token).ConfigureAwait(false);
116
117 var stream = client.GetStream();
118 _reader = new StreamReader(stream);
119 _writer = new StreamWriter(stream) { AutoFlush = true };
120 _client = client;
121
122 return PlcResult.Success();
123 }
124 catch
125 {
126 client.Dispose();
127 throw;
128 }
129 }
130
162 protected override Task<PlcResult> DisconnectCoreAsync(CancellationToken cancellationToken)
163 {
164 cancellationToken.ThrowIfCancellationRequested();
165
166 _writer?.Dispose();
167 _reader?.Dispose();
168 _client?.Dispose();
169 _writer = null;
170 _reader = null;
171 _client = null;
172
173 return Task.FromResult(PlcResult.Success());
174 }
175
213 protected override async Task<PlcResult<bool[]>> ReadBitsCoreAsync(PlcAddress address, int count, CancellationToken cancellationToken)
214 {
215 var payload = await SendCommandAsync($"{PlcSimulatorProtocol.ReadBits} {address} {count}", cancellationToken).ConfigureAwait(false);
216 return PlcResult<bool[]>.Success(PlcSimulatorProtocol.ParseBits(payload));
217 }
218
256 protected override async Task<PlcResult<short[]>> ReadWordsCoreAsync(PlcAddress address, int count, CancellationToken cancellationToken)
257 {
258 var payload = await SendCommandAsync($"{PlcSimulatorProtocol.ReadWords} {address} {count}", cancellationToken).ConfigureAwait(false);
259 return PlcResult<short[]>.Success(PlcSimulatorProtocol.ParseWords(payload));
260 }
261
299 protected override async Task<PlcResult> WriteBitsCoreAsync(PlcAddress address, IReadOnlyList<bool> values, CancellationToken cancellationToken)
300 {
301 var valuesText = PlcSimulatorProtocol.FormatBits(values);
302 await SendCommandAsync($"{PlcSimulatorProtocol.WriteBits} {address} {valuesText}", cancellationToken).ConfigureAwait(false);
303 return PlcResult.Success();
304 }
305
343 protected override async Task<PlcResult> WriteWordsCoreAsync(PlcAddress address, IReadOnlyList<short> values, CancellationToken cancellationToken)
344 {
345 var valuesText = PlcSimulatorProtocol.FormatWords(values);
346 await SendCommandAsync($"{PlcSimulatorProtocol.WriteWords} {address} {valuesText}", cancellationToken).ConfigureAwait(false);
347 return PlcResult.Success();
348 }
349
396 private async Task<string> SendCommandAsync(string command, CancellationToken cancellationToken)
397 {
398 if (_writer is null || _reader is null)
399 {
400 throw new InvalidOperationException("The PLC simulator TCP client is not connected.");
401 }
402
403 await _writer.WriteLineAsync(command.AsMemory(), cancellationToken).ConfigureAwait(false);
404 var line = await _reader.ReadLineAsync(cancellationToken).ConfigureAwait(false);
405 if (line is null)
406 {
407 throw new IOException("The PLC simulator server closed the connection.");
408 }
409
411 }
412}
static string FormatWords(IEnumerable< short > values)
static string FormatBits(IEnumerable< bool > values)
override Task< PlcResult > DisconnectCoreAsync(CancellationToken cancellationToken)
async Task< string > SendCommandAsync(string command, CancellationToken cancellationToken)
override async Task< PlcResult > ConnectCoreAsync(CancellationToken cancellationToken)
override async Task< PlcResult > WriteWordsCoreAsync(PlcAddress address, IReadOnlyList< short > values, CancellationToken cancellationToken)
override async Task< PlcResult< bool[]> > ReadBitsCoreAsync(PlcAddress address, int count, CancellationToken cancellationToken)
PlcSimulatorTcpClient(PlcSimulatorClientOptions options)
override async Task< PlcResult > WriteBitsCoreAsync(PlcAddress address, IReadOnlyList< bool > values, CancellationToken cancellationToken)
override async Task< PlcResult< short[]> > ReadWordsCoreAsync(PlcAddress address, int count, CancellationToken cancellationToken)