Dreamine.PLC.Mitsubishi.MC 1.0.1
Dreamine.PLC.Mitsubishi.MC 산업 자동화 및 I/O 기능을 제공합니다.
로딩중...
검색중...
일치하는것 없음
MitsubishiMcPlcClient.cs
이 파일의 문서화 페이지로 가기
1using Dreamine.PLC.Abstractions.Devices;
2using Dreamine.PLC.Abstractions.Results;
3using Dreamine.PLC.Core.Clients;
7
9
18public sealed class MitsubishiMcPlcClient : PlcClientBase
19{
56
95
142 IMitsubishiMcTransport transport,
145 {
146 _options = options ?? throw new ArgumentNullException(nameof(options));
147 _transport = transport ?? throw new ArgumentNullException(nameof(transport));
148 _frameBuilder = frameBuilder ?? throw new ArgumentNullException(nameof(frameBuilder));
149 _responseParser = responseParser ?? throw new ArgumentNullException(nameof(responseParser));
150 }
151
161
199 {
200 ArgumentNullException.ThrowIfNull(options);
201
202 return options.TransportType switch
203 {
206 _ => throw new ArgumentOutOfRangeException(nameof(options), options.TransportType, "Unsupported Mitsubishi MC transport type.")
207 };
208 }
209
232 protected override Task<PlcResult> ConnectCoreAsync(CancellationToken cancellationToken)
233 {
234 return _transport.ConnectAsync(
235 _options.Host,
236 _options.Port,
237 _options.ConnectTimeoutMs,
238 cancellationToken);
239 }
240
263 protected override Task<PlcResult> DisconnectCoreAsync(CancellationToken cancellationToken)
264 {
265 return _transport.DisconnectAsync(cancellationToken);
266 }
267
304 protected override async Task<PlcResult<bool[]>> ReadBitsCoreAsync(
305 PlcAddress address,
306 int count,
307 CancellationToken cancellationToken)
308 {
309 var frameResult = _frameBuilder.BuildBatchReadFrame(
310 _options,
311 address,
312 count,
313 isBitAccess: true);
314
315 if (!frameResult.IsSuccess || frameResult.Value is null)
316 {
317 return PlcResult<bool[]>.Failure(
318 frameResult.Message ?? "Failed to build Mitsubishi MC bit read frame.",
319 frameResult.ErrorCode);
320 }
321
322 var responseResult = await _transport.SendAndReceiveAsync(
323 frameResult.Value,
324 _options.ReceiveTimeoutMs,
325 _options.RetryCount,
326 cancellationToken).ConfigureAwait(false);
327
328 if (!responseResult.IsSuccess || responseResult.Value is null)
329 {
330 return PlcResult<bool[]>.Failure(
331 responseResult.Message ?? "Failed to receive Mitsubishi MC bit read response.",
332 responseResult.ErrorCode);
333 }
334
335 return _responseParser.ParseReadBits(responseResult.Value, count);
336 }
337
374 protected override async Task<PlcResult<short[]>> ReadWordsCoreAsync(
375 PlcAddress address,
376 int count,
377 CancellationToken cancellationToken)
378 {
379 var frameResult = _frameBuilder.BuildBatchReadFrame(
380 _options,
381 address,
382 count,
383 isBitAccess: false);
384
385 if (!frameResult.IsSuccess || frameResult.Value is null)
386 {
387 return PlcResult<short[]>.Failure(
388 frameResult.Message ?? "Failed to build Mitsubishi MC word read frame.",
389 frameResult.ErrorCode);
390 }
391
392 var responseResult = await _transport.SendAndReceiveAsync(
393 frameResult.Value,
394 _options.ReceiveTimeoutMs,
395 _options.RetryCount,
396 cancellationToken).ConfigureAwait(false);
397
398 if (!responseResult.IsSuccess || responseResult.Value is null)
399 {
400 return PlcResult<short[]>.Failure(
401 responseResult.Message ?? "Failed to receive Mitsubishi MC word read response.",
402 responseResult.ErrorCode);
403 }
404
405 return _responseParser.ParseReadWords(responseResult.Value, count);
406 }
407
444 protected override async Task<PlcResult> WriteBitsCoreAsync(
445 PlcAddress address,
446 IReadOnlyList<bool> values,
447 CancellationToken cancellationToken)
448 {
449 var frameResult = _frameBuilder.BuildBatchWriteBitsFrame(
450 _options,
451 address,
452 values);
453
454 if (!frameResult.IsSuccess || frameResult.Value is null)
455 {
456 return PlcResult.Failure(
457 frameResult.Message ?? "Failed to build Mitsubishi MC bit write frame.",
458 frameResult.ErrorCode);
459 }
460
461 var responseResult = await _transport.SendAndReceiveAsync(
462 frameResult.Value,
463 _options.ReceiveTimeoutMs,
464 _options.RetryCount,
465 cancellationToken).ConfigureAwait(false);
466
467 if (!responseResult.IsSuccess || responseResult.Value is null)
468 {
469 return PlcResult.Failure(
470 responseResult.Message ?? "Failed to receive Mitsubishi MC bit write response.",
471 responseResult.ErrorCode);
472 }
473
474 var parseResult = _responseParser.Parse(responseResult.Value);
475 if (!parseResult.IsSuccess)
476 {
477 return PlcResult.Failure(
478 parseResult.Message ?? "Failed to parse Mitsubishi MC bit write response.",
479 parseResult.ErrorCode);
480 }
481
482 return PlcResult.Success();
483 }
484
521 protected override async Task<PlcResult> WriteWordsCoreAsync(
522 PlcAddress address,
523 IReadOnlyList<short> values,
524 CancellationToken cancellationToken)
525 {
526 var frameResult = _frameBuilder.BuildBatchWriteWordsFrame(
527 _options,
528 address,
529 values);
530
531 if (!frameResult.IsSuccess || frameResult.Value is null)
532 {
533 return PlcResult.Failure(
534 frameResult.Message ?? "Failed to build Mitsubishi MC word write frame.",
535 frameResult.ErrorCode);
536 }
537
538 var responseResult = await _transport.SendAndReceiveAsync(
539 frameResult.Value,
540 _options.ReceiveTimeoutMs,
541 _options.RetryCount,
542 cancellationToken).ConfigureAwait(false);
543
544 if (!responseResult.IsSuccess || responseResult.Value is null)
545 {
546 return PlcResult.Failure(
547 responseResult.Message ?? "Failed to receive Mitsubishi MC word write response.",
548 responseResult.ErrorCode);
549 }
550
551 var parseResult = _responseParser.Parse(responseResult.Value);
552 if (!parseResult.IsSuccess)
553 {
554 return PlcResult.Failure(
555 parseResult.Message ?? "Failed to parse Mitsubishi MC word write response.",
556 parseResult.ErrorCode);
557 }
558
559 return PlcResult.Success();
560 }
561
577 public override async ValueTask DisposeAsync()
578 {
579 await base.DisposeAsync().ConfigureAwait(false);
580 await _transport.DisposeAsync().ConfigureAwait(false);
581 }
582}
override Task< PlcResult > DisconnectCoreAsync(CancellationToken cancellationToken)
readonly MitsubishiMcBinary3EResponseParser _responseParser
MitsubishiMcPlcClient(MitsubishiMcConnectionOptions options)
MitsubishiMcPlcClient(MitsubishiMcConnectionOptions options, IMitsubishiMcTransport transport, MitsubishiMcBinary3EFrameBuilder frameBuilder, MitsubishiMcBinary3EResponseParser responseParser)
static IMitsubishiMcTransport CreateTransport(MitsubishiMcConnectionOptions options)
override async Task< PlcResult< bool[]> > ReadBitsCoreAsync(PlcAddress address, int count, CancellationToken cancellationToken)
readonly MitsubishiMcBinary3EFrameBuilder _frameBuilder
override Task< PlcResult > ConnectCoreAsync(CancellationToken cancellationToken)
override async Task< PlcResult< short[]> > ReadWordsCoreAsync(PlcAddress address, int count, CancellationToken cancellationToken)
override async Task< PlcResult > WriteBitsCoreAsync(PlcAddress address, IReadOnlyList< bool > values, CancellationToken cancellationToken)
override async Task< PlcResult > WriteWordsCoreAsync(PlcAddress address, IReadOnlyList< short > values, CancellationToken cancellationToken)