-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updated libraries, changed Base to Core
- Loading branch information
Mark Lanning
authored and
Mark Lanning
committed
Dec 12, 2024
1 parent
f02865d
commit b23eeab
Showing
24 changed files
with
180 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
Base/src/ThingsLibrary.Base/DataType/Extensions/Checksum.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// ================================================================================ | ||
// <copyright file="Checksum.cs" company="Starlight Software Co"> | ||
// Copyright (c) Starlight Software Co. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. | ||
// </copyright> | ||
// ================================================================================ | ||
|
||
namespace ThingsLibrary.DataType.Extensions | ||
{ | ||
public static class ChecksumExtensions | ||
{ | ||
/// <summary> | ||
/// Append the checksum | ||
/// </summary> | ||
/// <param name="sentence"></param> | ||
/// <remarks>Checksum based on the NMEA0183 checksum which is just a simple byte-by-byte XOR of all the bytes between $ and * signs exclusively.</remarks> | ||
public static void AppendChecksum(this StringBuilder sentence) | ||
{ | ||
// not the beginning of a sentence | ||
if (sentence.Length == 0) { return; } | ||
if (sentence[0] != '$') { return; } | ||
|
||
//Start with first Item | ||
int checksum = Convert.ToByte(sentence[1]); | ||
|
||
// Loop through all chars to get a checksum | ||
int i; | ||
for (i = 2; i < sentence.Length; i++) | ||
{ | ||
if (sentence[i] == '*') { break; } | ||
|
||
// No. XOR the checksum with this character's value | ||
checksum ^= Convert.ToByte(sentence[i]); | ||
} | ||
|
||
// no astrisk to mark the CRC check | ||
if (i == sentence.Length) | ||
{ | ||
sentence.Append("*"); | ||
} | ||
|
||
// Return the checksum formatted as a two-character hexadecimal | ||
sentence.Append(checksum.ToString("X2")); | ||
} | ||
|
||
/// <summary> | ||
/// Calculate a checksum value based on the NMEA0183 style calculation | ||
/// </summary> | ||
/// <param name="sentence">Sentence</param> | ||
/// <remarks>Checksum based on the NMEA0183 checksum which is just a simple byte-by-byte XOR of all the bytes between $ and * signs exclusively.</remarks> | ||
/// <returns>Two character hexadecimal checksum value</returns> | ||
public static string ToChecksum(this string sentence) | ||
{ | ||
if (string.IsNullOrEmpty(sentence)) { return string.Empty; } | ||
|
||
// not the beginning of a sentence | ||
if (sentence[0] != '$') { return string.Empty; } | ||
|
||
//Start with first Item | ||
int checksum = Convert.ToByte(sentence[1]); | ||
|
||
// Loop through all chars to get a checksum | ||
for (int i = 2; i < sentence.Length; i++) | ||
{ | ||
if (sentence[i] == '*') { break; } | ||
|
||
// No. XOR the checksum with this character's value | ||
checksum ^= Convert.ToByte(sentence[i]); | ||
} | ||
|
||
// Return the checksum formatted as a two-character hexadecimal | ||
return checksum.ToString("X2"); | ||
} | ||
|
||
/// <summary> | ||
/// Validate that the checksum on the sentence is correct | ||
/// </summary> | ||
/// <param name="sentence"></param> | ||
/// <returns></returns> | ||
public static bool ValidateChecksum(this string sentence) | ||
{ | ||
var checksum = sentence.ToChecksum(); | ||
if (string.IsNullOrEmpty(checksum)) { return false; } | ||
|
||
return sentence.EndsWith(checksum); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 23 additions & 23 deletions
46
Database/tests/ThingsLibrary.Database.Tests/ThingsLibrary.Database.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,31 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net8.0;net9.0</TargetFrameworks> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<PropertyGroup> | ||
<TargetFrameworks>net8.0;net9.0</TargetFrameworks> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="coverlet.collector" Version="6.0.2"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" /> | ||
<PackageReference Include="MSTest.TestAdapter" Version="3.6.3" /> | ||
<PackageReference Include="MSTest.TestFramework" Version="3.6.3" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="coverlet.collector" Version="6.0.2"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" /> | ||
<PackageReference Include="MSTest.TestAdapter" Version="3.6.3" /> | ||
<PackageReference Include="MSTest.TestFramework" Version="3.6.3" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\Base\src\ThingsLibrary.Testing\ThingsLibrary.Testing.csproj" /> | ||
<ProjectReference Include="..\..\src\ThingsLibrary.Database\ThingsLibrary.Database.csproj" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\Base\src\ThingsLibrary.Testing\ThingsLibrary.Testing.csproj" /> | ||
<ProjectReference Include="..\..\src\ThingsLibrary.Database\ThingsLibrary.Database.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.