1- using System . Text ;
1+ using System ;
2+ using System . Text ;
3+ using System . Threading ;
24using System . Collections ;
5+ using System . Collections . Generic ;
6+ using System . Linq ;
37using System . IO . Ports ;
48
59namespace PL . Modbus
@@ -115,9 +119,9 @@ public virtual byte[] Command(byte functionCode, byte[] data)
115119 _stream . Write ( commandBuffer ) ;
116120
117121 string responseString = Encoding . ASCII . GetString ( _stream . ReadTo ( ( byte ) '\n ' ) ) ;
118- if ( responseString [ 0 ] != ':' || responseString [ ^ 2 .. ] != "\r \n " )
122+ if ( responseString [ 0 ] != ':' || responseString . Substring ( responseString . Length - 2 ) != "\r \n " )
119123 responseError = true ;
120- responseBuffer = Utility . AsciiStringToByteArray ( responseString [ 1 .. ^ 2 ] ) ;
124+ responseBuffer = Utility . AsciiStringToByteArray ( responseString . Substring ( 1 , responseString . Length - 3 ) ) ;
121125 break ;
122126
123127 case Protocol . Tcp :
@@ -151,7 +155,7 @@ public virtual byte[] Command(byte functionCode, byte[] data)
151155 _stream . ReadAvailableData ( ) ;
152156 if ( DelayAfterRead > 0 )
153157 Thread . Sleep ( DelayAfterRead ) ;
154- throw new ( "Modbus response error." ) ;
158+ throw new System . Exception ( "Modbus response error." ) ;
155159 }
156160
157161 if ( _protocol == Protocol . Rtu )
@@ -170,7 +174,7 @@ public virtual byte[] Command(byte functionCode, byte[] data)
170174 _stream . ReadAvailableData ( ) ;
171175 if ( DelayAfterRead > 0 )
172176 Thread . Sleep ( DelayAfterRead ) ;
173- throw new ( "Modbus CRC error." ) ;
177+ throw new System . Exception ( "Modbus CRC error." ) ;
174178 }
175179
176180 if ( DelayAfterRead > 0 )
@@ -182,11 +186,11 @@ public virtual byte[] Command(byte functionCode, byte[] data)
182186 switch ( _protocol )
183187 {
184188 case Protocol . Rtu :
185- return responseBuffer [ 2 .. ^ 2 ] ;
189+ return responseBuffer . Skip ( 2 ) . Take ( responseBuffer . Length - 4 ) . ToArray ( ) ;
186190 case Protocol . Ascii :
187- return responseBuffer [ 2 .. ^ 1 ] ;
191+ return responseBuffer . Skip ( 2 ) . Take ( responseBuffer . Length - 3 ) . ToArray ( ) ;
188192 default :
189- return responseBuffer [ 2 .. ] ;
193+ return responseBuffer . Skip ( 2 ) . ToArray ( ) ;
190194 }
191195 }
192196 }
@@ -384,12 +388,12 @@ protected virtual byte[] ReadRtuData(byte functionCode)
384388 _stream . ReadAvailableData ( ) ;
385389 if ( DelayAfterRead > 0 )
386390 Thread . Sleep ( DelayAfterRead ) ;
387- throw new ( "Unknown Modbus function code." ) ;
391+ throw new System . Exception ( "Unknown Modbus function code." ) ;
388392 }
389393
390394 private List < bool > ReadBits ( byte functionCode , ushort address , ushort numberOfBits )
391395 {
392- List < bool > bits = new ( ) ;
396+ List < bool > bits = new List < bool > ( ) ;
393397 foreach ( var addressRange in Utility . SplitAddressRange ( address , numberOfBits , 2000 ) )
394398 {
395399 byte [ ] commandData = new byte [ 4 ] ;
@@ -398,7 +402,7 @@ private List<bool> ReadBits(byte functionCode, ushort address, ushort numberOfBi
398402 BitConverter . GetBytes ( addressRange . NumberOfItems ) . CopyTo ( commandData , 2 ) ;
399403 Array . Reverse ( commandData , 2 , 2 ) ;
400404
401- BitArray responseData = new ( Command ( functionCode , commandData ) [ 1 .. ] ) ;
405+ BitArray responseData = new BitArray ( Command ( functionCode , commandData ) . Skip ( 1 ) . ToArray ( ) ) ;
402406
403407 int maxIndex = Math . Min ( addressRange . NumberOfItems , responseData . Length ) ;
404408 for ( int j = 0 ; j < maxIndex ; j ++ )
@@ -409,7 +413,7 @@ private List<bool> ReadBits(byte functionCode, ushort address, ushort numberOfBi
409413
410414 private List < ushort > ReadRegisters ( byte functionCode , ushort address , ushort numberOfRegisters )
411415 {
412- List < ushort > registers = new ( ) ;
416+ List < ushort > registers = new List < ushort > ( ) ;
413417 foreach ( var addressRange in Utility . SplitAddressRange ( address , numberOfRegisters , 125 ) )
414418 {
415419 byte [ ] commandData = new byte [ 4 ] ;
@@ -418,7 +422,7 @@ private List<ushort> ReadRegisters(byte functionCode, ushort address, ushort num
418422 BitConverter . GetBytes ( addressRange . NumberOfItems ) . CopyTo ( commandData , 2 ) ;
419423 Array . Reverse ( commandData , 2 , 2 ) ;
420424
421- byte [ ] responseData = Command ( functionCode , commandData ) [ 1 .. ] ;
425+ byte [ ] responseData = Command ( functionCode , commandData ) . Skip ( 1 ) . ToArray ( ) ;
422426
423427 int maxIndex = Math . Min ( addressRange . NumberOfItems * 2 , responseData . Length / 2 * 2 ) ;
424428 for ( int j = 0 ; j < maxIndex ; j += 2 )
0 commit comments