Dreamine.PLC.Core
1.0.1
Dreamine.PLC.Core 산업 자동화 및 I/O 기능을 제공합니다.
Toggle main menu visibility
로딩중...
검색중...
일치하는것 없음
PlcSimulatorTcpClient.cs
이 파일의 문서화 페이지로 가기
1
using
System.Net.Sockets;
2
using
Dreamine.PLC.Abstractions.Devices;
3
using
Dreamine.PLC.Abstractions.Results;
4
using
Dreamine.PLC.Core.Clients
;
5
6
namespace
Dreamine.PLC.Core.Simulation
;
7
16
public
sealed
class
PlcSimulatorTcpClient
:
PlcClientBase
17
{
26
private
readonly
PlcSimulatorClientOptions
_options
;
35
private
TcpClient?
_client
;
44
private
StreamReader?
_reader
;
53
private
StreamWriter?
_writer
;
54
79
public
PlcSimulatorTcpClient
(
PlcSimulatorClientOptions
options)
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
410
return
PlcSimulatorProtocol
.
ReadOkPayload
(line);
411
}
412
}
Dreamine.PLC.Core.Clients
Definition
InMemoryPlcClient.cs:5
Dreamine.PLC.Core.Simulation
Definition
PlcSimulatorClientOptions.cs:1
Dreamine.PLC.Core.Clients.PlcClientBase
Definition
PlcClientBase.cs:19
Dreamine.PLC.Core.Simulation.PlcSimulatorClientOptions
Definition
PlcSimulatorClientOptions.cs:12
Dreamine.PLC.Core.Simulation.PlcSimulatorProtocol
Definition
PlcSimulatorProtocol.cs:14
Dreamine.PLC.Core.Simulation.PlcSimulatorProtocol.FormatWords
static string FormatWords(IEnumerable< short > values)
Definition
PlcSimulatorProtocol.cs:166
Dreamine.PLC.Core.Simulation.PlcSimulatorProtocol.ParseBits
static bool[] ParseBits(string payload)
Definition
PlcSimulatorProtocol.cs:203
Dreamine.PLC.Core.Simulation.PlcSimulatorProtocol.ReadOkPayload
static string ReadOkPayload(string line)
Definition
PlcSimulatorProtocol.cs:289
Dreamine.PLC.Core.Simulation.PlcSimulatorProtocol.FormatBits
static string FormatBits(IEnumerable< bool > values)
Definition
PlcSimulatorProtocol.cs:137
Dreamine.PLC.Core.Simulation.PlcSimulatorProtocol.ParseWords
static short[] ParseWords(string payload)
Definition
PlcSimulatorProtocol.cs:250
Dreamine.PLC.Core.Simulation.PlcSimulatorTcpClient._client
TcpClient? _client
Definition
PlcSimulatorTcpClient.cs:35
Dreamine.PLC.Core.Simulation.PlcSimulatorTcpClient._writer
StreamWriter? _writer
Definition
PlcSimulatorTcpClient.cs:53
Dreamine.PLC.Core.Simulation.PlcSimulatorTcpClient.DisconnectCoreAsync
override Task< PlcResult > DisconnectCoreAsync(CancellationToken cancellationToken)
Definition
PlcSimulatorTcpClient.cs:162
Dreamine.PLC.Core.Simulation.PlcSimulatorTcpClient.SendCommandAsync
async Task< string > SendCommandAsync(string command, CancellationToken cancellationToken)
Definition
PlcSimulatorTcpClient.cs:396
Dreamine.PLC.Core.Simulation.PlcSimulatorTcpClient._reader
StreamReader? _reader
Definition
PlcSimulatorTcpClient.cs:44
Dreamine.PLC.Core.Simulation.PlcSimulatorTcpClient.ConnectCoreAsync
override async Task< PlcResult > ConnectCoreAsync(CancellationToken cancellationToken)
Definition
PlcSimulatorTcpClient.cs:107
Dreamine.PLC.Core.Simulation.PlcSimulatorTcpClient.WriteWordsCoreAsync
override async Task< PlcResult > WriteWordsCoreAsync(PlcAddress address, IReadOnlyList< short > values, CancellationToken cancellationToken)
Definition
PlcSimulatorTcpClient.cs:343
Dreamine.PLC.Core.Simulation.PlcSimulatorTcpClient.ReadBitsCoreAsync
override async Task< PlcResult< bool[]> > ReadBitsCoreAsync(PlcAddress address, int count, CancellationToken cancellationToken)
Definition
PlcSimulatorTcpClient.cs:213
Dreamine.PLC.Core.Simulation.PlcSimulatorTcpClient.PlcSimulatorTcpClient
PlcSimulatorTcpClient(PlcSimulatorClientOptions options)
Definition
PlcSimulatorTcpClient.cs:79
Dreamine.PLC.Core.Simulation.PlcSimulatorTcpClient.WriteBitsCoreAsync
override async Task< PlcResult > WriteBitsCoreAsync(PlcAddress address, IReadOnlyList< bool > values, CancellationToken cancellationToken)
Definition
PlcSimulatorTcpClient.cs:299
Dreamine.PLC.Core.Simulation.PlcSimulatorTcpClient._options
readonly PlcSimulatorClientOptions _options
Definition
PlcSimulatorTcpClient.cs:26
Dreamine.PLC.Core.Simulation.PlcSimulatorTcpClient.ReadWordsCoreAsync
override async Task< PlcResult< short[]> > ReadWordsCoreAsync(PlcAddress address, int count, CancellationToken cancellationToken)
Definition
PlcSimulatorTcpClient.cs:256
Simulation
PlcSimulatorTcpClient.cs
다음에 의해 생성됨 :
1.17.0