Skip to content

Commit 4053c15

Browse files
committed
CRC's are now calculated whenever a CRC is found. Renamed solution file. Renamed CRCCalculator to CRCVerifier to better match what it does.
1 parent 238acbb commit 4053c15

7 files changed

Lines changed: 27 additions & 34 deletions

File tree

File renamed without changes.

DSMRParser.Tests/ExtendedTelegramParserTests.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,5 @@ public void DSMRTelegramParser_Reads_Telegram()
3333
Assert.AreEqual(1, result.Values.Count);
3434
Assert.AreEqual("ThisLineIsNotIgnored", result.GetByObisID("0-1:2.3"));
3535
}
36-
37-
[TestMethod]
38-
public void DSMRTelegramParser_Ignores_CRC_For_Unknown_Versions()
39-
{
40-
var target = new DSMRTelegramParser();
41-
var result = target.Parse("/Foo\r\n\r\n0-1:2.3(Test)\r\n!ABCD");
42-
Assert.AreEqual("Test", result.GetByObisID("0-1:2.3"));
43-
}
4436
}
4537
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ namespace DSMRParser.CRCHandling
88
/// Implements a CRC-16-IBM CRC.
99
/// </summary>
1010
/// <remarks>See https://en.wikipedia.org/wiki/Cyclic_redundancy_check</remarks>
11-
public class CRC16Calculator : ICRCCalculator
11+
public class CRC16Verifier : ICRCVerifier
1212
{
1313
/// <summary>
1414
/// Internal lookup table
1515
/// </summary>
1616
private readonly ushort[] _lookup = new ushort[256];
1717

1818
/// <summary>
19-
/// Initializes a new instance of a <see cref="CRC16Calculator" /> with the given polynomial (defaults to a reversed polynomial 0XA001).
19+
/// Initializes a new instance of a <see cref="CRC16Verifier" /> with the given polynomial (defaults to a reversed polynomial 0XA001).
2020
/// </summary>
2121
/// <param name="polynomial">The polynomial this </param>
22-
public CRC16Calculator(ushort polynomial = 0xA001)
22+
public CRC16Verifier(ushort polynomial = 0xA001)
2323
{
2424
ushort value;
2525
ushort temp;
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace DSMRParser.CRCHandling
55
/// <summary>
66
/// Provides the base interface for CRC calculation
77
/// </summary>
8-
public interface ICRCCalculator
8+
public interface ICRCVerifier
99
{
1010
/// <summary>
1111
/// Extracts the CRC of the given Telegram data and verifies it against the calculated CRC.
@@ -22,8 +22,8 @@ public interface ICRCCalculator
2222
ushort CalculateCRC(Span<byte> rawTelegram);
2323

2424
/// <summary>
25-
/// Returns the default CRC calculator.
25+
/// Returns the default CRC verifier.
2626
/// </summary>
27-
static ICRCCalculator Default => new CRC16Calculator();
27+
static ICRCVerifier Default => new CRC16Verifier();
2828
}
2929
}

DSMRParser/DSMRParser.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<RepositoryUrl>https://github.com/RobThree/DSMR.Net</RepositoryUrl>
2020
<RepositoryType>git</RepositoryType>
2121
<PackageProjectUrl>https://github.com/RobThree/DSMR.Net</PackageProjectUrl>
22-
<Version>1.0.1</Version>
22+
<Version>1.0.2</Version>
2323
</PropertyGroup>
2424

2525
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='ReleaseWithDocumentation|AnyCPU'">

DSMRParser/DSMRTelegramParser.cs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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.

DSMRParser/Models/Telegram.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -477,15 +477,15 @@ protected static IEnumerable<byte> DecodeHexString(string? value)
477477
/// <summary>
478478
/// Gets the string representation of this <see cref="Telegram"/>.
479479
/// </summary>
480-
/// <param name="crcCalculator">
481-
/// The <see cref="ICRCCalculator"/> to use when determining the CRC for the telegram. Defaults to <see cref="ICRCCalculator.Default"/>.
480+
/// <param name="crcVerifier">
481+
/// The <see cref="ICRCVerifier"/> to use when determining the CRC for the telegram. Defaults to <see cref="ICRCVerifier.Default"/>.
482482
/// </param>
483483
/// <returns>The string representation of this <see cref="Telegram"/>.</returns>
484-
public string AsString(ICRCCalculator? crcCalculator = null)
484+
public string AsString(ICRCVerifier? crcVerifier = null)
485485
{
486486
var values = string.Join("\r\n", Values.Select(kv => $"{kv.Key}({string.Join(")(", kv.Value)})"));
487487
var telegram = $"/{Identification}\r\n\r\n{values}\r\n!";
488-
var crc = (crcCalculator ?? ICRCCalculator.Default).CalculateCRC(Encoding.ASCII.GetBytes(telegram));
488+
var crc = (crcVerifier ?? ICRCVerifier.Default).CalculateCRC(Encoding.ASCII.GetBytes(telegram));
489489
return $"{telegram}{crc:X4}\r\n";
490490
}
491491
}

0 commit comments

Comments
 (0)