AllenBradleyNet.cs
33.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
using HslCommunication.Core;
using HslCommunication.Core.IMessage;
using HslCommunication.Core.Net;
using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Text;
/*********************************************************************************************
*
* thanks: 江阴- ∮溪风-⊙_⌒ 提供了测试的PLC
* thanks:
*
* 感谢一个开源的java项目支持才使得本项目顺利开发:https://github.com/Tulioh/Ethernetip4j
*
***********************************************************************************************/
namespace HslCommunication.Profinet.AllenBradley
{
/// <summary>
/// AB PLC Data communication class, support read and write PLC node data
/// </summary>
/// <remarks>
/// thanks 江阴- ∮溪风-⊙_⌒ help test the dll
/// thanks 上海-null 测试了这个dll
/// </remarks>
public class AllenBradleyNet : NetworkDeviceBase<AllenBradleyMessage, RegularByteTransform>
{
#region Constructor
/// <summary>
/// Instantiate a communication object for a Allenbradley PLC protocol
/// </summary>
public AllenBradleyNet()
{
WordLength = 2;
}
/// <summary>
/// Instantiate a communication object for a Allenbradley PLC protocol
/// </summary>
/// <param name="ipAddress">PLC IpAddress</param>
/// <param name="port">PLC Port</param>
public AllenBradleyNet(string ipAddress, int port = 44818)
{
WordLength = 2;
IpAddress = ipAddress;
Port = port;
}
#endregion
#region Public Properties
/// <summary>
/// The current session handle, which is determined by the PLC when communicating with the PLC handshake
/// </summary>
public uint SessionHandle { get; private set; }
/// <summary>
/// Gets or sets the slot number information for the current plc, which should be set before connections
/// </summary>
public byte Slot { get; set; } = 0;
/// <summary>
/// when read array type, this means the segment length. when data type is 8-byte data, it should set to be 50
/// </summary>
public int ArraySegment { get; set; } = 100;
#endregion
#region Double Mode Override
/// <summary>
/// After connecting the Allenbradley plc, a next step handshake protocol is required
/// </summary>
/// <param name="socket">socket after connectting sucessful</param>
/// <returns>Success of initialization</returns>
protected override OperateResult InitializationOnConnect(Socket socket)
{
// Registering Session Information
OperateResult<byte[]> read = ReadFromCoreServer(socket, RegisterSessionHandle());
if (!read.IsSuccess) return read;
// Check the returned status
OperateResult check = CheckResponse(read.Content);
if (!check.IsSuccess) return check;
// Extract session ID
SessionHandle = ByteTransform.TransUInt32(read.Content, 4);
return OperateResult.CreateSuccessResult();
}
/// <summary>
/// A next step handshake agreement is required before disconnecting the Allenbradley plc
/// </summary>
/// <param name="socket">socket befor connection close </param>
/// <returns>Whether the disconnect operation was successful</returns>
protected override OperateResult ExtraOnDisconnect(Socket socket)
{
// Unregister session Information
OperateResult<byte[]> read = ReadFromCoreServer(socket, UnRegisterSessionHandle());
if (!read.IsSuccess) return read;
return OperateResult.CreateSuccessResult();
}
#endregion
#region Build Command
/// <summary>
/// Build a read command bytes
/// </summary>
/// <param name="address">the address of the tag name</param>
/// <param name="length">Array information, if not arrays, is 1 </param>
/// <returns>Message information that contains the result object </returns>
public OperateResult<byte[]> BuildReadCommand(string[] address, int[] length)
{
if (address == null || length == null) return new OperateResult<byte[]>("address or length is null");
if (address.Length != length.Length) return new OperateResult<byte[]>("address and length is not same array");
try
{
List<byte[]> cips = new List<byte[]>();
for (int i = 0; i < address.Length; i++)
{
cips.Add(AllenBradleyHelper.PackRequsetRead(address[i], length[i]));
}
byte[] commandSpecificData = AllenBradleyHelper.PackCommandSpecificData(Slot, cips.ToArray());
return OperateResult.CreateSuccessResult(AllenBradleyHelper.PackRequestHeader(0x6F, SessionHandle, commandSpecificData));
}
catch (Exception ex)
{
return new OperateResult<byte[]>("Address Wrong:" + ex.Message);
}
}
/// <summary>
/// Build a read command bytes
/// </summary>
/// <param name="address">The address of the tag name </param>
/// <returns>Message information that contains the result object </returns>
public OperateResult<byte[]> BuildReadCommand(string[] address)
{
if (address == null) return new OperateResult<byte[]>("address or length is null");
int[] length = new int[address.Length];
for (int i = 0; i < address.Length; i++)
{
length[i] = 1;
}
return BuildReadCommand(address, length);
}
/// <summary>
/// Create a written message instruction
/// </summary>
/// <param name="address">The address of the tag name </param>
/// <param name="typeCode">Data type</param>
/// <param name="data">Source Data </param>
/// <param name="length">In the case of arrays, the length of the array </param>
/// <returns>Message information that contains the result object</returns>
public OperateResult<byte[]> BuildWriteCommand(string address, ushort typeCode, byte[] data, int length = 1)
{
try
{
byte[] cip = AllenBradleyHelper.PackRequestWrite(address, typeCode, data, length);
byte[] commandSpecificData = AllenBradleyHelper.PackCommandSpecificData(Slot, cip);
return OperateResult.CreateSuccessResult(AllenBradleyHelper.PackRequestHeader(0x6F, SessionHandle, commandSpecificData));
}
catch (Exception ex)
{
return new OperateResult<byte[]>("Address Wrong:" + ex.Message);
}
}
#endregion
#region Override Read
/// <summary>
/// Read data information, data length for read array length information
/// </summary>
/// <param name="address">Address format of the node</param>
/// <param name="length">In the case of arrays, the length of the array </param>
/// <returns>Result data with result object </returns>
public override OperateResult<byte[]> Read(string address, ushort length)
{
if (length > 1)
{
return ReadSegment(address, 0, length);
}
else
{
return Read(new string[] { address }, new int[] { length });
}
}
/// <summary>
/// Bulk read Data information
/// </summary>
/// <param name="address">Name of the node </param>
/// <returns>Result data with result object </returns>
public OperateResult<byte[]> Read(string[] address)
{
if (address == null) return new OperateResult<byte[]>("address can not be null");
int[] length = new int[address.Length];
for (int i = 0; i < length.Length; i++)
{
length[i] = 1;
}
return Read(address, length);
}
/// <summary>
/// 批量读取数据信息,数据长度为读取的数组长度信息 -> Bulk read data information, data length for read array length information
/// </summary>
/// <param name="address">节点的名称 -> Name of the node </param>
/// <param name="length">如果是数组,就为数组长度 -> In the case of arrays, the length of the array </param>
/// <returns>带有结果对象的结果数据 -> Result data with result object </returns>
public OperateResult<byte[]> Read(string[] address, int[] length)
{
// 指令生成 -> Instruction Generation
OperateResult<byte[]> command = BuildReadCommand(address, length);
if (!command.IsSuccess) return command;
// 核心交互 -> Core Interactions
OperateResult<byte[]> read = ReadFromCoreServer(command.Content);
if (!read.IsSuccess) return read;
// 检查反馈 -> Check Feedback
OperateResult check = CheckResponse(read.Content);
if (!check.IsSuccess) return OperateResult.CreateFailedResult<byte[]>(check);
// 提取数据 -> Extracting data
return AllenBradleyHelper.ExtractActualData(read.Content, true);
}
/// <summary>
/// Read Segment Data Array form plc, use address tag name
/// </summary>
/// <param name="address">Tag name in plc</param>
/// <param name="startIndex">array start index</param>
/// <param name="length">array length</param>
/// <returns>Results Bytes</returns>
public OperateResult<byte[]> ReadSegment(string address, int startIndex, int length)
{
try
{
List<byte> bytesContent = new List<byte>();
ushort alreadyFinished = 0;
while (alreadyFinished < length)
{
ushort readLength = (ushort)Math.Min(length - alreadyFinished, 100);
OperateResult<byte[]> read = ReadByCips(AllenBradleyHelper.PackRequestReadSegment(address, startIndex + alreadyFinished, readLength));
if (!read.IsSuccess) return read;
bytesContent.AddRange(read.Content);
alreadyFinished += readLength;
}
return OperateResult.CreateSuccessResult(bytesContent.ToArray());
}
catch (Exception ex)
{
return new OperateResult<byte[]>("Address Wrong:" + ex.Message);
}
}
private OperateResult<byte[]> ReadByCips(params byte[][] cips)
{
OperateResult<byte[]> read = ReadCipFromServer(cips);
if (!read.IsSuccess) return read;
// 提取数据 -> Extracting data
return AllenBradleyHelper.ExtractActualData(read.Content, true);
}
/// <summary>
/// 使用CIP报文和服务器进行核心的数据交换
/// </summary>
/// <param name="cips">Cip commands</param>
/// <returns>Results Bytes</returns>
public OperateResult<byte[]> ReadCipFromServer(params byte[][] cips)
{
byte[] commandSpecificData = AllenBradleyHelper.PackCommandSpecificData(Slot, cips);
byte[] command = AllenBradleyHelper.PackRequestHeader(0x6F, SessionHandle, commandSpecificData);
// 核心交互 -> Core Interactions
OperateResult<byte[]> read = ReadFromCoreServer(command);
if (!read.IsSuccess) return read;
// 检查反馈 -> Check Feedback
OperateResult check = CheckResponse(read.Content);
if (!check.IsSuccess) return OperateResult.CreateFailedResult<byte[]>(check);
return OperateResult.CreateSuccessResult(read.Content);
}
/// <summary>
/// 读取单个的bool数据信息 -> Read a single BOOL data information
/// </summary>
/// <param name="address">节点的名称 -> Name of the node </param>
/// <returns>带有结果对象的结果数据 -> Result data with result info </returns>
public override OperateResult<bool> ReadBool(string address)
{
OperateResult<byte[]> read = Read(address, 1);
if (!read.IsSuccess) return OperateResult.CreateFailedResult<bool>(read);
return OperateResult.CreateSuccessResult(ByteTransform.TransBool(read.Content, 0));
}
/// <summary>
/// 批量读取的bool数组信息 -> Bulk read of bool array information
/// </summary>
/// <param name="address">节点的名称 -> Name of the node </param>
/// <returns>带有结果对象的结果数据 -> Result data with result info </returns>
public OperateResult<bool[]> ReadBoolArray(string address)
{
OperateResult<byte[]> read = Read(address, 1);
if (!read.IsSuccess) return OperateResult.CreateFailedResult<bool[]>(read);
return OperateResult.CreateSuccessResult(ByteTransform.TransBool(read.Content, 0, read.Content.Length));
}
/// <summary>
/// 读取PLC的byte类型的数据 -> Read the byte type of PLC data
/// </summary>
/// <param name="address">节点的名称 -> Name of the node </param>
/// <returns>带有结果对象的结果数据 -> Result data with result info </returns>
public OperateResult<byte> ReadByte(string address)
{
OperateResult<byte[]> read = Read(address, 1);
if (!read.IsSuccess) return OperateResult.CreateFailedResult<byte>(read);
return OperateResult.CreateSuccessResult(ByteTransform.TransByte(read.Content, 0));
}
#endregion
#region Device Override
/// <summary>
/// 读取PLC的short类型的数组 -> Read an array of the short type of the PLC
/// </summary>
/// <param name="address">节点的名称 -> Name of the node </param>
/// <param name="length">数组长度 -> Array length </param>
/// <returns>带有结果对象的结果数据 -> Result data with result info </returns>
/// <example>
/// 以下为三菱的连接对象示例,其他的设备读写情况参照下面的代码:
/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Core\NetworkDeviceBase.cs" region="ReadInt16Array" title="Int16类型示例" />
/// </example>
public override OperateResult<short[]> ReadInt16(string address, ushort length)
{
return ByteTransformHelper.GetResultFromBytes(Read(address, length), m => ByteTransform.TransInt16(m, 0, length));
}
/// <summary>
/// 读取PLC的ushort类型的数组 -> An array that reads the ushort type of the PLC
/// </summary>
/// <param name="address">节点的名称 -> Name of the node </param>
/// <param name="length">数组长度 -> Array length </param>
/// <returns>带有结果对象的结果数据 -> Result data with result info </returns>
/// <example>
/// 以下为三菱的连接对象示例,其他的设备读写情况参照下面的代码:
/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Core\NetworkDeviceBase.cs" region="ReadUInt16Array" title="UInt16类型示例" />
/// </example>
public override OperateResult<ushort[]> ReadUInt16(string address, ushort length)
{
return ByteTransformHelper.GetResultFromBytes(Read(address, length), m => ByteTransform.TransUInt16(m, 0, length));
}
/// <summary>
/// 读取PLC的int类型的数组 -> An array that reads the int type of the PLC
/// </summary>
/// <param name="address">节点的名称 -> Name of the node </param>
/// <param name="length">数组长度 -> Array length </param>
/// <returns>带有结果对象的结果数据 -> Result data with result info </returns>
/// <example>
/// 以下为三菱的连接对象示例,其他的设备读写情况参照下面的代码:
/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Core\NetworkDeviceBase.cs" region="ReadInt32Array" title="Int32类型示例" />
/// </example>
public override OperateResult<int[]> ReadInt32(string address, ushort length)
{
return ByteTransformHelper.GetResultFromBytes(Read(address, length), m => ByteTransform.TransInt32(m, 0, length));
}
/// <summary>
/// 读取PLC的uint类型的数组 -> An array that reads the UINT type of the PLC
/// </summary>
/// <param name="address">节点的名称 -> Name of the node </param>
/// <param name="length">数组长度 -> Array length </param>
/// <returns>带有结果对象的结果数据 -> Result data with result info </returns>
/// <example>
/// 以下为三菱的连接对象示例,其他的设备读写情况参照下面的代码:
/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Core\NetworkDeviceBase.cs" region="ReadUInt32Array" title="UInt32类型示例" />
/// </example>
public override OperateResult<uint[]> ReadUInt32(string address, ushort length)
{
return ByteTransformHelper.GetResultFromBytes(Read(address, length), m => ByteTransform.TransUInt32(m, 0, length));
}
/// <summary>
/// 读取PLC的float类型的数组 -> An array that reads the float type of the PLC
/// </summary>
/// <param name="address">节点的名称 -> Name of the node </param>
/// <param name="length">数组长度 -> Array length </param>
/// <returns>带有结果对象的结果数据 -> Result data with result info </returns>
/// <example>
/// 以下为三菱的连接对象示例,其他的设备读写情况参照下面的代码:
/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Core\NetworkDeviceBase.cs" region="ReadFloatArray" title="Float类型示例" />
/// </example>
public override OperateResult<float[]> ReadFloat(string address, ushort length)
{
return ByteTransformHelper.GetResultFromBytes(Read(address, length), m => ByteTransform.TransSingle(m, 0, length));
}
/// <summary>
/// 读取PLC的long类型的数组 -> An array that reads the long type of the PLC
/// </summary>
/// <param name="address">节点的名称 -> Name of the node </param>
/// <param name="length">数组长度 -> Array length </param>
/// <returns>带有结果对象的结果数据 -> Result data with result info </returns>
/// <example>
/// 以下为三菱的连接对象示例,其他的设备读写情况参照下面的代码:
/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Core\NetworkDeviceBase.cs" region="ReadInt64Array" title="Int64类型示例" />
/// </example>
public override OperateResult<long[]> ReadInt64(string address, ushort length)
{
return ByteTransformHelper.GetResultFromBytes(Read(address, length), m => ByteTransform.TransInt64(m, 0, length));
}
/// <summary>
/// 读取PLC的ulong类型的数组 -> An array that reads the ULONG type of the PLC
/// </summary>
/// <param name="address">节点的名称 -> Name of the node </param>
/// <param name="length">数组长度 -> Array length </param>
/// <returns>带有结果对象的结果数据 -> Result data with result info </returns>
/// <example>
/// 以下为三菱的连接对象示例,其他的设备读写情况参照下面的代码:
/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Core\NetworkDeviceBase.cs" region="ReadUInt64Array" title="UInt64类型示例" />
/// </example>
public override OperateResult<ulong[]> ReadUInt64(string address, ushort length)
{
return ByteTransformHelper.GetResultFromBytes(Read(address, length), m => ByteTransform.TransUInt64(m, 0, length));
}
/// <summary>
/// 读取PLC的double类型的数组 -> An array that reads the double type of the PLC
/// </summary>
/// <param name="address">节点的名称 -> Name of the node </param>
/// <param name="length">数组长度 -> Array length </param>
/// <returns>带有结果对象的结果数据 -> Result data with result info </returns>
/// <example>
/// 以下为三菱的连接对象示例,其他的设备读写情况参照下面的代码:
/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Core\NetworkDeviceBase.cs" region="ReadDoubleArray" title="Double类型示例" />
/// </example>
public override OperateResult<double[]> ReadDouble(string address, ushort length)
{
return ByteTransformHelper.GetResultFromBytes(Read(address, length), m => ByteTransform.TransDouble(m, 0, length));
}
#endregion
#region Write Support
/// <summary>
/// 使用指定的类型写入指定的节点数据 -> Writes the specified node data with the specified type
/// </summary>
/// <param name="address">节点的名称 -> Name of the node </param>
/// <param name="typeCode">类型代码,详细参见<see cref="AllenBradleyHelper"/>上的常用字段 -> Type code, see the commonly used Fields section on the <see cref= "AllenBradleyHelper"/> in detail</param>
/// <param name="value">实际的数据值 -> The actual data value </param>
/// <param name="length">如果节点是数组,就是数组长度 -> If the node is an array, it is the array length </param>
/// <returns>是否写入成功 -> Whether to write successfully</returns>
public OperateResult WriteTag(string address, ushort typeCode, byte[] value, int length = 1)
{
OperateResult<byte[]> command = BuildWriteCommand(address, typeCode, value, length);
if (!command.IsSuccess) return command;
OperateResult<byte[]> read = ReadFromCoreServer(command.Content);
if (!read.IsSuccess) return read;
OperateResult check = CheckResponse(read.Content);
if (!check.IsSuccess) return OperateResult.CreateFailedResult<byte[]>(check);
return AllenBradleyHelper.ExtractActualData(read.Content, false);
}
#endregion
#region Write Override
/// <summary>
/// 向PLC中写入short数组,返回是否写入成功 -> Writes a short array to the PLC to return whether the write was successful
/// </summary>
/// <param name="address">节点的名称 -> Name of the node </param>
/// <param name="values">实际数据 -> Actual data </param>
/// <returns>是否写入成功 -> Whether to write successfully</returns>
/// <example>
/// 以下为三菱的连接对象示例,其他的设备读写情况参照下面的代码:
/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Core\NetworkDeviceBase.cs" region="WriteInt16Array" title="Int16类型示例" />
/// </example>
public override OperateResult Write(string address, short[] values)
{
return WriteTag(address, AllenBradleyHelper.CIP_Type_Word, ByteTransform.TransByte(values), values.Length);
}
/// <summary>
/// 向PLC中写入ushort数组,返回是否写入成功 -> Writes an array of ushort to the PLC to return whether the write was successful
/// </summary>
/// <param name="address">节点的名称 -> Name of the node </param>
/// <param name="values">实际数据 -> Actual data </param>
/// <returns>是否写入成功 -> Whether to write successfully</returns>
/// <example>
/// 以下为三菱的连接对象示例,其他的设备读写情况参照下面的代码:
/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Core\NetworkDeviceBase.cs" region="WriteUInt16Array" title="UInt16类型示例" />
/// </example>
public override OperateResult Write(string address, ushort[] values)
{
return WriteTag(address, AllenBradleyHelper.CIP_Type_Word, ByteTransform.TransByte(values), values.Length);
}
/// <summary>
/// 向PLC中写入int数组,返回是否写入成功 -> Writes an int array to the PLC to return whether the write was successful
/// </summary>
/// <param name="address">节点的名称 -> Name of the node </param>
/// <param name="values">实际数据 -> Actual data </param>
/// <returns>是否写入成功 -> Whether to write successfully</returns>
/// <example>
/// 以下为三菱的连接对象示例,其他的设备读写情况参照下面的代码:
/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Core\NetworkDeviceBase.cs" region="WriteInt32Array" title="Int32类型示例" />
/// </example>
public override OperateResult Write(string address, int[] values)
{
return WriteTag(address, AllenBradleyHelper.CIP_Type_DWord, ByteTransform.TransByte(values), values.Length);
}
/// <summary>
/// Writes an array of UINT to the PLC to return whether the write was successful
/// </summary>
/// <param name="address">节点的名称 -> Name of the node </param>
/// <param name="values">实际数据 -> Actual data </param>
/// <returns>是否写入成功 -> Whether to write successfully</returns>
/// <example>
/// 以下为三菱的连接对象示例,其他的设备读写情况参照下面的代码:
/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Core\NetworkDeviceBase.cs" region="WriteUInt32Array" title="UInt32类型示例" />
/// </example>
public override OperateResult Write(string address, uint[] values)
{
return WriteTag(address, AllenBradleyHelper.CIP_Type_DWord, ByteTransform.TransByte(values), values.Length);
}
/// <summary>
/// Writes an array of float to the PLC to return whether the write was successful
/// </summary>
/// <param name="address">Name of the node </param>
/// <param name="values">Actual data </param>
/// <returns>Whether to write successfully</returns>
/// <example>
/// 以下为三菱的连接对象示例,其他的设备读写情况参照下面的代码:
/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Core\NetworkDeviceBase.cs" region="WriteFloatArray" title="Float类型示例" />
/// </example>
public override OperateResult Write(string address, float[] values)
{
return WriteTag(address, AllenBradleyHelper.CIP_Type_Real, ByteTransform.TransByte(values), values.Length);
}
/// <summary>
/// Writes an array of long to the PLC to return whether the write was successful
/// </summary>
/// <param name="address">Name of the node </param>
/// <param name="values">Actual data </param>
/// <returns>Whether to write successfully</returns>
/// <example>
/// 以下为三菱的连接对象示例,其他的设备读写情况参照下面的代码:
/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Core\NetworkDeviceBase.cs" region="WriteInt64Array" title="Int64类型示例" />
/// </example>
public override OperateResult Write(string address, long[] values)
{
return WriteTag(address, AllenBradleyHelper.CIP_Type_LInt, ByteTransform.TransByte(values), values.Length);
}
/// <summary>
/// Writes an array of ulong to the PLC to return whether the write was successful
/// </summary>
/// <param name="address">Name of the node </param>
/// <param name="values">Actual data </param>
/// <returns>Whether to write successfully</returns>
/// <example>
/// 以下为三菱的连接对象示例,其他的设备读写情况参照下面的代码:
/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Core\NetworkDeviceBase.cs" region="WriteUInt64Array" title="UInt64类型示例" />
/// </example>
public override OperateResult Write(string address, ulong[] values)
{
return WriteTag(address, AllenBradleyHelper.CIP_Type_LInt, ByteTransform.TransByte(values), values.Length);
}
/// <summary>
/// Writes an array of double to the PLC to return whether the write was successful
/// </summary>
/// <param name="address">Name of the node </param>
/// <param name="values">Actual data </param>
/// <returns>Whether to write successfully</returns>
/// <example>
/// 以下为三菱的连接对象示例,其他的设备读写情况参照下面的代码:
/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Core\NetworkDeviceBase.cs" region="WriteDoubleArray" title="Double类型示例" />
/// </example>
public override OperateResult Write(string address, double[] values)
{
return WriteTag(address, AllenBradleyHelper.CIP_Type_Double, ByteTransform.TransByte(values), values.Length);
}
/// <summary>
/// 向PLC中写入string数据,返回是否写入成功,该string类型是针对PLC的DINT类型,长度自动扩充到8
/// </summary>
/// <param name="address">节点的名称 -> Name of the node </param>
/// <param name="value">实际数据 -> Actual data </param>
/// <returns>是否写入成功 -> Whether to write successfully</returns>
public override OperateResult Write(string address, string value)
{
return WriteTag(address, AllenBradleyHelper.CIP_Type_DWord, BasicFramework.SoftBasic.ArrayExpandToLength(ByteTransform.TransByte(value, Encoding.ASCII), 8));
}
/// <summary>
/// 向PLC中写入bool数据,返回是否写入成功
/// </summary>
/// <param name="address">节点的名称 -> Name of the node </param>
/// <param name="value">实际数据 -> Actual data </param>
/// <returns>是否写入成功 -> Whether to write successfully</returns>
public override OperateResult Write(string address, bool value)
{
return WriteTag(address, AllenBradleyHelper.CIP_Type_Bool, value ? new byte[] { 0xFF, 0xFF } : new byte[] { 0x00, 0x00 });
}
/// <summary>
/// 向PLC中写入byte数据,返回是否写入成功
/// </summary>
/// <param name="address">节点的名称 -> Name of the node </param>
/// <param name="value">实际数据 -> Actual data </param>
/// <returns>是否写入成功 -> Whether to write successfully</returns>
public OperateResult Write(string address, byte value)
{
return WriteTag(address, AllenBradleyHelper.CIP_Type_Byte, new byte[] { value, 0x00 });
}
#endregion
#region Handle Single
/// <summary>
/// 向PLC注册会话ID的报文 ->
/// Register a message with the PLC for the session ID
/// </summary>
/// <returns>报文信息 -> Message information </returns>
public byte[] RegisterSessionHandle()
{
byte[] commandSpecificData = new byte[] { 0x01, 0x00, 0x00, 0x00, };
return AllenBradleyHelper.PackRequestHeader(0x65, 0, commandSpecificData);
}
/// <summary>
/// 获取卸载一个已注册的会话的报文 ->
/// Get a message to uninstall a registered session
/// </summary>
/// <returns>字节报文信息 -> BYTE message information </returns>
public byte[] UnRegisterSessionHandle()
{
return AllenBradleyHelper.PackRequestHeader(0x66, SessionHandle, new byte[0]);
}
private OperateResult CheckResponse(byte[] response)
{
try
{
int status = ByteTransform.TransInt32(response, 8);
if (status == 0) return OperateResult.CreateSuccessResult();
string msg = string.Empty;
switch (status)
{
case 0x01: msg = StringResources.Language.AllenBradleySessionStatus01; break;
case 0x02: msg = StringResources.Language.AllenBradleySessionStatus02; break;
case 0x03: msg = StringResources.Language.AllenBradleySessionStatus03; break;
case 0x64: msg = StringResources.Language.AllenBradleySessionStatus64; break;
case 0x65: msg = StringResources.Language.AllenBradleySessionStatus65; break;
case 0x69: msg = StringResources.Language.AllenBradleySessionStatus69; break;
default: msg = StringResources.Language.UnknownError; break;
}
return new OperateResult(status, msg);
}
catch (Exception ex)
{
return new OperateResult(ex.Message);
}
}
#endregion
#region Object Override
/// <summary>
/// 返回表示当前对象的字符串
/// </summary>
/// <returns>字符串信息</returns>
public override string ToString()
{
return $"AllenBradleyNet[{IpAddress}:{Port}]";
}
#endregion
}
}