I also want to participate in this great topic, below 3 functions 100/100 working for the calculation of the CRC16
1.CRC16_KERMIT
2.CRC16CCITT_False
3.crc16_mcrf4xx
function CRC16_KERMIT(cadena : string):word;
var
valuehex : word;
i: integer;
CRC : word;
Begin
CRC := 0;
While i< length(cadena) do
begin
valuehex := ((strtoint('$'+cadena[i+1]+cadena[i+2]) XOR CRC) AND $0F) * $1081;
CRC := CRC SHR 4;
CRC := CRC XOR valuehex;
valuehex := (((strtoint('$'+cadena[i+1]+cadena[i+2]) SHR 4) XOR LO(CRC)) AND $0F);
CRC := CRC SHR 4;
CRC := CRC XOR (valuehex * $1081);
i:=i+2;
end;
CRC16_KERMIT:=(LO(CRC) SHL 8) OR HI(CRC);
end;
************************************************** **********
function crc16CCITT_False(Buffer:String):Cardinal;
Const polynom=$1021;
var
i,j: Integer;
Initial:word;
begin
Initial:=$ffff;
Result:=Initial;
i:=0;
While i< Length(Buffer) do begin
Result:=Result xor (strtoint('$'+buffer[i+1]+buffer[i+2]) shl 8);
for j:=0 to 7 do begin
if (Result and $8000)<>0 then Result:=(Result shl 1) xor Polynom
else Result:=Result shl 1;
end;
i:=i+2;
end;
Result:=Result and $ffff ;
end;
************************************************** ******
function crc16_mcrf4xx( data: string): word;
var
i,j: integer;
crc:word;
len:int64;
begin
crc:=$FFFF;
len:=length(data);
i:=0;
if len <= 0 then exit
else
begin
while i<len do begin
crc := crc xor strtoint('$'+data[i+1]+data[i+2]);
for j:=0 to 7 do
begin
if odd(crc) then
crc:=(crc shr 1) xor $8408
else
crc:=(crc shr 1);
end;
i:=i+2;
end;
end;
crc16_mcrf4xx:=crc;
end;
********************************
for the use of these functions:
crc16CCITT_False(Data); // data: must be in Hex
crc16_mcrf4xx(Data); // data: must be in Hex
CRC16_KERMIT(Data); // data: must be in Hex
You can check the results using the site https://crccalc.com/
these types of crc16 are commonly used, example bsi valeo and continental 95256, JC and Delphi 25128
Bookmarks