forked from S7NetPlus/s7netplus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
S7NetTestsAsync.cs
1105 lines (961 loc) · 47.8 KB
/
S7NetTestsAsync.cs
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
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#region Using
using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using S7.Net.UnitTest.Helpers;
using S7.Net.Types;
using S7.UnitTest.Helpers;
using System.Threading.Tasks;
using System.Threading;
using System.Security.Cryptography;
#if NET5_0_OR_GREATER
using System.Buffers;
#endif
#endregion
/**
* About the tests:
* ---------------------------------------------------------------------------
* The tests were written to show how to use this library to read and write
* different types of values, how to box and unbox values and of course to
* address some of the bugs of the library.
* These tests are not meant to cover 100% the code, but to check that once a
* variable is written, it stores the correct value.
* ----------------------------------------------------------------------------
* The plc used for the tests is the S7 "server" provided by Snap7 opensource
* library, that you can get for free here:http://snap7.sourceforge.net/
* The implementation of the server will not be discussed here, but there are
* some issues with the interop that cause the server, and unit test, to fail
* under some circumstances, like "click on Run all tests" too much.
* This doesn't mean that S7.Net has bugs, but that the implementation of the
* server has problems.
*
*/
//Tests for Async Methods
namespace S7.Net.UnitTest
{
public partial class S7NetTests
{
#region Tests
[TestMethod]
public async Task Test_Async_Connection()
{
if (plc.IsConnected == false)
{
await plc.OpenAsync();
}
}
/// <summary>
/// Read/Write a single Int16 or UInt16 with a single request.
/// Test that writing a UInt16 (ushort) and reading it gives the correct value.
/// Test also that writing a Int16 (short) and reading it gives the correct value.
/// </summary>
[TestMethod]
public async Task Test_Async_WriteAndReadInt16Variable()
{
Assert.IsTrue(plc.IsConnected, "Before executing this test, the plc must be connected. Check constructor.");
// To write a ushort i don't need any cast, only unboxing must be done
ushort val = 40000;
await plc.WriteAsync("DB1.DBW0", val);
ushort result = (ushort)await plc.ReadAsync("DB1.DBW0");
Assert.AreEqual(val, result, "A ushort goes from 0 to 64512");
// To write a short i need to convert it to UShort, then i need to reconvert the readed value to get
// the negative sign back
// Depending if i'm writing on a DWORD or on a DEC, i will see ushort or short value in the plc
short value = -100;
Assert.IsTrue(plc.IsConnected, "After connecting, IsConnected must be set to true");
await plc.WriteAsync("DB1.DBW0", value.ConvertToUshort());
short result2 = ((ushort)await plc.ReadAsync("DB1.DBW0")).ConvertToShort();
Assert.AreEqual(value, result2, "A short goes from -32767 to 32766");
}
/// <summary>
/// Read/Write a single Int32 or UInt32 with a single request.
/// Test that writing a UInt32 (uint) and reading it gives the correct value.
/// Test also that writing a Int32 (int) and reading it gives the correct value.
/// </summary>
[TestMethod]
public async Task Test_Async_WriteAndReadInt32Variable()
{
Assert.IsTrue(plc.IsConnected, "Before executing this test, the plc must be connected. Check constructor.");
// To write a uint I don't need any cast, only unboxing must be done
int val = 1000;
await plc.WriteAsync("DB1.DBD40", val);
int result = ((uint)await plc.ReadAsync("DB1.DBD40")).ConvertToInt();
Assert.AreEqual(val, result);
// To write a int I need to convert it to uint, then I need to reconvert the readed value to get
// the negative sign back
// Depending if I'm writing on a DBD or on a LONG, I will see uint or int value in the plc
int value = -60000;
await plc.WriteAsync("DB1.DBD60", value);
int result2 = ((uint)await plc.ReadAsync("DB1.DBD60")).ConvertToInt();
Assert.AreEqual(value, result2);
}
/// <summary>
/// Read/Write a single REAL with a single request.
/// Test that writing a float and reading it gives the correct value.
/// </summary>
[TestMethod]
public async Task Test_Async_WriteAndReadRealVariables()
{
Assert.IsTrue(plc.IsConnected, "Before executing this test, the plc must be connected. Check constructor.");
// Reading and writing a float is quite complicated, because it needs to be converted to DWord before the write,
// then reconvert to float after the read. Float values can contain only 7 digits, so no precision is lost.
float val2 = 1234567;
await plc.WriteAsync("DB1.DBD40", val2.ConvertToUInt());
float result2 = ((uint)await plc.ReadAsync("DB1.DBD40")).ConvertToFloat();
Assert.AreEqual(val2, result2);
float val3 = 12.34567f;
await plc.WriteAsync("DB1.DBD40", val3.ConvertToUInt());
float result3 = ((uint)await plc.ReadAsync("DB1.DBD40")).ConvertToFloat();
Assert.AreEqual(val3, result3);
}
/// <summary>
/// Write/Read a large amount of data to test PDU max
/// </summary>
[TestMethod]
public async Task Test_Async_WriteLargeByteArray()
{
Assert.IsTrue(plc.IsConnected, "Before executing this test, the plc must be connected. Check constructor.");
var randomEngine = new Random();
var data = new byte[8192];
var db = 2;
randomEngine.NextBytes(data);
await plc.WriteBytesAsync(DataType.DataBlock, db, 0, data);
var readData = await plc.ReadBytesAsync(DataType.DataBlock, db, 0, data.Length);
CollectionAssert.AreEqual(data, readData);
}
#if NET5_0_OR_GREATER
/// <summary>
/// Write/Read a large amount of data to test PDU max
/// </summary>
[TestMethod]
public async Task Test_Async_WriteLargeByteArrayWithMemory()
{
Assert.IsTrue(plc.IsConnected, "Before executing this test, the plc must be connected. Check constructor.");
var randomEngine = new Random();
using var dataOwner = MemoryPool<byte>.Shared.Rent(8192);
var data = dataOwner.Memory.Slice(0, 8192);
var db = 2;
randomEngine.NextBytes(data.Span);
await plc.WriteBytesAsync(DataType.DataBlock, db, 0, data);
using var readDataOwner = MemoryPool<byte>.Shared.Rent(data.Length);
var readData = readDataOwner.Memory.Slice(0, data.Length);
await plc.ReadBytesAsync(readData, DataType.DataBlock, db, 0);
CollectionAssert.AreEqual(data.ToArray(), readData.ToArray());
}
#endif
/// <summary>
/// Read/Write a class that has the same properties of a DB with the same field in the same order
/// </summary>
[TestMethod]
public async Task Test_Async_ReadAndWriteClass()
{
Assert.IsTrue(plc.IsConnected, "Before executing this test, the plc must be connected. Check constructor.");
TestClass tc = new TestClass
{
BitVariable00 = true,
BitVariable10 = true,
DIntVariable = -100000,
IntVariable = -15000,
LRealVariable = -154.789,
RealVariable = -154.789f,
DWordVariable = 850,
WStringVariable = "ÄÜÉÊéà",
StringVariable = "Hallo"
};
await plc.WriteClassAsync(tc, DB2);
TestClass tc2 = new TestClass();
// Values that are read from a class are stored inside the class itself, that is passed by reference
await plc.ReadClassAsync(tc2, DB2);
Assert.AreEqual(tc.BitVariable00, tc2.BitVariable00);
Assert.AreEqual(tc.BitVariable10, tc2.BitVariable10);
Assert.AreEqual(tc.DIntVariable, tc2.DIntVariable);
Assert.AreEqual(tc.IntVariable, tc2.IntVariable);
Assert.AreEqual(tc.LRealVariable, tc2.LRealVariable);
Assert.AreEqual(tc.RealVariable, tc2.RealVariable);
Assert.AreEqual(tc.DWordVariable, tc2.DWordVariable);
Assert.AreEqual(tc.WStringVariable, tc2.WStringVariable);
Assert.AreEqual(tc.StringVariable, tc2.StringVariable);
}
[TestMethod]
public async Task Test_Async_ReadAndWriteNestedClass()
{
Assert.IsTrue(plc.IsConnected, "Before executing this test, the plc must be connected. Check constructor.");
TestClassWithNestedClass tc = new TestClassWithNestedClass
{
BitVariable00 = true,
BitVariable01 = new TestClassInnerWithBool { BitVariable00 = true },
ByteVariable02 = new TestClassInnerWithByte { ByteVariable00 = 128 },
BitVariable03 = true,
ShortVariable04 = new TestClassInnerWithShort { ShortVarialbe00 = -15000 }
};
await plc.WriteClassAsync(tc, DB4);
TestClassWithNestedClass tc2 = new TestClassWithNestedClass();
// Values that are read from a class are stored inside the class itself, that is passed by reference
await plc.ReadClassAsync(tc2, DB4);
Assert.AreEqual(tc.BitVariable00, tc2.BitVariable00);
Assert.AreEqual(tc.BitVariable01.BitVariable00, tc2.BitVariable01.BitVariable00);
Assert.AreEqual(tc.ByteVariable02.ByteVariable00, tc2.ByteVariable02.ByteVariable00);
Assert.AreEqual(tc.BitVariable03, tc2.BitVariable03);
Assert.AreEqual(tc.ShortVariable04.ShortVarialbe00, tc2.ShortVariable04.ShortVarialbe00);
}
/// <summary>
/// Read/Write a struct that has the same properties of a DB with the same field in the same order
/// </summary>
[TestMethod]
public async Task Test_Async_ReadAndWriteStruct()
{
Assert.IsTrue(plc.IsConnected, "Before executing this test, the plc must be connected. Check constructor.");
TestStruct tc = new TestStruct
{
BitVariable00 = true,
BitVariable10 = true,
DIntVariable = -100000,
IntVariable = -15000,
LRealVariable = -154.789,
RealVariable = -154.789f,
DWordVariable = 850,
WStringVariable = "ÄÜÉÊéà",
StringVariable = "Hallo"
};
plc.WriteStruct(tc, DB2);
// Values that are read from a struct are stored in a new struct, returned by the funcion ReadStruct
TestStruct tc2 = (TestStruct)await plc.ReadStructAsync(typeof(TestStruct), DB2);
Assert.AreEqual(tc.BitVariable00, tc2.BitVariable00);
Assert.AreEqual(tc.BitVariable10, tc2.BitVariable10);
Assert.AreEqual(tc.DIntVariable, tc2.DIntVariable);
Assert.AreEqual(tc.IntVariable, tc2.IntVariable);
Assert.AreEqual(tc.LRealVariable, tc2.LRealVariable);
Assert.AreEqual(tc.RealVariable, tc2.RealVariable);
Assert.AreEqual(tc.DWordVariable, tc2.DWordVariable);
Assert.AreEqual(tc.WStringVariable, tc2.WStringVariable);
Assert.AreEqual(tc.StringVariable, tc2.StringVariable);
}
/// <summary>
/// Read/Write a struct that has the same properties of a DB with the same field in the same order
/// </summary>
[TestMethod]
public async Task Test_Async_ReadAndWriteLongStruct()
{
Assert.IsTrue(plc.IsConnected, "Before executing this test, the plc must be connected. Check constructor.");
TestLongStruct tc = new TestLongStruct
{
IntVariable0 = 0,
IntVariable1 = 1,
IntVariable10 = 10,
IntVariable11 = 11,
IntVariable20 = 20,
IntVariable21 = 21,
IntVariable30 = 30,
IntVariable31 = 31,
IntVariable40 = 40,
IntVariable41 = 41,
IntVariable50 = 50,
IntVariable51 = 51,
IntVariable60 = 60,
IntVariable61 = 61,
IntVariable70 = 70,
IntVariable71 = 71,
IntVariable80 = 80,
IntVariable81 = 81,
IntVariable90 = 90,
IntVariable91 = 91,
IntVariable100 = 100,
IntVariable101 = 101,
IntVariable110 = 200,
IntVariable111 = 201
};
plc.WriteStruct(tc, DB2);
// Values that are read from a struct are stored in a new struct, returned by the funcion ReadStruct
TestLongStruct tc2 = (TestLongStruct)await plc.ReadStructAsync(typeof(TestLongStruct), DB2);
Assert.AreEqual(tc.IntVariable0, tc2.IntVariable0);
Assert.AreEqual(tc.IntVariable1, tc2.IntVariable1);
Assert.AreEqual(tc.IntVariable10, tc2.IntVariable10);
Assert.AreEqual(tc.IntVariable11, tc2.IntVariable11);
Assert.AreEqual(tc.IntVariable20, tc2.IntVariable20);
Assert.AreEqual(tc.IntVariable21, tc2.IntVariable21);
Assert.AreEqual(tc.IntVariable30, tc2.IntVariable30);
Assert.AreEqual(tc.IntVariable31, tc2.IntVariable31);
Assert.AreEqual(tc.IntVariable40, tc2.IntVariable40);
Assert.AreEqual(tc.IntVariable41, tc2.IntVariable41);
Assert.AreEqual(tc.IntVariable50, tc2.IntVariable50);
Assert.AreEqual(tc.IntVariable51, tc2.IntVariable51);
Assert.AreEqual(tc.IntVariable60, tc2.IntVariable60);
Assert.AreEqual(tc.IntVariable61, tc2.IntVariable61);
Assert.AreEqual(tc.IntVariable70, tc2.IntVariable70);
Assert.AreEqual(tc.IntVariable71, tc2.IntVariable71);
Assert.AreEqual(tc.IntVariable80, tc2.IntVariable80);
Assert.AreEqual(tc.IntVariable81, tc2.IntVariable81);
Assert.AreEqual(tc.IntVariable90, tc2.IntVariable90);
Assert.AreEqual(tc.IntVariable91, tc2.IntVariable91);
Assert.AreEqual(tc.IntVariable100, tc2.IntVariable100);
Assert.AreEqual(tc.IntVariable101, tc2.IntVariable101);
Assert.AreEqual(tc.IntVariable110, tc2.IntVariable110);
Assert.AreEqual(tc.IntVariable111, tc2.IntVariable111);
}
/// <summary>
/// Read/Write a class that has the same properties of a DB with the same field in the same order
/// </summary>
[TestMethod]
public async Task Test_Async_ReadAndWriteLongClass()
{
Assert.IsTrue(plc.IsConnected, "Before executing this test, the plc must be connected. Check constructor.");
TestLongClass tc = new TestLongClass
{
IntVariable0 = 0,
IntVariable1 = 1,
IntVariable10 = 10,
IntVariable11 = 11,
IntVariable20 = 20,
IntVariable21 = 21,
IntVariable30 = 30,
IntVariable31 = 31,
IntVariable40 = 40,
IntVariable41 = 41,
IntVariable50 = 50,
IntVariable51 = 51,
IntVariable60 = 60,
IntVariable61 = 61,
IntVariable70 = 70,
IntVariable71 = 71,
IntVariable80 = 80,
IntVariable81 = 81,
IntVariable90 = 90,
IntVariable91 = 91,
IntVariable100 = 100,
IntVariable101 = 101,
IntVariable110 = 200,
IntVariable111 = 201
};
await plc.WriteClassAsync(tc, DB2);
// Values that are read from a struct are stored in a new struct, returned by the funcion ReadStruct
TestLongClass tc2 = new TestLongClass();
await plc.ReadClassAsync(tc2, DB2);
Assert.AreEqual(tc.IntVariable0, tc2.IntVariable0);
Assert.AreEqual(tc.IntVariable1, tc2.IntVariable1);
Assert.AreEqual(tc.IntVariable10, tc2.IntVariable10);
Assert.AreEqual(tc.IntVariable11, tc2.IntVariable11);
Assert.AreEqual(tc.IntVariable20, tc2.IntVariable20);
Assert.AreEqual(tc.IntVariable21, tc2.IntVariable21);
Assert.AreEqual(tc.IntVariable30, tc2.IntVariable30);
Assert.AreEqual(tc.IntVariable31, tc2.IntVariable31);
Assert.AreEqual(tc.IntVariable40, tc2.IntVariable40);
Assert.AreEqual(tc.IntVariable41, tc2.IntVariable41);
Assert.AreEqual(tc.IntVariable50, tc2.IntVariable50);
Assert.AreEqual(tc.IntVariable51, tc2.IntVariable51);
Assert.AreEqual(tc.IntVariable60, tc2.IntVariable60);
Assert.AreEqual(tc.IntVariable61, tc2.IntVariable61);
Assert.AreEqual(tc.IntVariable70, tc2.IntVariable70);
Assert.AreEqual(tc.IntVariable71, tc2.IntVariable71);
Assert.AreEqual(tc.IntVariable80, tc2.IntVariable80);
Assert.AreEqual(tc.IntVariable81, tc2.IntVariable81);
Assert.AreEqual(tc.IntVariable90, tc2.IntVariable90);
Assert.AreEqual(tc.IntVariable91, tc2.IntVariable91);
Assert.AreEqual(tc.IntVariable100, tc2.IntVariable100);
Assert.AreEqual(tc.IntVariable101, tc2.IntVariable101);
Assert.AreEqual(tc.IntVariable110, tc2.IntVariable110);
Assert.AreEqual(tc.IntVariable111, tc2.IntVariable111);
}
/// <summary>
/// Tests that a read and a write on addresses bigger than 8192 are executed correctly
/// </summary>
[TestMethod]
public async Task Test_Async_WriteAndReadInt16VariableAddress8192()
{
Assert.IsTrue(plc.IsConnected, "Before executing this test, the plc must be connected. Check constructor.");
// To write a ushort i don't need any cast, only unboxing must be done
ushort val = 8192;
await plc.WriteAsync("DB2.DBW8192", val);
ushort result = (ushort)await plc.ReadAsync("DB2.DBW8192");
Assert.AreEqual(val, result, "A ushort goes from 0 to 64512");
// To write a short i need to convert it to UShort, then i need to reconvert the readed value to get
// the negative sign back
// Depending if i'm writing on a DWORD or on a DEC, i will see ushort or short value in the plc
short value = -8192;
Assert.IsTrue(plc.IsConnected, "After connecting, IsConnected must be set to true");
await plc.WriteAsync("DB2.DBW8192", value.ConvertToUshort());
short result2 = ((ushort)await plc.ReadAsync("DB2.DBW8192")).ConvertToShort();
Assert.AreEqual(value, result2, "A short goes from -32767 to 32766");
}
/// <summary>
/// Tests that a read and a write on addresses bigger than 8192 are executed correctly
/// </summary>
[TestMethod]
public async Task Test_Async_WriteAndReadInt16VariableAddress16384()
{
Assert.IsTrue(plc.IsConnected, "Before executing this test, the plc must be connected. Check constructor.");
// To write a ushort i don't need any cast, only unboxing must be done
ushort val = 16384;
await plc.WriteAsync("DB2.DBW16384", val);
ushort result = (ushort)await plc.ReadAsync("DB2.DBW16384");
Assert.AreEqual(val, result, "A ushort goes from 0 to 64512");
// To write a short i need to convert it to UShort, then i need to reconvert the readed value to get
// the negative sign back
// Depending if i'm writing on a DWORD or on a DEC, i will see ushort or short value in the plc
short value = -16384;
Assert.IsTrue(plc.IsConnected, "After connecting, IsConnected must be set to true");
await plc.WriteAsync("DB2.DBW16384", value.ConvertToUshort());
short result2 = ((ushort)await plc.ReadAsync("DB2.DBW16384")).ConvertToShort();
Assert.AreEqual(value, result2, "A short goes from -32767 to 32766");
}
[TestMethod]
public async Task Test_Async_ReadMultipleBytes()
{
Assert.IsTrue(plc.IsConnected, "Before executing this test, the plc must be connected. Check constructor.");
bool val = true;
await plc.WriteAsync("DB2.DBX0.5", val);
bool result = (bool)await plc.ReadAsync("DB2.DBX0.5");
Assert.AreEqual(val, result);
ushort val1 = 16384;
await plc.WriteAsync("DB2.DBW16384", val1);
ushort result1 = (ushort)await plc.ReadAsync("DB2.DBW16384");
Assert.AreEqual(val1, result1, "A ushort goes from 0 to 64512");
bool val2 = true;
await plc.WriteAsync("DB2.DBX8192.7", val2);
bool result2 = (bool)await plc.ReadAsync("DB2.DBX8192.7");
Assert.AreEqual(val2, result2);
ushort val3 = 129;
await plc.WriteAsync("DB2.DBW16", val3);
ushort result3 = (ushort)await plc.ReadAsync("DB2.DBW16");
Assert.AreEqual(val3, result3, "A ushort goes from 0 to 64512");
byte[] val4 = new byte[] { 0x12, 0x34 };
await plc.WriteAsync("DB2.DBB2048", val4[0]);
await plc.WriteAsync("DB2.DBB2049", val4[1]);
byte result4b0 = (byte)await plc.ReadAsync("DB2.DBB2048");
byte result4b1 = (byte)await plc.ReadAsync("DB2.DBB2049");
Assert.AreEqual(val4[0], result4b0);
Assert.AreEqual(val4[1], result4b1);
bool val6 = true;
await plc.WriteAsync("DB2.DBX16384.6", val6);
bool result6 = (bool)await plc.ReadAsync("DB2.DBX16384.6");
Assert.AreEqual(val6, result6);
var dataItems = new List<DataItem>()
{
new DataItem
{
Count = 1,
DataType = DataType.DataBlock,
DB = 2,
StartByteAdr = 0,
BitAdr = 5,
VarType = VarType.Bit
}
,new DataItem
{
Count = 1,
DataType = DataType.DataBlock,
DB = 2,
StartByteAdr = 16384,
VarType = VarType.Word
},
new DataItem
{
Count = 1,
DataType = DataType.DataBlock,
DB = 2,
StartByteAdr = 8192,
BitAdr = 7,
VarType = VarType.Bit
},
new DataItem
{
Count = 1,
DataType = DataType.DataBlock,
DB = 2,
StartByteAdr = 16,
VarType = VarType.Word
},
// single byte
new DataItem
{
Count = 1,
DataType = DataType.DataBlock,
DB = 2,
StartByteAdr = 2048,
VarType = VarType.Byte
},
// multiple bytes
new DataItem
{
Count = 2,
DataType = DataType.DataBlock,
DB = 2,
StartByteAdr = 2048,
VarType = VarType.Byte
},
new DataItem
{
Count = 1,
DataType = DataType.DataBlock,
DB = 2,
StartByteAdr = 16384,
BitAdr = 6,
VarType = VarType.Bit
},
};
var dataItemsRes = await plc.ReadMultipleVarsAsync(dataItems);
Assert.AreEqual(val, dataItemsRes[0].Value);
Assert.AreEqual(val1, dataItemsRes[1].Value);
Assert.AreEqual(val2, dataItemsRes[2].Value);
Assert.AreEqual(val3, dataItemsRes[3].Value);
Assert.AreEqual(val4[0], dataItemsRes[4].Value);
Assert.AreEqual(val4[0], ((byte[])dataItemsRes[5].Value)[0]); //dataItem[5].Value should be byte[2]
Assert.AreEqual(val4[1], ((byte[])dataItemsRes[5].Value)[1]);
Assert.AreEqual(val6, dataItemsRes[6].Value);
}
/// <summary>
/// Tests that a read and a write on addresses bigger than 8192 are executed correctly
/// </summary>
[TestMethod]
public async Task Test_Async_WriteAndReadBooleanVariable()
{
Assert.IsTrue(plc.IsConnected, "Before executing this test, the plc must be connected. Check constructor.");
// tests when writing true/false
await plc.WriteAsync("DB1.DBX0.0", false);
var boolVariable = (bool)await plc.ReadAsync("DB1.DBX0.0");
Assert.IsFalse(boolVariable);
await plc.WriteAsync("DB1.DBX0.0", true);
boolVariable = (bool)await plc.ReadAsync("DB1.DBX0.0");
Assert.IsTrue(boolVariable);
// tests when writing 0/1
await plc.WriteAsync("DB1.DBX0.0", 0);
boolVariable = (bool)await plc.ReadAsync("DB1.DBX0.0");
Assert.IsFalse(boolVariable);
await plc.WriteAsync("DB1.DBX0.0", 1);
boolVariable = (bool)await plc.ReadAsync("DB1.DBX0.0");
Assert.IsTrue(boolVariable);
await plc.WriteAsync("DB1.DBX0.7", 1);
boolVariable = (bool)await plc.ReadAsync("DB1.DBX0.7");
Assert.IsTrue(boolVariable);
await plc.WriteAsync("DB1.DBX0.7", 0);
boolVariable = (bool)await plc.ReadAsync("DB1.DBX0.7");
Assert.IsFalse(boolVariable);
await plc.WriteAsync("DB1.DBX658.0", 1);
boolVariable = (bool)await plc.ReadAsync("DB1.DBX658.0");
Assert.IsTrue(boolVariable);
await plc.WriteAsync("DB1.DBX658.7", 1);
boolVariable = (bool)await plc.ReadAsync("DB1.DBX658.7");
Assert.IsTrue(boolVariable);
await plc.WriteAsync("DB2.DBX9658.0", 1);
boolVariable = (bool)await plc.ReadAsync("DB2.DBX9658.0");
Assert.IsTrue(boolVariable);
}
[TestMethod]
public async Task Test_Async_ReadClassIgnoresNonPublicSetters()
{
Assert.IsTrue(plc.IsConnected, "Before executing this test, the plc must be connected. Check constructor.");
TestClassWithPrivateSetters tc = new TestClassWithPrivateSetters
{
BitVariable00 = true,
BitVariable10 = true,
DIntVariable = -100000,
IntVariable = -15000,
LRealVariable = -154.789,
RealVariable = -154.789f,
DWordVariable = 850,
WStringVariable = "ÄÜÉÊéà",
StringVariable = "Hallo"
};
await plc.WriteClassAsync(tc, DB2);
TestClassWithPrivateSetters tc2 = new TestClassWithPrivateSetters();
// Values that are read from a class are stored inside the class itself, that is passed by reference
var res = await plc.ReadClassAsync(tc2, DB2);
tc = (TestClassWithPrivateSetters)res.Item2;
Assert.AreEqual(tc.BitVariable00, tc2.BitVariable00);
Assert.AreEqual(tc.BitVariable10, tc2.BitVariable10);
Assert.AreEqual(tc.DIntVariable, tc2.DIntVariable);
Assert.AreEqual(tc.IntVariable, tc2.IntVariable);
Assert.AreEqual(tc.LRealVariable, tc2.LRealVariable, 0.1);
Assert.AreEqual(tc.RealVariable, tc2.RealVariable);
Assert.AreEqual(tc.DWordVariable, tc2.DWordVariable);
Assert.AreEqual(TestClassWithPrivateSetters.PRIVATE_SETTER_VALUE, tc2.PrivateSetterProperty);
Assert.AreEqual(TestClassWithPrivateSetters.PROTECTED_SETTER_VALUE, tc2.ProtectedSetterProperty);
Assert.AreEqual(TestClassWithPrivateSetters.INTERNAL_SETTER_VALUE, tc2.InternalSetterProperty);
Assert.AreEqual(TestClassWithPrivateSetters.JUST_A_GETTER_VALUE, tc2.JustAGetterProperty);
}
[TestMethod]
public async Task Test_Async_ReadBytesReturnsNullIfPlcIsNotConnected()
{
using (var notConnectedPlc = new Plc(CpuType.S7300, "255.255.255.255", 0, 0))
{
Assert.IsFalse(notConnectedPlc.IsConnected);
TestClass tc = new TestClass();
await Assert.ThrowsExceptionAsync<PlcException>(async () => await notConnectedPlc.ReadClassAsync(tc, DB2));
}
}
[TestMethod]
public async Task Test_Async_ReadClassWithGenericReturnsSameResultAsReadClassWithoutGeneric()
{
Assert.IsTrue(plc.IsConnected, "Before executing this test, the plc must be connected. Check constructor.");
TestClass tc = new TestClass
{
BitVariable00 = true,
BitVariable10 = true,
DIntVariable = -100000,
IntVariable = -15000,
LRealVariable = -154.789,
RealVariable = -154.789f,
DWordVariable = 850,
WStringVariable = "ÄÜÉÊéà",
StringVariable = "Hallo"
};
await plc.WriteClassAsync(tc, DB2);
// Values that are read from a class are stored inside the class itself, that is passed by reference
TestClass tc2 = new TestClass();
var res = await plc.ReadClassAsync(tc2, DB2);
tc2 = (TestClass)res.Item2;
TestClass tc2Generic = await plc.ReadClassAsync<TestClass>(DB2);
Assert.AreEqual(tc2.BitVariable00, tc2Generic.BitVariable00);
Assert.AreEqual(tc2.BitVariable10, tc2Generic.BitVariable10);
Assert.AreEqual(tc2.DIntVariable, tc2Generic.DIntVariable);
Assert.AreEqual(tc2.IntVariable, tc2Generic.IntVariable);
Assert.AreEqual(Math.Round(tc2.LRealVariable, 3), Math.Round(tc2Generic.LRealVariable, 3));
Assert.AreEqual(tc2.RealVariable, tc2Generic.RealVariable);
Assert.AreEqual(tc2.DWordVariable, tc2Generic.DWordVariable);
Assert.AreEqual(tc2.WStringVariable, tc2Generic.WStringVariable);
Assert.AreEqual(tc2.StringVariable, tc2Generic.StringVariable);
}
[TestMethod]
public async Task Test_Async_ReadClassWithGenericReturnsNullIfPlcIsNotConnected()
{
using (var notConnectedPlc = new Plc(CpuType.S7300, "255.255.255.255", 0, 0))
{
Assert.IsFalse(notConnectedPlc.IsConnected, "Before executing this test, the plc must be connected. Check constructor.");
await Assert.ThrowsExceptionAsync<PlcException>(async () => await notConnectedPlc.ReadClassAsync<TestClass>(DB2));
}
}
[TestMethod]
public async Task Test_Async_ReadClassWithGenericAndClassFactoryReturnsSameResultAsReadClassWithoutGeneric()
{
Assert.IsTrue(plc.IsConnected, "Before executing this test, the plc must be connected. Check constructor.");
TestClass tc = new TestClass
{
BitVariable00 = true,
BitVariable10 = true,
DIntVariable = -100000,
IntVariable = -15000,
LRealVariable = -154.789,
RealVariable = -154.789f,
DWordVariable = 850,
WStringVariable = "ÄÜÉÊéà",
StringVariable = "Hallo"
};
await plc.WriteClassAsync(tc, DB2);
// Values that are read from a class are stored inside the class itself, that is passed by reference
TestClass tc2Generic = await plc.ReadClassAsync<TestClass>(DB2);
TestClass tc2GenericWithClassFactory = await plc.ReadClassAsync(() => new TestClass(), DB2);
Assert.AreEqual(tc2Generic.BitVariable00, tc2GenericWithClassFactory.BitVariable00);
Assert.AreEqual(tc2Generic.BitVariable10, tc2GenericWithClassFactory.BitVariable10);
Assert.AreEqual(tc2Generic.DIntVariable, tc2GenericWithClassFactory.DIntVariable);
Assert.AreEqual(Math.Round(tc2Generic.LRealVariable, 3), Math.Round(tc2GenericWithClassFactory.LRealVariable, 3));
Assert.AreEqual(tc2Generic.RealVariable, tc2GenericWithClassFactory.RealVariable);
Assert.AreEqual(tc2Generic.DWordVariable, tc2GenericWithClassFactory.DWordVariable);
Assert.AreEqual(tc2Generic.WStringVariable, tc2GenericWithClassFactory.WStringVariable);
Assert.AreEqual(tc2Generic.StringVariable, tc2GenericWithClassFactory.StringVariable);
}
[TestMethod]
public async Task Test_Async_ReadClassWithGenericAndClassFactoryThrowsExceptionPlcIsNotConnected()
{
using (var notConnectedPlc = new Plc(CpuType.S7300, "255.255.255.255", 0, 0))
{
Assert.IsFalse(notConnectedPlc.IsConnected);
await Assert.ThrowsExceptionAsync<PlcException>(async () => await notConnectedPlc.ReadClassAsync(() => new TestClass(), DB2));
}
}
[TestMethod]
public async Task Test_Async_ReadClassWithNestedClassAfterBit()
{
Assert.IsTrue(plc.IsConnected, "Before executing this test, the plc must be connected. Check constructor.");
Assert.AreEqual(6, Types.Class.GetClassSize(new TestClassWithNestedClass()));
TestClassWithNestedClass tc = new TestClassWithNestedClass();
tc.BitVariable00 = true;
tc.BitVariable01.BitVariable00 = true;
tc.ByteVariable02.ByteVariable00 = 128;
tc.BitVariable03 = true;
tc.ShortVariable04.ShortVarialbe00 = -15000;
TestClassWithNestedClass tc2 = await plc.ReadClassAsync<TestClassWithNestedClass>(DB4);
Assert.AreEqual(tc.BitVariable00, tc2.BitVariable00);
Assert.AreEqual(tc.BitVariable01.BitVariable00, tc2.BitVariable01.BitVariable00);
Assert.AreEqual(tc.ByteVariable02.ByteVariable00, tc2.ByteVariable02.ByteVariable00);
Assert.AreEqual(tc.BitVariable03, tc2.BitVariable03);
Assert.AreEqual(tc.ShortVariable04.ShortVarialbe00, tc2.ShortVariable04.ShortVarialbe00);
}
[TestMethod]
public async Task Test_Async_ReadStructThrowsExceptionPlcIsNotConnected()
{
using (var notConnectedPlc = new Plc(CpuType.S7300, "255.255.255.255", 0, 0))
{
Assert.IsFalse(notConnectedPlc.IsConnected);
await Assert.ThrowsExceptionAsync<PlcException>(async () => await notConnectedPlc.ReadStructAsync(typeof(TestStruct), DB2));
}
}
[TestMethod]
public async Task Test_Async_ReadStructWithGenericReturnsSameResultAsReadStructWithoutGeneric()
{
Assert.IsTrue(plc.IsConnected, "Before executing this test, the plc must be connected. Check constructor.");
TestStruct ts = new TestStruct
{
BitVariable00 = true,
BitVariable10 = true,
DIntVariable = -100000,
IntVariable = -15000,
LRealVariable = -154.789,
RealVariable = -154.789f,
DWordVariable = 850,
WStringVariable = "ÄÜÉÊéà",
StringVariable = "Hallo"
};
plc.WriteStruct(ts, DB2);
// Values that are read from a struct are stored in a new struct, returned by the funcion ReadStruct
TestStruct ts2 = (TestStruct)await plc.ReadStructAsync(typeof(TestStruct), DB2);
var test = await plc.ReadStructAsync<TestStruct>(DB2);
TestStruct ts2Generic = test.Value;
Assert.AreEqual(ts2.BitVariable00, ts2Generic.BitVariable00);
Assert.AreEqual(ts2.BitVariable10, ts2Generic.BitVariable10);
Assert.AreEqual(ts2.DIntVariable, ts2Generic.DIntVariable);
Assert.AreEqual(ts2.IntVariable, ts2Generic.IntVariable);
Assert.AreEqual(ts2.LRealVariable, ts2Generic.LRealVariable);
Assert.AreEqual(ts2.RealVariable, ts2Generic.RealVariable);
Assert.AreEqual(ts2.DWordVariable, ts2Generic.DWordVariable);
Assert.AreEqual(ts2.WStringVariable, ts2Generic.WStringVariable);
Assert.AreEqual(ts2.StringVariable, ts2Generic.StringVariable);
}
[TestMethod]
public async Task Test_Async_ReadStructWithGenericThrowsExceptionIfPlcIsNotConnected()
{
using (var notConnectedPlc = new Plc(CpuType.S7300, "255.255.255.255", 0, 0))
{
Assert.IsFalse(notConnectedPlc.IsConnected);
await Assert.ThrowsExceptionAsync<PlcException>(async () => await notConnectedPlc.ReadStructAsync<TestStruct>(DB2));
}
}
/// <summary>
/// Tests that the method ReadClass returns the number of bytes read from the plc
/// </summary>
[TestMethod]
public async Task Test_Async_ReadClassReturnsNumberOfReadBytesFromThePlc()
{
Assert.IsTrue(plc.IsConnected, "Before executing this test, the plc must be connected. Check constructor.");
TestClass tc = new TestClass
{
BitVariable00 = true,
BitVariable10 = true,
DIntVariable = -100000,
IntVariable = -15000,
LRealVariable = -154.789,
RealVariable = -154.789f,
DWordVariable = 850,
WStringVariable = "ÄÜÉÊéà",
StringVariable = "Hallo"
};
plc.WriteClass(tc, DB2);
int expectedReadBytes = (int)Types.Class.GetClassSize(tc);
TestClass tc2 = new TestClass();
// Values that are read from a class are stored inside the class itself, that is passed by reference
var res = await plc.ReadClassAsync(tc2, DB2);
int actualReadBytes = res.Item1;
Assert.AreEqual(expectedReadBytes, actualReadBytes);
}
[TestMethod]
public async Task Test_Async_ReadClassWithArray()
{
Assert.IsTrue(plc.IsConnected, "Before executing this test, the plc must be connected. Check constructor.");
TestClassWithArrays tc = new TestClassWithArrays
{
Bool = true
};
tc.BoolValues[1] = true;
tc.Int = int.MinValue;
tc.Ints[0] = int.MinValue;
tc.Ints[1] = int.MaxValue;
tc.Short = short.MinValue;
tc.Shorts[0] = short.MinValue;
tc.Shorts[1] = short.MaxValue;
tc.Double = float.MinValue;
tc.Doubles[0] = float.MinValue + 1;
tc.Doubles[1] = float.MaxValue;
tc.UShort = ushort.MinValue + 1;
tc.UShorts[0] = ushort.MinValue + 1;
tc.UShorts[1] = ushort.MaxValue;
plc.WriteClass(tc, DB2);
TestClassWithArrays tc2 = await plc.ReadClassAsync<TestClassWithArrays>(DB2);
Assert.AreEqual(tc.Bool, tc2.Bool);
Assert.AreEqual(tc.BoolValues[0], tc2.BoolValues[0]);
Assert.AreEqual(tc.BoolValues[1], tc2.BoolValues[1]);
Assert.AreEqual(tc.Int, tc2.Int);
Assert.AreEqual(tc.Ints[0], tc2.Ints[0]);
Assert.AreEqual(tc.Ints[1], tc.Ints[1]);
Assert.AreEqual(tc.Short, tc2.Short);
Assert.AreEqual(tc.Shorts[0], tc2.Shorts[0]);
Assert.AreEqual(tc.Shorts[1], tc2.Shorts[1]);
Assert.AreEqual(tc.Double, tc2.Double);
Assert.AreEqual(tc.Doubles[0], tc2.Doubles[0]);
Assert.AreEqual(tc.Doubles[1], tc2.Doubles[1]);
Assert.AreEqual(tc.UShort, tc2.UShort);
Assert.AreEqual(tc.UShorts[0], tc2.UShorts[0]);
Assert.AreEqual(tc.UShorts[1], tc2.UShorts[1]);
}
[TestMethod]
public async Task Test_Async_ReadClassWithArrayAndCustomType()
{
Assert.IsTrue(plc.IsConnected, "Before executing this test, the plc must be connected. Check constructor.");
TestClassWithCustomType tc = new TestClassWithCustomType
{
Int = int.MinValue,
CustomType = new CustomType()
};
tc.CustomType.Bools[1] = true;
tc.CustomTypes[0] = new CustomType();
tc.CustomTypes[1] = new CustomType();
tc.CustomTypes[0].Bools[0] = true;
tc.CustomTypes[1].Bools[1] = true;
plc.WriteClass(tc, DB2);
TestClassWithCustomType tc2 = await plc.ReadClassAsync<TestClassWithCustomType>(DB2);
Assert.AreEqual(tc.Int, tc2.Int);
Assert.AreEqual(tc.CustomType.Bools[0], tc2.CustomType.Bools[0]);
Assert.AreEqual(tc.CustomType.Bools[1], tc2.CustomType.Bools[1]);
Assert.AreEqual(tc.CustomTypes[0].Bools[0], tc2.CustomTypes[0].Bools[0]);
Assert.AreEqual(tc.CustomTypes[0].Bools[1], tc2.CustomTypes[0].Bools[1]);
Assert.AreEqual(tc.CustomTypes[1].Bools[0], tc2.CustomTypes[1].Bools[0]);
Assert.AreEqual(tc.CustomTypes[1].Bools[1], tc2.CustomTypes[1].Bools[1]);
}
[TestMethod]
public async Task Test_Async_ReadWriteSingle()
{
float test_value = 55.6632f;
await plc.WriteAsync("DB1.DBD0", test_value);
var helper = await plc.ReadAsync("DB1.DBD0");
float test_value2 = Conversion.ConvertToFloat((uint)helper);
Assert.AreEqual(test_value, test_value2, "Compare Write/Read"); //No delta, datatype matches
}
[TestMethod]
public async Task Test_Async_ReadWriteBytesMany()
{
Assert.IsTrue(plc.IsConnected, "Before executing this test, the plc must be connected. Check constructor.");
var count = 2000;
var dataItems = new List<byte>();
for (int i = 0; i < count; i++)
{
dataItems.Add((byte)(i % 256));
}
await plc.WriteBytesAsync(DataType.DataBlock, 2, 0, dataItems.ToArray());
var res = await plc.ReadBytesAsync(DataType.DataBlock, 2, 0, count);
for (int x = 0; x < count; x++)
{
Assert.AreEqual(x % 256, res[x], string.Format("Bit {0} failed", x));
}
}
#if NET5_0_OR_GREATER
[TestMethod]
public async Task Test_Async_ReadWriteBytesManyWithMemory()
{
Assert.IsTrue(plc.IsConnected, "Before executing this test, the plc must be connected. Check constructor.");
using var data = MemoryPool<byte>.Shared.Rent(2000);
for (int i = 0; i < data.Memory.Length; i++)
data.Memory.Span[i] = (byte)(i % 256);
await plc.WriteBytesAsync(DataType.DataBlock, 2, 0, data.Memory);
using var readData = MemoryPool<byte>.Shared.Rent(data.Memory.Length);
await plc.ReadBytesAsync(readData.Memory.Slice(0, data.Memory.Length), DataType.DataBlock, 2, 0);
for (int x = 0; x < data.Memory.Length; x++)
{
Assert.AreEqual(x % 256, readData.Memory.Span[x], string.Format("Bit {0} failed", x));
}
}
#endif
/// <summary>
/// Write a large amount of data and test cancellation
/// </summary>
[TestMethod]
public async Task Test_Async_WriteLargeByteArrayWithCancellation()
{
Assert.IsTrue(plc.IsConnected, "Before executing this test, the plc must be connected. Check constructor.");