@@ -95,7 +95,6 @@ private async Task SetupConnection(Stream stream, CancellationToken cancellation
95
95
MaxPDUSize = s7data [ 18 ] * 256 + s7data [ 19 ] ;
96
96
}
97
97
98
-
99
98
/// <summary>
100
99
/// Reads a number of bytes from a DB starting from a specified index. This handles more than 200 bytes with multiple requests.
101
100
/// If the read was not successful, check LastErrorCode or LastErrorString.
@@ -110,20 +109,12 @@ private async Task SetupConnection(Stream stream, CancellationToken cancellation
110
109
public async Task < byte [ ] > ReadBytesAsync ( DataType dataType , int db , int startByteAdr , int count , CancellationToken cancellationToken = default )
111
110
{
112
111
var resultBytes = new byte [ count ] ;
113
- int index = 0 ;
114
- while ( count > 0 )
115
- {
116
- //This works up to MaxPDUSize-1 on SNAP7. But not MaxPDUSize-0.
117
- var maxToRead = Math . Min ( count , MaxPDUSize - 18 ) ;
118
- await ReadBytesWithSingleRequestAsync ( dataType , db , startByteAdr + index , resultBytes , index , maxToRead , cancellationToken ) . ConfigureAwait ( false ) ;
119
- count -= maxToRead ;
120
- index += maxToRead ;
121
- }
112
+
113
+ await ReadBytesAsync ( resultBytes , dataType , db , startByteAdr , cancellationToken ) . ConfigureAwait ( false ) ;
114
+
122
115
return resultBytes ;
123
116
}
124
117
125
- #if NET5_0_OR_GREATER
126
-
127
118
/// <summary>
128
119
/// Reads a number of bytes from a DB starting from a specified index. This handles more than 200 bytes with multiple requests.
129
120
/// If the read was not successful, check LastErrorCode or LastErrorString.
@@ -148,8 +139,6 @@ public async Task ReadBytesAsync(Memory<byte> buffer, DataType dataType, int db,
148
139
}
149
140
}
150
141
151
- #endif
152
-
153
142
/// <summary>
154
143
/// Read and decode a certain number of bytes of the "VarType" provided.
155
144
/// This can be used to read multiple consecutive variables of the same type (Word, DWord, Int, etc).
@@ -323,7 +312,6 @@ public async Task<List<DataItem>> ReadMultipleVarsAsync(List<DataItem> dataItems
323
312
return dataItems ;
324
313
}
325
314
326
-
327
315
/// <summary>
328
316
/// Write a number of bytes from a DB starting from a specified index. This handles more than 200 bytes with multiple requests.
329
317
/// If the write was not successful, check LastErrorCode or LastErrorString.
@@ -335,21 +323,11 @@ public async Task<List<DataItem>> ReadMultipleVarsAsync(List<DataItem> dataItems
335
323
/// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is None.
336
324
/// Please note that cancellation is advisory/cooperative and will not lead to immediate cancellation in all cases.</param>
337
325
/// <returns>A task that represents the asynchronous write operation.</returns>
338
- public async Task WriteBytesAsync ( DataType dataType , int db , int startByteAdr , byte [ ] value , CancellationToken cancellationToken = default )
326
+ public Task WriteBytesAsync ( DataType dataType , int db , int startByteAdr , byte [ ] value , CancellationToken cancellationToken = default )
339
327
{
340
- int localIndex = 0 ;
341
- int count = value . Length ;
342
- while ( count > 0 )
343
- {
344
- var maxToWrite = ( int ) Math . Min ( count , MaxPDUSize - 35 ) ;
345
- await WriteBytesWithASingleRequestAsync ( dataType , db , startByteAdr + localIndex , value , localIndex , maxToWrite , cancellationToken ) . ConfigureAwait ( false ) ;
346
- count -= maxToWrite ;
347
- localIndex += maxToWrite ;
348
- }
328
+ return WriteBytesAsync ( dataType , db , startByteAdr , value . AsMemory ( ) , cancellationToken ) ;
349
329
}
350
330
351
- #if NET5_0_OR_GREATER
352
-
353
331
/// <summary>
354
332
/// Write a number of bytes from a DB starting from a specified index. This handles more than 200 bytes with multiple requests.
355
333
/// If the write was not successful, check LastErrorCode or LastErrorString.
@@ -373,8 +351,6 @@ public async Task WriteBytesAsync(DataType dataType, int db, int startByteAdr, R
373
351
}
374
352
}
375
353
376
- #endif
377
-
378
354
/// <summary>
379
355
/// Write a single bit from a DB with the specified index.
380
356
/// </summary>
@@ -496,18 +472,6 @@ public async Task WriteClassAsync(object classValue, int db, int startByteAdr =
496
472
await WriteBytesAsync ( DataType . DataBlock , db , startByteAdr , bytes , cancellationToken ) . ConfigureAwait ( false ) ;
497
473
}
498
474
499
- private async Task ReadBytesWithSingleRequestAsync ( DataType dataType , int db , int startByteAdr , byte [ ] buffer , int offset , int count , CancellationToken cancellationToken )
500
- {
501
- var dataToSend = BuildReadRequestPackage ( new [ ] { new DataItemAddress ( dataType , db , startByteAdr , count ) } ) ;
502
-
503
- var s7data = await RequestTsduAsync ( dataToSend , cancellationToken ) ;
504
- AssertReadResponse ( s7data , count ) ;
505
-
506
- Array . Copy ( s7data , 18 , buffer , offset , count ) ;
507
- }
508
-
509
- #if NET5_0_OR_GREATER
510
-
511
475
private async Task ReadBytesWithSingleRequestAsync ( DataType dataType , int db , int startByteAdr , Memory < byte > buffer , CancellationToken cancellationToken )
512
476
{
513
477
var dataToSend = BuildReadRequestPackage ( new [ ] { new DataItemAddress ( dataType , db , startByteAdr , buffer . Length ) } ) ;
@@ -518,8 +482,6 @@ private async Task ReadBytesWithSingleRequestAsync(DataType dataType, int db, in
518
482
s7data . AsSpan ( 18 , buffer . Length ) . CopyTo ( buffer . Span ) ;
519
483
}
520
484
521
- #endif
522
-
523
485
/// <summary>
524
486
/// Write DataItem(s) to the PLC. Throws an exception if the response is invalid
525
487
/// or when the PLC reports errors for item(s) written.
@@ -538,35 +500,6 @@ public async Task WriteAsync(params DataItem[] dataItems)
538
500
S7WriteMultiple . ParseResponse ( response , response . Length , dataItems ) ;
539
501
}
540
502
541
- /// <summary>
542
- /// Writes up to 200 bytes to the PLC. You must specify the memory area type, memory are address, byte start address and bytes count.
543
- /// </summary>
544
- /// <param name="dataType">Data type of the memory area, can be DB, Timer, Counter, Merker(Memory), Input, Output.</param>
545
- /// <param name="db">Address of the memory area (if you want to read DB1, this is set to 1). This must be set also for other memory area types: counters, timers,etc.</param>
546
- /// <param name="startByteAdr">Start byte address. If you want to read DB1.DBW200, this is 200.</param>
547
- /// <param name="value">Bytes to write. The lenght of this parameter can't be higher than 200. If you need more, use recursion.</param>
548
- /// <returns>A task that represents the asynchronous write operation.</returns>
549
- private async Task WriteBytesWithASingleRequestAsync ( DataType dataType , int db , int startByteAdr , byte [ ] value , int dataOffset , int count , CancellationToken cancellationToken )
550
- {
551
- try
552
- {
553
- var dataToSend = BuildWriteBytesPackage ( dataType , db , startByteAdr , value , dataOffset , count ) ;
554
- var s7data = await RequestTsduAsync ( dataToSend , cancellationToken ) . ConfigureAwait ( false ) ;
555
-
556
- ValidateResponseCode ( ( ReadWriteErrorCode ) s7data [ 14 ] ) ;
557
- }
558
- catch ( OperationCanceledException )
559
- {
560
- throw ;
561
- }
562
- catch ( Exception exc )
563
- {
564
- throw new PlcException ( ErrorCode . WriteData , exc ) ;
565
- }
566
- }
567
-
568
- #if NET5_0_OR_GREATER
569
-
570
503
/// <summary>
571
504
/// Writes up to 200 bytes to the PLC. You must specify the memory area type, memory are address, byte start address and bytes count.
572
505
/// </summary>
@@ -594,8 +527,6 @@ private async Task WriteBytesWithASingleRequestAsync(DataType dataType, int db,
594
527
}
595
528
}
596
529
597
- #endif
598
-
599
530
private async Task WriteBitWithASingleRequestAsync ( DataType dataType , int db , int startByteAdr , int bitAdr , bool bitValue , CancellationToken cancellationToken )
600
531
{
601
532
try
0 commit comments