Dreamine.PLC.Omron.CxComponent
1.0.1
Dreamine.PLC.Omron.CxComponent 산업 자동화 및 I/O 기능을 제공합니다.
Toggle main menu visibility
로딩중...
검색중...
일치하는것 없음
OmronCxComponentPlcClient.cs
이 파일의 문서화 페이지로 가기
1
using
System.Globalization;
2
using
System.Runtime.InteropServices;
3
using
Dreamine.PLC.Abstractions.Devices;
4
using
Dreamine.PLC.Abstractions.Results;
5
using
Dreamine.PLC.Core.Clients;
6
using
Dreamine.PLC.Omron.CxComponent.Devices
;
7
using
Dreamine.PLC.Omron.CxComponent.Internal
;
8
using
Dreamine.PLC.Omron.CxComponent.Options
;
9
10
namespace
Dreamine.PLC.Omron.CxComponent.Clients
;
11
20
public
sealed
class
OmronCxComponentPlcClient
: PlcClientBase
21
{
30
private
readonly
OmronCxComponentOptions
_options
;
39
private
readonly
IComObjectFactory
_factory
;
48
private
object
?
_component
;
49
74
public
OmronCxComponentPlcClient
(
OmronCxComponentOptions
options)
75
: this(options, new
DefaultComObjectFactory
())
76
{
77
}
78
111
public
OmronCxComponentPlcClient
(
OmronCxComponentOptions
options,
IComObjectFactory
factory)
112
{
113
_options
= options ??
throw
new
ArgumentNullException(nameof(options));
114
_factory
= factory ??
throw
new
ArgumentNullException(nameof(factory));
115
}
116
125
public
OmronCxComponentOptions
Options
=>
_options
;
126
175
protected
override
Task<PlcResult>
ConnectCoreAsync
(CancellationToken cancellationToken)
176
{
177
cancellationToken.ThrowIfCancellationRequested();
178
179
_component
=
_factory
.Create(
_options
.ProgId);
180
TrySetProperty
(
_component
,
_options
.PeerAddressPropertyName,
_options
.PeerAddress);
181
ComInvoker.SetProperty(
_component
,
_options
.ActivePropertyName,
true
);
182
183
return
Task.FromResult(PlcResult.Success());
184
}
185
226
protected
override
Task<PlcResult>
DisconnectCoreAsync
(CancellationToken cancellationToken)
227
{
228
cancellationToken.ThrowIfCancellationRequested();
229
230
if
(
_component
is
null
)
231
{
232
return
Task.FromResult(PlcResult.Success());
233
}
234
235
TrySetProperty
(
_component
,
_options
.ActivePropertyName,
false
);
236
ReleaseComponent
();
237
return
Task.FromResult(PlcResult.Success());
238
}
239
296
protected
override
Task<PlcResult<bool[]>>
ReadBitsCoreAsync
(
297
PlcAddress address,
298
int
count,
299
CancellationToken cancellationToken)
300
{
301
var component =
RequireComponent
();
302
var values =
new
bool
[count];
303
304
try
305
{
306
for
(var index = 0; index < count; index++)
307
{
308
cancellationToken.ThrowIfCancellationRequested();
309
310
var raw = ComInvoker.Invoke(
311
component,
312
_options
.ReadVariableMethodName,
313
OmronCxAddressNameFormatter
.
FormatOffset
(address, index));
314
315
values[index] = Convert.ToInt32(raw, CultureInfo.InvariantCulture) != 0;
316
}
317
}
318
catch
(Exception ex) when (ex is not OperationCanceledException)
319
{
320
return
Task.FromResult(PlcResult<
bool
[]>.Failure($
"CX-Compolet bit read failed. {ex.Message}"
));
321
}
322
323
return
Task.FromResult(PlcResult<
bool
[]>.Success(values));
324
}
325
382
protected
override
Task<PlcResult<short[]>>
ReadWordsCoreAsync
(
383
PlcAddress address,
384
int
count,
385
CancellationToken cancellationToken)
386
{
387
var component =
RequireComponent
();
388
var values =
new
short
[count];
389
390
try
391
{
392
for
(var index = 0; index < count; index++)
393
{
394
cancellationToken.ThrowIfCancellationRequested();
395
396
var raw = ComInvoker.Invoke(
397
component,
398
_options
.ReadVariableMethodName,
399
OmronCxAddressNameFormatter
.
FormatOffset
(address, index));
400
401
values[index] = Convert.ToInt16(raw, CultureInfo.InvariantCulture);
402
}
403
}
404
catch
(Exception ex) when (ex is not OperationCanceledException)
405
{
406
return
Task.FromResult(PlcResult<
short
[]>.Failure($
"CX-Compolet word read failed. {ex.Message}"
));
407
}
408
409
return
Task.FromResult(PlcResult<
short
[]>.Success(values));
410
}
411
468
protected
override
Task<PlcResult>
WriteBitsCoreAsync
(
469
PlcAddress address,
470
IReadOnlyList<bool> values,
471
CancellationToken cancellationToken)
472
{
473
var component =
RequireComponent
();
474
475
try
476
{
477
for
(var index = 0; index < values.Count; index++)
478
{
479
cancellationToken.ThrowIfCancellationRequested();
480
481
ComInvoker.Invoke(
482
component,
483
_options
.WriteVariableMethodName,
484
OmronCxAddressNameFormatter
.
FormatOffset
(address, index),
485
values[index] ? 1 : 0);
486
}
487
}
488
catch
(Exception ex) when (ex is not OperationCanceledException)
489
{
490
return
Task.FromResult(PlcResult.Failure($
"CX-Compolet bit write failed. {ex.Message}"
));
491
}
492
493
return
Task.FromResult(PlcResult.Success());
494
}
495
552
protected
override
Task<PlcResult>
WriteWordsCoreAsync
(
553
PlcAddress address,
554
IReadOnlyList<short> values,
555
CancellationToken cancellationToken)
556
{
557
var component =
RequireComponent
();
558
559
try
560
{
561
for
(var index = 0; index < values.Count; index++)
562
{
563
cancellationToken.ThrowIfCancellationRequested();
564
565
ComInvoker.Invoke(
566
component,
567
_options
.WriteVariableMethodName,
568
OmronCxAddressNameFormatter
.
FormatOffset
(address, index),
569
values[index]);
570
}
571
}
572
catch
(Exception ex) when (ex is not OperationCanceledException)
573
{
574
return
Task.FromResult(PlcResult.Failure($
"CX-Compolet word write failed. {ex.Message}"
));
575
}
576
577
return
Task.FromResult(PlcResult.Success());
578
}
579
596
public
override
async ValueTask
DisposeAsync
()
597
{
598
await base.DisposeAsync().ConfigureAwait(
false
);
599
ReleaseComponent
();
600
}
601
642
private
static
void
TrySetProperty
(
object
target,
string
name,
object
? value)
643
{
644
try
645
{
646
ComInvoker.SetProperty(target, name, value);
647
}
648
catch
(MissingMethodException)
649
{
650
// CX-Compolet controls vary by PLC family; optional properties are intentionally ignored.
651
}
652
}
653
678
private
object
RequireComponent
()
679
{
680
return
_component
??
throw
new
InvalidOperationException(
"CX-Compolet is not connected."
);
681
}
682
691
private
void
ReleaseComponent
()
692
{
693
if
(
_component
is
null
)
694
{
695
return
;
696
}
697
698
if
(OperatingSystem.IsWindows() && Marshal.IsComObject(
_component
))
699
{
700
Marshal.FinalReleaseComObject(
_component
);
701
}
702
703
_component
=
null
;
704
}
705
}
Dreamine.PLC.Omron.CxComponent.Clients
Definition
OmronCxComponentPlcClient.cs:10
Dreamine.PLC.Omron.CxComponent.Devices
Definition
OmronCxAddressNameFormatter.cs:4
Dreamine.PLC.Omron.CxComponent.Internal
Definition
ComInvoker.cs:5
Dreamine.PLC.Omron.CxComponent.Options
Definition
OmronCxComponentOptions.cs:1
Dreamine.PLC.Omron.CxComponent.Clients.OmronCxComponentPlcClient.OmronCxComponentPlcClient
OmronCxComponentPlcClient(OmronCxComponentOptions options, IComObjectFactory factory)
Definition
OmronCxComponentPlcClient.cs:111
Dreamine.PLC.Omron.CxComponent.Clients.OmronCxComponentPlcClient._options
readonly OmronCxComponentOptions _options
Definition
OmronCxComponentPlcClient.cs:30
Dreamine.PLC.Omron.CxComponent.Clients.OmronCxComponentPlcClient.WriteWordsCoreAsync
override Task< PlcResult > WriteWordsCoreAsync(PlcAddress address, IReadOnlyList< short > values, CancellationToken cancellationToken)
Definition
OmronCxComponentPlcClient.cs:552
Dreamine.PLC.Omron.CxComponent.Clients.OmronCxComponentPlcClient.ReadBitsCoreAsync
override Task< PlcResult< bool[]> > ReadBitsCoreAsync(PlcAddress address, int count, CancellationToken cancellationToken)
Definition
OmronCxComponentPlcClient.cs:296
Dreamine.PLC.Omron.CxComponent.Clients.OmronCxComponentPlcClient.TrySetProperty
static void TrySetProperty(object target, string name, object? value)
Definition
OmronCxComponentPlcClient.cs:642
Dreamine.PLC.Omron.CxComponent.Clients.OmronCxComponentPlcClient.ConnectCoreAsync
override Task< PlcResult > ConnectCoreAsync(CancellationToken cancellationToken)
Definition
OmronCxComponentPlcClient.cs:175
Dreamine.PLC.Omron.CxComponent.Clients.OmronCxComponentPlcClient.RequireComponent
object RequireComponent()
Definition
OmronCxComponentPlcClient.cs:678
Dreamine.PLC.Omron.CxComponent.Clients.OmronCxComponentPlcClient.ReleaseComponent
void ReleaseComponent()
Definition
OmronCxComponentPlcClient.cs:691
Dreamine.PLC.Omron.CxComponent.Clients.OmronCxComponentPlcClient.OmronCxComponentPlcClient
OmronCxComponentPlcClient(OmronCxComponentOptions options)
Definition
OmronCxComponentPlcClient.cs:74
Dreamine.PLC.Omron.CxComponent.Clients.OmronCxComponentPlcClient._factory
readonly IComObjectFactory _factory
Definition
OmronCxComponentPlcClient.cs:39
Dreamine.PLC.Omron.CxComponent.Clients.OmronCxComponentPlcClient.ReadWordsCoreAsync
override Task< PlcResult< short[]> > ReadWordsCoreAsync(PlcAddress address, int count, CancellationToken cancellationToken)
Definition
OmronCxComponentPlcClient.cs:382
Dreamine.PLC.Omron.CxComponent.Clients.OmronCxComponentPlcClient.WriteBitsCoreAsync
override Task< PlcResult > WriteBitsCoreAsync(PlcAddress address, IReadOnlyList< bool > values, CancellationToken cancellationToken)
Definition
OmronCxComponentPlcClient.cs:468
Dreamine.PLC.Omron.CxComponent.Clients.OmronCxComponentPlcClient._component
object? _component
Definition
OmronCxComponentPlcClient.cs:48
Dreamine.PLC.Omron.CxComponent.Clients.OmronCxComponentPlcClient.Options
OmronCxComponentOptions Options
Definition
OmronCxComponentPlcClient.cs:125
Dreamine.PLC.Omron.CxComponent.Clients.OmronCxComponentPlcClient.DisposeAsync
override async ValueTask DisposeAsync()
Definition
OmronCxComponentPlcClient.cs:596
Dreamine.PLC.Omron.CxComponent.Clients.OmronCxComponentPlcClient.DisconnectCoreAsync
override Task< PlcResult > DisconnectCoreAsync(CancellationToken cancellationToken)
Definition
OmronCxComponentPlcClient.cs:226
Dreamine.PLC.Omron.CxComponent.Devices.OmronCxAddressNameFormatter
Definition
OmronCxAddressNameFormatter.cs:15
Dreamine.PLC.Omron.CxComponent.Devices.OmronCxAddressNameFormatter.FormatOffset
static string FormatOffset(PlcAddress address, int delta)
Definition
OmronCxAddressNameFormatter.cs:107
Dreamine.PLC.Omron.CxComponent.Internal.DefaultComObjectFactory
Definition
DefaultComObjectFactory.cs:12
Dreamine.PLC.Omron.CxComponent.Internal.IComObjectFactory
Definition
IComObjectFactory.cs:12
Dreamine.PLC.Omron.CxComponent.Options.OmronCxComponentOptions
Definition
OmronCxComponentOptions.cs:12
Clients
OmronCxComponentPlcClient.cs
다음에 의해 생성됨 :
1.17.0