mirror of
https://github.com/UtilitechAS/amsreader-firmware.git
synced 2026-01-27 12:43:08 +00:00
Added code for checksum calculation
This commit is contained in:
56
Code/HanDebugger/HanDebugger/Crc16.cs
Normal file
56
Code/HanDebugger/HanDebugger/Crc16.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace HanDebugger
|
||||
{
|
||||
public static class Crc16
|
||||
{
|
||||
private const ushort polynomial = 0x8408;
|
||||
private static ushort[] table = new ushort[256];
|
||||
|
||||
static Crc16()
|
||||
{
|
||||
ushort value;
|
||||
ushort temp;
|
||||
for (ushort i = 0; i < table.Length; ++i)
|
||||
{
|
||||
value = 0;
|
||||
temp = i;
|
||||
for (byte j = 0; j < 8; ++j)
|
||||
{
|
||||
if (((value ^ temp) & 0x0001) != 0)
|
||||
{
|
||||
value = (ushort)((value >> 1) ^ polynomial);
|
||||
}
|
||||
else
|
||||
{
|
||||
value >>= 1;
|
||||
}
|
||||
temp >>= 1;
|
||||
}
|
||||
table[i] = value;
|
||||
}
|
||||
}
|
||||
|
||||
public static ushort ComputeChecksum(byte[] data)
|
||||
{
|
||||
return ComputeChecksum(data, 0, data.Length);
|
||||
}
|
||||
|
||||
public static ushort ComputeChecksum(byte[] data, int start, int length)
|
||||
{
|
||||
ushort fcs = 0xffff;
|
||||
for (int i = start; i < (start + length); i++)
|
||||
{
|
||||
var index = (fcs ^ data[i]) & 0xff;
|
||||
fcs = (ushort)((fcs >> 8) ^ table[index]);
|
||||
}
|
||||
fcs ^= 0xffff;
|
||||
return fcs;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user