Dreamine.PLC.Mitsubishi.MC
1.0.1
Dreamine.PLC.Mitsubishi.MC 산업 자동화 및 I/O 기능을 제공합니다.
Toggle main menu visibility
로딩중...
검색중...
일치하는것 없음
MitsubishiMcBinary3EFrameBuilder.cs
이 파일의 문서화 페이지로 가기
1
using
Dreamine.PLC.Abstractions.Devices;
2
using
Dreamine.PLC.Abstractions.Results;
3
using
Dreamine.PLC.Mitsubishi.MC.Devices
;
4
using
Dreamine.PLC.Mitsubishi.MC.Options
;
5
6
namespace
Dreamine.PLC.Mitsubishi.MC.Protocol
;
7
16
public
sealed
class
MitsubishiMcBinary3EFrameBuilder
17
{
26
private
const
ushort
SubHeader
= 0x5000;
35
private
readonly
MitsubishiMcDeviceCodeMapper
_deviceCodeMapper
;
36
45
public
MitsubishiMcBinary3EFrameBuilder
()
46
: this(new
MitsubishiMcDeviceCodeMapper
())
47
{
48
}
49
74
public
MitsubishiMcBinary3EFrameBuilder
(
MitsubishiMcDeviceCodeMapper
deviceCodeMapper)
75
{
76
_deviceCodeMapper
= deviceCodeMapper ??
throw
new
ArgumentNullException(nameof(deviceCodeMapper));
77
}
78
145
public
PlcResult<byte[]>
BuildBatchReadFrame
(
146
MitsubishiMcConnectionOptions
options,
147
PlcAddress address,
148
int
count,
149
bool
isBitAccess)
150
{
151
ArgumentNullException.ThrowIfNull(options);
152
153
if
(count <= 0)
154
{
155
return
PlcResult<byte[]>.Failure(
"The read count must be greater than zero."
);
156
}
157
158
var deviceCodeResult =
_deviceCodeMapper
.Map(address.DeviceType);
159
if
(!deviceCodeResult.IsSuccess)
160
{
161
return
PlcResult<byte[]>.Failure(
162
deviceCodeResult.Message ??
"Unsupported Mitsubishi MC device type."
,
163
deviceCodeResult.ErrorCode);
164
}
165
166
var payload =
BuildBatchAccessPayload
(
167
MitsubishiMcCommand
.BatchRead,
168
isBitAccess ?
MitsubishiMcSubCommand
.Bit :
MitsubishiMcSubCommand
.Word,
169
address,
170
count,
171
deviceCodeResult.Value);
172
173
return
PlcResult<byte[]>.Success(
BuildFrame
(options, payload));
174
}
175
235
public
PlcResult<byte[]>
BuildBatchWriteWordsFrame
(
236
MitsubishiMcConnectionOptions
options,
237
PlcAddress address,
238
IReadOnlyList<short> values)
239
{
240
ArgumentNullException.ThrowIfNull(options);
241
ArgumentNullException.ThrowIfNull(values);
242
243
if
(values.Count == 0)
244
{
245
return
PlcResult<byte[]>.Failure(
"The word value collection must not be empty."
);
246
}
247
248
var deviceCodeResult =
_deviceCodeMapper
.Map(address.DeviceType);
249
if
(!deviceCodeResult.IsSuccess)
250
{
251
return
PlcResult<byte[]>.Failure(
252
deviceCodeResult.Message ??
"Unsupported Mitsubishi MC device type."
,
253
deviceCodeResult.ErrorCode);
254
}
255
256
var payload =
BuildBatchAccessPayload
(
257
MitsubishiMcCommand
.BatchWrite,
258
MitsubishiMcSubCommand
.Word,
259
address,
260
values.Count,
261
deviceCodeResult.Value);
262
263
foreach
(var value
in
values)
264
{
265
MitsubishiMcEndian
.
WriteUInt16LittleEndian
(payload, unchecked((ushort)value));
266
}
267
268
return
PlcResult<byte[]>.Success(
BuildFrame
(options, payload));
269
}
270
330
public
PlcResult<byte[]>
BuildBatchWriteBitsFrame
(
331
MitsubishiMcConnectionOptions
options,
332
PlcAddress address,
333
IReadOnlyList<bool> values)
334
{
335
ArgumentNullException.ThrowIfNull(options);
336
ArgumentNullException.ThrowIfNull(values);
337
338
if
(values.Count == 0)
339
{
340
return
PlcResult<byte[]>.Failure(
"The bit value collection must not be empty."
);
341
}
342
343
var deviceCodeResult =
_deviceCodeMapper
.Map(address.DeviceType);
344
if
(!deviceCodeResult.IsSuccess)
345
{
346
return
PlcResult<byte[]>.Failure(
347
deviceCodeResult.Message ??
"Unsupported Mitsubishi MC device type."
,
348
deviceCodeResult.ErrorCode);
349
}
350
351
var payload =
BuildBatchAccessPayload
(
352
MitsubishiMcCommand
.BatchWrite,
353
MitsubishiMcSubCommand
.Bit,
354
address,
355
values.Count,
356
deviceCodeResult.Value);
357
358
for
(var i = 0; i < values.Count; i += 2)
359
{
360
var high = values[i] ? 0x10 : 0x00;
361
var low = i + 1 < values.Count && values[i + 1] ? 0x01 : 0x00;
362
363
payload.Add((
byte
)(high | low));
364
}
365
366
return
PlcResult<byte[]>.Success(
BuildFrame
(options, payload));
367
}
368
420
private
static
List<byte>
BuildBatchAccessPayload
(
421
MitsubishiMcCommand
command,
422
MitsubishiMcSubCommand
subCommand,
423
PlcAddress address,
424
int
count,
425
MitsubishiMcDeviceCode
deviceCode)
426
{
427
const
int
batchAccessHeaderLength = 10;
428
var payload =
new
List<byte>(batchAccessHeaderLength);
429
430
MitsubishiMcEndian
.
WriteUInt16LittleEndian
(payload, (ushort)command);
431
MitsubishiMcEndian
.
WriteUInt16LittleEndian
(payload, (ushort)subCommand);
432
MitsubishiMcEndian
.
WriteUInt24LittleEndian
(payload, address.Offset);
433
payload.Add((
byte
)deviceCode);
434
MitsubishiMcEndian
.
WriteUInt16LittleEndian
(payload, checked((ushort)count));
435
436
return
payload;
437
}
438
469
private
static
byte
[]
BuildFrame
(
470
MitsubishiMcConnectionOptions
options,
471
IReadOnlyCollection<byte> payload)
472
{
473
const
int
frameHeaderLength = 11;
474
var frame =
new
List<byte>(frameHeaderLength + payload.Count);
475
476
MitsubishiMcEndian
.
WriteUInt16LittleEndian
(frame,
SubHeader
);
477
frame.Add(options.
NetworkNumber
);
478
frame.Add(options.
PlcNumber
);
479
MitsubishiMcEndian
.
WriteUInt16LittleEndian
(frame, options.
DestinationModuleIoNumber
);
480
frame.Add(options.
DestinationModuleStationNumber
);
481
482
var requestDataLength = checked((ushort)(payload.Count + 2));
483
MitsubishiMcEndian
.
WriteUInt16LittleEndian
(frame, requestDataLength);
484
MitsubishiMcEndian
.
WriteUInt16LittleEndian
(frame, options.
MonitoringTimer
);
485
486
frame.AddRange(payload);
487
488
return
frame.ToArray();
489
}
490
}
Dreamine.PLC.Mitsubishi.MC.Devices
Definition
MitsubishiMcDeviceCodeMapper.cs:5
Dreamine.PLC.Mitsubishi.MC.Options
Definition
MitsubishiMcConnectionOptions.cs:3
Dreamine.PLC.Mitsubishi.MC.Protocol
Definition
MitsubishiMcBinary3EFrameBuilder.cs:6
Dreamine.PLC.Mitsubishi.MC.Protocol.MitsubishiMcSubCommand
MitsubishiMcSubCommand
Definition
MitsubishiMcSubCommand.cs:12
Dreamine.PLC.Mitsubishi.MC.Protocol.MitsubishiMcDeviceCode
MitsubishiMcDeviceCode
Definition
MitsubishiMcDeviceCode.cs:12
Dreamine.PLC.Mitsubishi.MC.Protocol.MitsubishiMcCommand
MitsubishiMcCommand
Definition
MitsubishiMcCommand.cs:12
Dreamine.PLC.Mitsubishi.MC.Devices.MitsubishiMcDeviceCodeMapper
Definition
MitsubishiMcDeviceCodeMapper.cs:16
Dreamine.PLC.Mitsubishi.MC.Options.MitsubishiMcConnectionOptions
Definition
MitsubishiMcConnectionOptions.cs:14
Dreamine.PLC.Mitsubishi.MC.Options.MitsubishiMcConnectionOptions.PlcNumber
byte PlcNumber
Definition
MitsubishiMcConnectionOptions.cs:53
Dreamine.PLC.Mitsubishi.MC.Options.MitsubishiMcConnectionOptions.DestinationModuleIoNumber
ushort DestinationModuleIoNumber
Definition
MitsubishiMcConnectionOptions.cs:63
Dreamine.PLC.Mitsubishi.MC.Options.MitsubishiMcConnectionOptions.NetworkNumber
byte NetworkNumber
Definition
MitsubishiMcConnectionOptions.cs:43
Dreamine.PLC.Mitsubishi.MC.Options.MitsubishiMcConnectionOptions.MonitoringTimer
ushort MonitoringTimer
Definition
MitsubishiMcConnectionOptions.cs:83
Dreamine.PLC.Mitsubishi.MC.Options.MitsubishiMcConnectionOptions.DestinationModuleStationNumber
byte DestinationModuleStationNumber
Definition
MitsubishiMcConnectionOptions.cs:73
Dreamine.PLC.Mitsubishi.MC.Protocol.MitsubishiMcBinary3EFrameBuilder.MitsubishiMcBinary3EFrameBuilder
MitsubishiMcBinary3EFrameBuilder()
Definition
MitsubishiMcBinary3EFrameBuilder.cs:45
Dreamine.PLC.Mitsubishi.MC.Protocol.MitsubishiMcBinary3EFrameBuilder.BuildBatchAccessPayload
static List< byte > BuildBatchAccessPayload(MitsubishiMcCommand command, MitsubishiMcSubCommand subCommand, PlcAddress address, int count, MitsubishiMcDeviceCode deviceCode)
Definition
MitsubishiMcBinary3EFrameBuilder.cs:420
Dreamine.PLC.Mitsubishi.MC.Protocol.MitsubishiMcBinary3EFrameBuilder.SubHeader
const ushort SubHeader
Definition
MitsubishiMcBinary3EFrameBuilder.cs:26
Dreamine.PLC.Mitsubishi.MC.Protocol.MitsubishiMcBinary3EFrameBuilder.BuildFrame
static byte[] BuildFrame(MitsubishiMcConnectionOptions options, IReadOnlyCollection< byte > payload)
Definition
MitsubishiMcBinary3EFrameBuilder.cs:469
Dreamine.PLC.Mitsubishi.MC.Protocol.MitsubishiMcBinary3EFrameBuilder._deviceCodeMapper
readonly MitsubishiMcDeviceCodeMapper _deviceCodeMapper
Definition
MitsubishiMcBinary3EFrameBuilder.cs:35
Dreamine.PLC.Mitsubishi.MC.Protocol.MitsubishiMcBinary3EFrameBuilder.BuildBatchWriteWordsFrame
PlcResult< byte[]> BuildBatchWriteWordsFrame(MitsubishiMcConnectionOptions options, PlcAddress address, IReadOnlyList< short > values)
Definition
MitsubishiMcBinary3EFrameBuilder.cs:235
Dreamine.PLC.Mitsubishi.MC.Protocol.MitsubishiMcBinary3EFrameBuilder.BuildBatchWriteBitsFrame
PlcResult< byte[]> BuildBatchWriteBitsFrame(MitsubishiMcConnectionOptions options, PlcAddress address, IReadOnlyList< bool > values)
Definition
MitsubishiMcBinary3EFrameBuilder.cs:330
Dreamine.PLC.Mitsubishi.MC.Protocol.MitsubishiMcBinary3EFrameBuilder.MitsubishiMcBinary3EFrameBuilder
MitsubishiMcBinary3EFrameBuilder(MitsubishiMcDeviceCodeMapper deviceCodeMapper)
Definition
MitsubishiMcBinary3EFrameBuilder.cs:74
Dreamine.PLC.Mitsubishi.MC.Protocol.MitsubishiMcBinary3EFrameBuilder.BuildBatchReadFrame
PlcResult< byte[]> BuildBatchReadFrame(MitsubishiMcConnectionOptions options, PlcAddress address, int count, bool isBitAccess)
Definition
MitsubishiMcBinary3EFrameBuilder.cs:145
Dreamine.PLC.Mitsubishi.MC.Protocol.MitsubishiMcEndian
Definition
MitsubishiMcEndian.cs:12
Dreamine.PLC.Mitsubishi.MC.Protocol.MitsubishiMcEndian.WriteUInt16LittleEndian
static void WriteUInt16LittleEndian(ICollection< byte > buffer, ushort value)
Definition
MitsubishiMcEndian.cs:44
Dreamine.PLC.Mitsubishi.MC.Protocol.MitsubishiMcEndian.WriteUInt24LittleEndian
static void WriteUInt24LittleEndian(ICollection< byte > buffer, int value)
Definition
MitsubishiMcEndian.cs:91
Protocol
MitsubishiMcBinary3EFrameBuilder.cs
다음에 의해 생성됨 :
1.17.0