SiemensS7NetV.java
30.3 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
package com.huaheng.pc.plc.util;
import HslCommunication.BasicFramework.SoftBasic;
import HslCommunication.Core.IMessage.INetMessage;
import HslCommunication.Core.IMessage.S7Message;
import HslCommunication.Core.Net.NetworkBase.NetworkDeviceBase;
import HslCommunication.Core.Transfer.ByteTransformHelper;
import HslCommunication.Core.Transfer.ReverseBytesTransform;
import HslCommunication.Core.Types.FunctionOperateExOne;
import HslCommunication.Core.Types.OperateResult;
import HslCommunication.Core.Types.OperateResultExOne;
import HslCommunication.Core.Types.OperateResultExThree;
import HslCommunication.Profinet.Siemens.SiemensDateTime;
import HslCommunication.Profinet.Siemens.SiemensPLCS;
import HslCommunication.StringResources;
import HslCommunication.Utilities;
import java.io.ByteArrayOutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SiemensS7NetV extends NetworkDeviceBase {
private byte[] plcHead1 = new byte[]{3, 0, 0, 22, 17, -32, 0, 0, 0, 1, 0, -64, 1, 10, -63, 2, 1, 2, -62, 2, 1, 0};
private byte[] plcHead2 = new byte[]{3, 0, 0, 25, 2, -16, -128, 50, 1, 0, 0, 4, 0, 0, 8, 0, 0, -16, 0, 0, 1, 0, 1, 1, -32};
private byte[] plcOrderNumber = new byte[]{3, 0, 0, 33, 2, -16, -128, 50, 7, 0, 0, 0, 1, 0, 8, 0, 8, 0, 1, 18, 4, 17, 68, 1, 0, -1, 9, 0, 4, 0, 17, 0, 0};
private SiemensPLCS CurrentPlc;
private byte[] plcHead1_200smart;
private byte[] plcHead2_200smart;
private byte[] plcHead1_200;
private byte[] plcHead2_200;
private byte plc_slot;
private byte plc_rack;
public SiemensS7NetV(SiemensPLCS siemens) {
this.CurrentPlc = SiemensPLCS.S1200;
this.plcHead1_200smart = new byte[]{3, 0, 0, 22, 17, -32, 0, 0, 0, 1, 0, -63, 2, 16, 0, -62, 2, 3, 0, -64, 1, 10};
this.plcHead2_200smart = new byte[]{3, 0, 0, 25, 2, -16, -128, 50, 1, 0, 0, -52, -63, 0, 8, 0, 0, -16, 0, 0, 1, 0, 1, 3, -64};
this.plcHead1_200 = new byte[]{3, 0, 0, 22, 17, -32, 0, 0, 0, 1, 0, -63, 2, 77, 87, -62, 2, 77, 87, -64, 1, 9};
this.plcHead2_200 = new byte[]{3, 0, 0, 25, 2, -16, -128, 50, 1, 0, 0, 0, 0, 0, 8, 0, 0, -16, 0, 0, 1, 0, 1, 3, -64};
this.plc_slot = 0;
this.plc_rack = 0;
this.Initialization(siemens, "");
}
public SiemensS7NetV(SiemensPLCS siemens, String ipAddress) {
this.CurrentPlc = SiemensPLCS.S1200;
this.plcHead1_200smart = new byte[]{3, 0, 0, 22, 17, -32, 0, 0, 0, 1, 0, -63, 2, 16, 0, -62, 2, 3, 0, -64, 1, 10};
this.plcHead2_200smart = new byte[]{3, 0, 0, 25, 2, -16, -128, 50, 1, 0, 0, -52, -63, 0, 8, 0, 0, -16, 0, 0, 1, 0, 1, 3, -64};
this.plcHead1_200 = new byte[]{3, 0, 0, 22, 17, -32, 0, 0, 0, 1, 0, -63, 2, 77, 87, -62, 2, 77, 87, -64, 1, 9};
this.plcHead2_200 = new byte[]{3, 0, 0, 25, 2, -16, -128, 50, 1, 0, 0, 0, 0, 0, 8, 0, 0, -16, 0, 0, 1, 0, 1, 3, -64};
this.plc_slot = 0;
this.plc_rack = 0;
this.Initialization(siemens, ipAddress);
}
@Override
protected INetMessage GetNewNetMessage() {
return new S7Message();
}
private void Initialization(SiemensPLCS siemens, String ipAddress) {
this.WordLength = 2;
this.setIpAddress(ipAddress);
this.setPort(102);
this.CurrentPlc = siemens;
this.setByteTransform(new ReverseBytesTransform());
switch(siemens) {
case S1200:
this.plcHead1[21] = 0;
break;
case S300:
this.plcHead1[21] = 2;
break;
case S400:
this.plcHead1[21] = 3;
this.plcHead1[17] = 0;
break;
case S1500:
this.plcHead1[21] = 0;
break;
case S200Smart:
this.plcHead1 = this.plcHead1_200smart;
this.plcHead2 = this.plcHead2_200smart;
break;
case S200:
this.plcHead1 = this.plcHead1_200;
this.plcHead2 = this.plcHead2_200;
default:
this.plcHead1[18] = 0;
}
}
public byte getSlot() {
return this.plc_slot;
}
public void setSlot(byte value) {
this.plc_slot = value;
this.plcHead1[21] = (byte)(this.plc_rack * 32 + this.plc_slot);
}
public byte getRack() {
return this.plc_rack;
}
public void setRack(byte value) {
this.plc_rack = value;
this.plcHead1[21] = (byte)(this.plc_rack * 32 + this.plc_slot);
}
@Override
protected OperateResult InitializationOnConnect(Socket socket) {
OperateResultExOne<byte[]> read_first = this.ReadFromCoreServer(socket, this.plcHead1);
if (!read_first.IsSuccess) {
return read_first;
} else {
OperateResultExOne<byte[]> read_second = this.ReadFromCoreServer(socket, this.plcHead2);
return (OperateResult)(!read_second.IsSuccess ? read_second : OperateResult.CreateSuccessResult());
}
}
public OperateResultExOne<String> ReadOrderNumber() {
OperateResultExOne<String> result = new OperateResultExOne();
OperateResultExOne<byte[]> read = this.ReadFromCoreServer(this.plcOrderNumber);
if (read.IsSuccess && ((byte[])read.Content).length > 100) {
result.IsSuccess = true;
result.Content = Utilities.getString(Arrays.copyOfRange((byte[])read.Content, 71, 91), "ASCII");
}
if (!result.IsSuccess) {
result.CopyErrorFromOther(read);
}
return result;
}
@Override
public OperateResultExOne<byte[]> Read(String address, short length) {
OperateResultExThree<Byte, Integer, Integer> addressResult = AnalysisAddress(address);
if (!addressResult.IsSuccess) {
return OperateResultExOne.CreateFailedResult(addressResult);
} else {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
short readLength;
for(short alreadyFinished = 0; alreadyFinished < length; addressResult.Content2 = (Integer)addressResult.Content2 + readLength * 8) {
readLength = (short)Math.min(length - alreadyFinished, 200);
OperateResultExThree<Byte, Integer, Integer>[] list = new OperateResultExThree[]{addressResult};
OperateResultExOne<byte[]> read = this.Read(list, new short[]{readLength});
if (!read.IsSuccess) {
return read;
}
try {
outputStream.write((byte[])read.Content);
} catch (Exception var11) {
}
alreadyFinished += readLength;
}
byte[] buffer = outputStream.toByteArray();
try {
outputStream.close();
} catch (Exception var10) {
}
return OperateResultExOne.CreateSuccessResult(buffer);
}
}
private OperateResultExOne<byte[]> ReadBitFromPLC(String address) {
OperateResultExOne<byte[]> command = BuildBitReadCommand(address);
if (!command.IsSuccess) {
return command;
} else {
OperateResultExOne<byte[]> read = this.ReadFromCoreServer((byte[])command.Content);
if (!read.IsSuccess) {
return read;
} else {
int receiveCount = 1;
if (((byte[])read.Content).length >= 21 && ((byte[])read.Content)[20] == 1) {
byte[] buffer = new byte[receiveCount];
if (22 < ((byte[])read.Content).length && ((byte[])read.Content)[21] == -1 && ((byte[])read.Content)[22] == 3) {
buffer[0] = ((byte[])read.Content)[25];
}
return OperateResultExOne.CreateSuccessResult(buffer);
} else {
return new OperateResultExOne(read.ErrorCode, StringResources.Language.SiemensDataLengthCheckFailed());
}
}
}
}
public OperateResultExOne<byte[]> Read(String[] address, short[] length) {
OperateResultExThree<Byte, Integer, Integer>[] list = new OperateResultExThree[address.length];
for(int i = 0; i < address.length; ++i) {
OperateResultExThree<Byte, Integer, Integer> tmp = AnalysisAddress(address[i]);
if (!tmp.IsSuccess) {
return OperateResultExOne.CreateFailedResult(tmp);
}
list[i] = tmp;
}
return this.Read(list, length);
}
private OperateResultExOne<byte[]> Read(OperateResultExThree<Byte, Integer, Integer>[] address, short[] length) {
OperateResultExOne<byte[]> command = BuildReadCommand(address, length);
if (!command.IsSuccess) {
return command;
} else {
OperateResultExOne<byte[]> read = this.ReadFromCoreServer((byte[])command.Content);
if (!read.IsSuccess) {
return read;
} else {
int receiveCount = 0;
for(int i = 0; i < length.length; ++i) {
receiveCount += length[i];
}
if (((byte[])read.Content).length >= 21 && ((byte[])read.Content)[20] == length.length) {
byte[] buffer = new byte[receiveCount];
int kk = 0;
int ll = 0;
for(int ii = 21; ii < ((byte[])read.Content).length; ++ii) {
if (ii + 1 < ((byte[])read.Content).length && ((byte[])read.Content)[ii] == -1 && ((byte[])read.Content)[ii + 1] == 4) {
System.arraycopy(read.Content, ii + 4, buffer, ll, length[kk]);
ii += length[kk] + 3;
ll += length[kk];
++kk;
}
}
return OperateResultExOne.CreateSuccessResult(buffer);
} else {
return new OperateResultExOne(read.ErrorCode, StringResources.Language.SiemensDataLengthCheckFailed());
}
}
}
}
@Override
public OperateResultExOne<Boolean> ReadBool(String address) {
return ByteTransformHelper.GetResultFromBytes(this.ReadBitFromPLC(address), new FunctionOperateExOne<byte[], Boolean>() {
public Boolean action(byte[] content) {
return content[0] != 0;
}
});
}
public OperateResultExOne<Byte> ReadByte(String address) {
return ByteTransformHelper.GetResultFromBytes(this.Read(address, (short)1), new FunctionOperateExOne<byte[], Byte>() {
public Byte action(byte[] content) {
return content[0];
}
});
}
private OperateResult WriteBase(byte[] entireValue) {
OperateResultExOne<byte[]> write = this.ReadFromCoreServer(entireValue);
if (!write.IsSuccess) {
return write;
} else {
return ((byte[])write.Content)[((byte[])write.Content).length - 1] != -1 ? new OperateResult(((byte[])write.Content)[((byte[])write.Content).length - 1], StringResources.Language.SiemensWriteError() + ((byte[])write.Content)[((byte[])write.Content).length - 1]) : OperateResult.CreateSuccessResult();
}
}
@Override
public OperateResult Write(String address, byte[] value) {
OperateResultExThree<Byte, Integer, Integer> analysis = AnalysisAddress(address);
if (!analysis.IsSuccess) {
return OperateResultExOne.CreateFailedResult(analysis);
} else {
int length = value.length;
short writeLength=0;
//System.out.println("length======="+length);
//System.out.println("Content2======="+(Integer)analysis.Content2 + writeLength * 8);
for(int alreadyFinished = 0; alreadyFinished < length; analysis.Content2 = (Integer)analysis.Content2 + writeLength * 8) {
writeLength = (short)Math.min(length - alreadyFinished, 200);
byte[] buffer = this.getByteTransform().TransByte(value, alreadyFinished, writeLength);
OperateResultExOne<byte[]> command = BuildWriteByteCommand(analysis, buffer);
if (!command.IsSuccess) {
return command;
}
OperateResult write = this.WriteBase((byte[])command.Content);
if (!write.IsSuccess) {
return write;
}
alreadyFinished += writeLength;
}
return OperateResult.CreateSuccessResult();
}
}
@Override
public OperateResult Write(String address, boolean value) {
OperateResultExOne<byte[]> command = BuildWriteBitCommand(address, value);
return (OperateResult)(!command.IsSuccess ? command : this.WriteBase((byte[])command.Content));
}
@Override
public OperateResult Write(String address, boolean[] values) {
return this.Write(address, SoftBasic.BoolArrayToByte(values));
}
public OperateResult Write(String address, byte value) {
return this.Write(address, new byte[]{value});
}
public OperateResultExOne<String> ReadString(String address) {
OperateResultExOne read;
OperateResultExOne readString;
byte[] buffer;
if (this.CurrentPlc != SiemensPLCS.S200Smart) {
read = this.Read(address, (short)2);
if (!read.IsSuccess) {
return OperateResultExOne.CreateFailedResult(read);
} else if (((byte[])read.Content)[0] != -2) {
return new OperateResultExOne("Value in plc is not string type");
} else {
readString = this.Read(address, (short)(2 + ((byte[])read.Content)[1]));
if (!readString.IsSuccess) {
return OperateResultExOne.CreateFailedResult(readString);
} else {
try {
buffer = new byte[((byte[])read.Content)[1]];
System.arraycopy(readString.Content, 2, buffer, 0, buffer.length);
return OperateResultExOne.CreateSuccessResult(new String(buffer, StandardCharsets.US_ASCII));
} catch (Exception var5) {
return new OperateResultExOne(var5.getMessage());
}
}
}
} else {
read = this.Read(address, (short)1);
if (!read.IsSuccess) {
return OperateResultExOne.CreateFailedResult(read);
} else {
readString = this.Read(address, (short)(1 + ((byte[])read.Content)[0]));
if (!readString.IsSuccess) {
return OperateResultExOne.CreateFailedResult(readString);
} else {
try {
buffer = new byte[((byte[])read.Content)[0]];
System.arraycopy(readString.Content, 1, buffer, 0, buffer.length);
return OperateResultExOne.CreateSuccessResult(new String(buffer, StandardCharsets.US_ASCII));
} catch (Exception var6) {
return new OperateResultExOne(var6.getMessage());
}
}
}
}
}
@Override
public OperateResult Write(String address, String value) {
//byte[] temp = this.getByteTransform().TransByte(value, "US-ASCII");
try{
byte[] temp=value.getBytes("ASCII");
//byte[] temp = this.getByteTransform().TransByte(value, "US-ASCII");
//temp=value.getBytes();
if (this.WordLength == 1) {
temp = SoftBasic.ArrayExpandToLengthEven(temp);
}
return this.CurrentPlc != SiemensPLCS.S200Smart ? this.Write(address, SoftBasic.SpliceTwoByteArray(new byte[]{-2, (byte)temp.length}, temp)) : this.Write(address, SoftBasic.SpliceTwoByteArray(new byte[]{(byte)temp.length}, temp));
}catch (Exception e){
return new OperateResult();
}
}
public OperateResultExOne<Date> ReadDateTime(String address) {
OperateResultExOne<byte[]> read = this.Read(address, (short)8);
if (!read.IsSuccess) {
return OperateResultExOne.CreateFailedResult(read);
} else {
try {
return OperateResultExOne.CreateSuccessResult(SiemensDateTime.FromByteArray((byte[])read.Content));
} catch (Exception var4) {
return new OperateResultExOne(var4.getMessage());
}
}
}
public OperateResult Write(String address, Date dateTime) {
try {
return this.Write(address, SiemensDateTime.ToByteArray(dateTime));
} catch (Exception var4) {
return new OperateResult(var4.getMessage());
}
}
@Override
public String toString() {
return "SiemensS7Net";
}
public static int CalculateAddressStarted1(String address) {
if (address.indexOf(46) < 0) {
return Integer.parseInt(address) * 8;
} else {
String[] temp = address.split("\\.");
String temp1 = temp[0].split("DBX")[1];
System.out.println("temp1=========="+temp1);
//System.out.println("temp[1]=========="+temp[1]);
//return Integer.parseInt(temp2) * 8 ;
return Integer.parseInt(temp1) * 8 + Integer.parseInt(temp[1].split(",")[0]);
//return Integer.parseInt(temp[0]) * 8 + Integer.parseInt(temp[1]);
}
}
public static int CalculateAddressStarted( String address )
{
//System.out.println("address==="+address);
if (address.indexOf(".") < 0)
{
String str= getNumberByString(address.toString());
return Integer.parseInt( str ) * 8;
}
else
{
String[] temp = address.split( "\\." );
//System.out.println("temp==="+temp[0].toString());
String str= getNumberByString(temp[0].toString());
System.out.println("temp==="+str);
//System.out.println("temp==="+temp[1].split(",")[0]);
return Integer.parseInt(str) * 8 + Integer.parseInt( temp[1].split(",")[0] );
}
}
public static OperateResultExThree<Byte, Integer, Integer> AnalysisAddress(String address) {
OperateResultExThree result = new OperateResultExThree();
try {
result.Content3 = 0;
if (address.charAt(0) == 'I') {
result.Content1 = -127;
result.Content2 = CalculateAddressStarted(address.substring(1));
} else if (address.charAt(0) == 'Q') {
result.Content1 = -126;
result.Content2 = CalculateAddressStarted(address.substring(1));
} else if (address.charAt(0) == 'M') {
result.Content1 = -125;
result.Content2 = CalculateAddressStarted(address.substring(1));
} else if (address.charAt(0) != 'D' && address.substring(0, 2) != "DB") {
if (address.charAt(0) == 'T') {
result.Content1 = 29;
result.Content2 = CalculateAddressStarted(address.substring(1));
} else if (address.charAt(0) == 'C') {
result.Content1 = 28;
result.Content2 = CalculateAddressStarted(address.substring(1));
} else {
if (address.charAt(0) != 'V') {
return new OperateResultExThree(StringResources.Language.NotSupportedDataType());
}
result.Content1 = -124;
result.Content3 = 1;
result.Content2 = CalculateAddressStarted(address.substring(1));
}
} else {
//DBX8.0
result.Content1 = -124;
String[] adds = address.split("\\.");
if (address.charAt(1) == 'B') {
result.Content3 = Integer.parseInt(adds[0].substring(2,3));
} else {
result.Content3 = Integer.parseInt(adds[0].substring(1));
}
result.Content2 = CalculateAddressStarted(address.substring(3));
//result.Content2 = CalculateAddressStarted(address.substring(address.indexOf(46) + 1));
}
} catch (Exception var3) {
result.Message = var3.getMessage();
return result;
}
result.IsSuccess = true;
return result;
}
public static OperateResultExOne<byte[]> BuildReadCommand(String address, short length) {
OperateResultExThree<Byte, Integer, Integer> analysis = AnalysisAddress(address);
if (!analysis.IsSuccess) {
return OperateResultExOne.CreateFailedResult(analysis);
} else {
OperateResultExThree<Byte, Integer, Integer>[] addressList = new OperateResultExThree[]{analysis};
short[] lengthList = new short[]{length};
return BuildReadCommand(addressList, lengthList);
}
}
public static OperateResultExOne<byte[]> BuildReadCommand(OperateResultExThree<Byte, Integer, Integer>[] address, short[] length) {
if (address == null) {
throw new RuntimeException("address is null");
} else if (length == null) {
throw new RuntimeException("count is null");
} else if (address.length != length.length) {
throw new RuntimeException(StringResources.Language.TwoParametersLengthIsNotSame());
} else if (length.length > 19) {
throw new RuntimeException(StringResources.Language.SiemensReadLengthCannotLargerThan19());
} else {
int readCount = length.length;
byte[] _PLCCommand = new byte[19 + readCount * 12];
_PLCCommand[0] = 3;
_PLCCommand[1] = 0;
_PLCCommand[2] = (byte)(_PLCCommand.length / 256);
_PLCCommand[3] = (byte)(_PLCCommand.length % 256);
_PLCCommand[4] = 2;
_PLCCommand[5] = -16;
_PLCCommand[6] = -128;
_PLCCommand[7] = 50;
_PLCCommand[8] = 1;
_PLCCommand[9] = 0;
_PLCCommand[10] = 0;
_PLCCommand[11] = 0;
_PLCCommand[12] = 1;
_PLCCommand[13] = (byte)((_PLCCommand.length - 17) / 256);
_PLCCommand[14] = (byte)((_PLCCommand.length - 17) % 256);
_PLCCommand[15] = 0;
_PLCCommand[16] = 0;
_PLCCommand[17] = 4;
_PLCCommand[18] = (byte)readCount;
for(int ii = 0; ii < readCount; ++ii) {
_PLCCommand[19 + ii * 12] = 18;
_PLCCommand[20 + ii * 12] = 10;
_PLCCommand[21 + ii * 12] = 16;
_PLCCommand[22 + ii * 12] = 2;
_PLCCommand[23 + ii * 12] = (byte)(length[ii] / 256);
_PLCCommand[24 + ii * 12] = (byte)(length[ii] % 256);
_PLCCommand[25 + ii * 12] = (byte)((Integer)address[ii].Content3 / 256);
_PLCCommand[26 + ii * 12] = (byte)((Integer)address[ii].Content3 % 256);
//_PLCCommand[27 + ii * 12] = Byte.valueOf(address[ii].Content1.toString());
_PLCCommand[27 + ii * 12] = (byte)-124;
_PLCCommand[28 + ii * 12] = (byte)((Integer)address[ii].Content2 / 256 / 256 % 256);
_PLCCommand[29 + ii * 12] = (byte)((Integer)address[ii].Content2 / 256 % 256);
_PLCCommand[30 + ii * 12] = (byte)((Integer)address[ii].Content2 % 256);
}
return OperateResultExOne.CreateSuccessResult(_PLCCommand);
}
}
public static OperateResultExOne<byte[]> BuildBitReadCommand(String address) {
byte[] _PLCCommand = new byte[31];
_PLCCommand[0] = 3;
_PLCCommand[1] = 0;
_PLCCommand[2] = (byte)(_PLCCommand.length / 256);
_PLCCommand[3] = (byte)(_PLCCommand.length % 256);
_PLCCommand[4] = 2;
_PLCCommand[5] = -16;
_PLCCommand[6] = -128;
_PLCCommand[7] = 50;
_PLCCommand[8] = 1;
_PLCCommand[9] = 0;
_PLCCommand[10] = 0;
_PLCCommand[11] = 0;
_PLCCommand[12] = 1;
_PLCCommand[13] = (byte)((_PLCCommand.length - 17) / 256);
_PLCCommand[14] = (byte)((_PLCCommand.length - 17) % 256);
_PLCCommand[15] = 0;
_PLCCommand[16] = 0;
_PLCCommand[17] = 4;
_PLCCommand[18] = 1;
OperateResultExThree<Byte, Integer, Integer> analysis = AnalysisAddress(address);
if (!analysis.IsSuccess) {
return OperateResultExOne.CreateFailedResult(analysis);
} else {
_PLCCommand[19] = 18;
_PLCCommand[20] = 10;
_PLCCommand[21] = 16;
_PLCCommand[22] = 1;
_PLCCommand[23] = 0;
_PLCCommand[24] = 1;
_PLCCommand[25] = (byte)((Integer)analysis.Content3 / 256);
_PLCCommand[26] = (byte)((Integer)analysis.Content3 % 256);
//_PLCCommand[27] = (Byte)analysis.Content1;
_PLCCommand[27] = (byte)-124;
_PLCCommand[28] = (byte)((Integer)analysis.Content2 / 256 / 256 % 256);
_PLCCommand[29] = (byte)((Integer)analysis.Content2 / 256 % 256);
_PLCCommand[30] = (byte)((Integer)analysis.Content2 % 256);
return OperateResultExOne.CreateSuccessResult(_PLCCommand);
}
}
public static OperateResultExOne<byte[]> BuildWriteByteCommand(String address, byte[] data) {
if (data == null) {
data = new byte[0];
}
OperateResultExThree<Byte, Integer, Integer> analysis = AnalysisAddress(address);
return !analysis.IsSuccess ? OperateResultExOne.CreateFailedResult(analysis) : BuildWriteByteCommand(analysis, data);
}
public static OperateResultExOne<byte[]> BuildWriteByteCommand(OperateResultExThree<Byte, Integer, Integer> analysis, byte[] data) {
if (data == null) {
data = new byte[0];
}
byte[] _PLCCommand = new byte[35 + data.length];
_PLCCommand[0] = 3;
_PLCCommand[1] = 0;
_PLCCommand[2] = (byte)((35 + data.length) / 256);
_PLCCommand[3] = (byte)((35 + data.length) % 256);
_PLCCommand[4] = 2;
_PLCCommand[5] = -16;
_PLCCommand[6] = -128;
_PLCCommand[7] = 50;
_PLCCommand[8] = 1;
_PLCCommand[9] = 0;
_PLCCommand[10] = 0;
_PLCCommand[11] = 0;
_PLCCommand[12] = 1;
_PLCCommand[13] = 0;
_PLCCommand[14] = 14;
_PLCCommand[15] = (byte)((4 + data.length) / 256);
_PLCCommand[16] = (byte)((4 + data.length) % 256);
_PLCCommand[17] = 5;
_PLCCommand[18] = 1;
_PLCCommand[19] = 18;
_PLCCommand[20] = 10;
_PLCCommand[21] = 16;
_PLCCommand[22] = 2;
_PLCCommand[23] = (byte)(data.length / 256);
_PLCCommand[24] = (byte)(data.length % 256);
_PLCCommand[25] = (byte)((Integer)analysis.Content3 / 256);
_PLCCommand[26] = (byte)((Integer)analysis.Content3 % 256);
//_PLCCommand[27] = (Byte)analysis.Content1;
_PLCCommand[27] = (byte)-124;
_PLCCommand[28] = (byte)((Integer)analysis.Content2 / 256 / 256 % 256);
_PLCCommand[29] = (byte)((Integer)analysis.Content2 / 256 % 256);
_PLCCommand[30] = (byte)((Integer)analysis.Content2 % 256);
_PLCCommand[31] = 0;
_PLCCommand[32] = 4;
_PLCCommand[33] = (byte)(data.length * 8 / 256);
_PLCCommand[34] = (byte)(data.length * 8 % 256);
System.arraycopy(data, 0, _PLCCommand, 35, data.length);
return OperateResultExOne.CreateSuccessResult(_PLCCommand);
}
public static OperateResultExOne<byte[]> BuildWriteBitCommand(String address, boolean data) {
OperateResultExThree<Byte, Integer, Integer> analysis = AnalysisAddress(address);
if (!analysis.IsSuccess) {
return OperateResultExOne.CreateFailedResult(analysis);
} else {
byte[] buffer = new byte[]{(byte)(data ? 1 : 0)};
byte[] _PLCCommand = new byte[35 + buffer.length];
_PLCCommand[0] = 3;
_PLCCommand[1] = 0;
_PLCCommand[2] = (byte)((35 + buffer.length) / 256);
_PLCCommand[3] = (byte)((35 + buffer.length) % 256);
_PLCCommand[4] = 2;
_PLCCommand[5] = -16;
_PLCCommand[6] = -128;
_PLCCommand[7] = 50;
_PLCCommand[8] = 1;
_PLCCommand[9] = 0;
_PLCCommand[10] = 0;
_PLCCommand[11] = 0;
_PLCCommand[12] = 1;
_PLCCommand[13] = 0;
_PLCCommand[14] = 14;
_PLCCommand[15] = (byte)((4 + buffer.length) / 256);
_PLCCommand[16] = (byte)((4 + buffer.length) % 256);
_PLCCommand[17] = 5;
_PLCCommand[18] = 1;
_PLCCommand[19] = 18;
_PLCCommand[20] = 10;
_PLCCommand[21] = 16;
_PLCCommand[22] = 1;
_PLCCommand[23] = (byte)(buffer.length / 256);
_PLCCommand[24] = (byte)(buffer.length % 256);
_PLCCommand[25] = (byte)((Integer)analysis.Content3 / 256);
_PLCCommand[26] = (byte)((Integer)analysis.Content3 % 256);
//int a=analysis.Content1;
int a=-124;
_PLCCommand[27] = (byte)a;
_PLCCommand[28] = (byte)((Integer)analysis.Content2 / 256 / 256);
_PLCCommand[29] = (byte)((Integer)analysis.Content2 / 256);
_PLCCommand[30] = (byte)((Integer)analysis.Content2 % 256);
_PLCCommand[31] = 0;
_PLCCommand[32] = 3;
_PLCCommand[33] = (byte)(buffer.length / 256);
_PLCCommand[34] = (byte)(buffer.length % 256);
System.arraycopy(buffer, 0, _PLCCommand, 35, buffer.length);
return OperateResultExOne.CreateSuccessResult(_PLCCommand);
}
}
public byte[] TransByte(byte[] buffer, int index, int length) {
byte[] tmp = new byte[length];
System.arraycopy(buffer, index, tmp, 0, length);
return tmp;
}
public static String getNumberByString(String str1){
String regEx="[^0-9]";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str1);
//System.out.println( m.replaceAll("").trim());
return m.replaceAll("").trim();
}
public static void main(String[] args) {
String str1 = "DBCHAR12";
getNumberByString(str1);
}
}