@@ -17,20 +17,20 @@ namespace DSMRParser
1717 /// </remarks>
1818 public class DSMRTelegramParser : IDSMRTelegramParser
1919 {
20- private readonly ICRCCalculator _crc ;
20+ private readonly ICRCVerifier _crc ;
2121 private const bool DEFAULTIGNORECRC = false ;
2222
2323 /// <summary>
24- /// Initializes a new instance of a <see cref="DSMRTelegramParser"/> with a default <see cref="ICRCCalculator "/>.
24+ /// Initializes a new instance of a <see cref="DSMRTelegramParser"/> with a default <see cref="ICRCVerifier "/>.
2525 /// </summary>
2626 public DSMRTelegramParser ( )
27- : this ( ICRCCalculator . Default ) { }
27+ : this ( ICRCVerifier . Default ) { }
2828
2929 /// <summary>
30- /// Initializes a new instance of a <see cref="DSMRTelegramParser"/> with a given <see cref="ICRCCalculator "/>.
30+ /// Initializes a new instance of a <see cref="DSMRTelegramParser"/> with a given <see cref="ICRCVerifier "/>.
3131 /// </summary>
32- /// <exception cref="ArgumentNullException"/>Thrown when the <param name="crcCalculator "/> is null.
33- public DSMRTelegramParser ( ICRCCalculator crcCalculator ) => _crc = crcCalculator ?? throw new ArgumentNullException ( nameof ( crcCalculator ) ) ;
32+ /// <exception cref="ArgumentNullException"/>Thrown when the <param name="crcVerifier "/> is null.
33+ public DSMRTelegramParser ( ICRCVerifier crcVerifier ) => _crc = crcVerifier ?? throw new ArgumentNullException ( nameof ( crcVerifier ) ) ;
3434
3535 /// <summary>
3636 /// Parses a DSMR telegram in raw byte form into a <see cref="Telegram"/>.
@@ -140,24 +140,25 @@ public Telegram Parse(Span<byte> telegram, bool ignoreCrc = DEFAULTIGNORECRC)
140140 if ( telegram [ 0 ] == ( byte ) '/' )
141141 {
142142 // Get individual lines
143- var lines = Encoding . ASCII . GetString ( telegram ) . Split ( "\r \n " ) ;
143+ var lines = Encoding . ASCII . GetString ( telegram ) . Split ( "\r \n " , StringSplitOptions . RemoveEmptyEntries ) ;
144+
145+ // Do we have a CRC, then check it!
146+ if ( ! ignoreCrc && lines [ ^ 1 ] [ 0 ] == '!' && lines [ ^ 1 ] . Length > 1 )
147+ {
148+ var crcex = _crc . Verify ( telegram ) ;
149+ if ( crcex != null ) // CRC failed?
150+ return crcex ; // Return exception from CRCVerifier
151+ }
144152
145153 result = new Telegram (
146154 lines [ 0 ] . TrimStart ( '/' ) , // Get identification (part after the "/" of the first line)
147- lines . Skip ( 2 ) // Skip identification and mandatory empty line
155+ lines . Skip ( 1 ) // Skip identification ( mandatory empty line has already been removed by Split method)
148156 . Where ( l => ! string . IsNullOrEmpty ( l ) && char . IsDigit ( l [ 0 ] ) ) // Only process lines starting with a digit
149157 . Select ( l => ( // Parse values from telegram data
150158 OBISId . FromString ( l . Substring ( 0 , Math . Max ( 0 , l . IndexOf ( "(" , StringComparison . Ordinal ) ) ) ) , GetValues ( l )
151159 ) )
152160 ) ;
153161
154- if ( ! ignoreCrc && result . DSMRVersion >= 40 )
155- {
156- var crcex = _crc . Verify ( telegram ) ;
157- if ( crcex != null ) // CRC failed?
158- return crcex ; // Return exception froom CRCCalculator
159- }
160-
161162 return null ; // No exceptions, all went well!
162163 }
163164 // If we reache this point it must be an invalid format.
0 commit comments