Recent

Author Topic: CRC8/Maxim function?  (Read 26241 times)

avk

  • Hero Member
  • *****
  • Posts: 825
Re: CRC8/Maxim function?
« Reply #30 on: July 08, 2021, 02:43:07 pm »
Somehow I suddenly remembered that Lazarus has a set of Pascal Script components, probably having a common origin with TMS Scripter.
The results of running this test script
Code: Pascal  [Select][+][-]
  1. program CrcTest;
  2.  
  3. function ReflectLoByte(aValue: Cardinal): Cardinal;
  4. begin
  5.   aValue := (aValue and $f0) shr 4 or (aValue and $0f) shl 4;
  6.   aValue := (aValue and $cc) shr 2 or (aValue and $33) shl 2;
  7.   Result := (aValue and $aa) shr 1 or (aValue and $55) shl 1;
  8. end;
  9.  
  10. function Crc8Maxim(const aData: string): Cardinal;
  11. var
  12.   I, J: LongInt;
  13. begin
  14.   Result := 0;
  15.   for I := 1 to Length(aData) do
  16.     begin
  17.       Result := Result xor ReflectLoByte(Ord(aData[I]));
  18.       for J := 1 to 8 do
  19.         if Result and $80 <> 0 then
  20.           Result := Result shl 1 xor $31
  21.         else
  22.           Result := Result shl 1;
  23.     end;
  24.   Result := ReflectLoByte(Result) and $ff;
  25. end;
  26.  
  27. function Crc8Maxim_2(const aBuffer: string): Cardinal;
  28. var
  29.   CrcTable: array of Cardinal;
  30.   I: LongInt;
  31. begin
  32.   CrcTable := [
  33.     $00, $5e, $bc, $e2, $61, $3f, $dd, $83, $c2, $9c, $7e, $20, $a3, $fd, $1f, $41,
  34.     $9d, $c3, $21, $7f, $fc, $a2, $40, $1e, $5f, $01, $e3, $bd, $3e, $60, $82, $dc,
  35.     $23, $7d, $9f, $c1, $42, $1c, $fe, $a0, $e1, $bf, $5d, $03, $80, $de, $3c, $62,
  36.     $be, $e0, $02, $5c, $df, $81, $63, $3d, $7c, $22, $c0, $9e, $1d, $43, $a1, $ff,
  37.     $46, $18, $fa, $a4, $27, $79, $9b, $c5, $84, $da, $38, $66, $e5, $bb, $59, $07,
  38.     $db, $85, $67, $39, $ba, $e4, $06, $58, $19, $47, $a5, $fb, $78, $26, $c4, $9a,
  39.     $65, $3b, $d9, $87, $04, $5a, $b8, $e6, $a7, $f9, $1b, $45, $c6, $98, $7a, $24,
  40.     $f8, $a6, $44, $1a, $99, $c7, $25, $7b, $3a, $64, $86, $d8, $5b, $05, $e7, $b9,
  41.     $8c, $d2, $30, $6e, $ed, $b3, $51, $0f, $4e, $10, $f2, $ac, $2f, $71, $93, $cd,
  42.     $11, $4f, $ad, $f3, $70, $2e, $cc, $92, $d3, $8d, $6f, $31, $b2, $ec, $0e, $50,
  43.     $af, $f1, $13, $4d, $ce, $90, $72, $2c, $6d, $33, $d1, $8f, $0c, $52, $b0, $ee,
  44.     $32, $6c, $8e, $d0, $53, $0d, $ef, $b1, $f0, $ae, $4c, $12, $91, $cf, $2d, $73,
  45.     $ca, $94, $76, $28, $ab, $f5, $17, $49, $08, $56, $b4, $ea, $69, $37, $d5, $8b,
  46.     $57, $09, $eb, $b5, $36, $68, $8a, $d4, $95, $cb, $29, $77, $f4, $aa, $48, $16,
  47.     $e9, $b7, $55, $0b, $88, $d6, $34, $6a, $2b, $75, $97, $c9, $4a, $14, $f6, $a8,
  48.     $74, $2a, $c8, $96, $15, $4b, $a9, $f7, $b6, $e8, $0a, $54, $d7, $89, $6b, $35];
  49.  
  50.   Result := 0;
  51.   for I := 1 to Length(aBuffer) do
  52.     Result := CrcTable[Result xor Ord(aBuffer[I])];
  53. end;
  54.  
  55. function HexDecode(const aOrigin: string): string;
  56. var
  57.   I, CurPos: LongInt;
  58. begin
  59.   CurPos := 1;
  60.   SetLength(Result, Length(aOrigin) div 2 + Length(aOrigin) and 1);
  61.   for I := 1 to Length(Result) do
  62.     begin
  63.       Result[I] := Chr(StrToInt('$' + Copy(aOrigin, CurPos, 2)));
  64.       CurPos := CurPos + 2;
  65.     end;
  66. end;
  67.  
  68. function GetByteHexEdit(aIndex: LongInt): Byte;
  69. begin
  70.   if aIndex = 3 then
  71.     Result := 5
  72.   else
  73.     Result := 0;
  74. end;
  75.  
  76. function GetByteSequence: string;
  77. var
  78.   I: LongInt;
  79. begin
  80.   SetLength(Result, 14);
  81.   for I := 1 to Length(Result) do
  82.     Result[I] := Chr(GetByteHexEdit(I - 1));
  83. end;
  84.  
  85. var
  86.   s: string;
  87.   c: Cardinal;
  88. begin
  89.   WriteStr('Crc8Maxim results:');
  90.   WriteStr('self test:');
  91.   s := '123456789';
  92.   c := Crc8Maxim(s);
  93.   WriteCardAsHex(c);
  94.   WriteStr('hexadecimal string:');
  95.   s := '0000000500000000000000000000';
  96.   c := Crc8Maxim(HexDecode(s));
  97.   WriteCardAsHex(c);
  98.   WriteStr('byte sequence:');
  99.   s := GetByteSequence;
  100.   c := Crc8Maxim(s);
  101.   WriteCardAsHex(c);
  102.   WriteStr('');
  103.  
  104.   WriteStr('Crc8Maxim_2 results:');
  105.   WriteStr('self test:');
  106.   s := '123456789';
  107.   c := Crc8Maxim_2(s);
  108.   WriteCardAsHex(c);
  109.   WriteStr('hexadecimal string:');
  110.   s := '0000000500000000000000000000';
  111.   c := Crc8Maxim_2(HexDecode(s));
  112.   WriteCardAsHex(c);
  113.   WriteStr('byte sequence:');
  114.   s := GetByteSequence;
  115.   c := Crc8Maxim_2(s);
  116.   WriteCardAsHex(c);
  117. end.
  118.  

in TPSScript:
Code: Text  [Select][+][-]
  1. Crc8Maxim results:
  2. self test:
  3. A1
  4. hexadecimal string:
  5. AA
  6. byte sequence:
  7. AA
  8.  
  9. Crc8Maxim_2 results:
  10. self test:
  11. A1
  12. hexadecimal string:
  13. AA
  14. byte sequence:
  15. AA
  16.  
WriteStr and WriteCardAsHex are helper user-defined functions for outputting results to TMemo.

JCDes

  • New Member
  • *
  • Posts: 23
Re: CRC8/Maxim function?
« Reply #31 on: July 08, 2021, 07:11:02 pm »
I get error on HexDecode: Unknown method or routine "SetLength".  Length works but setlength does not seem to be part of the syntax supported.

"function Crc8Maxim_2(const aBuffer: string): Cardinal;" errors out unless I remove "const" so it looks like:
" function Crc8Maxim_2(aBuffer: string): Cardinal;" .

JCDes

  • New Member
  • *
  • Posts: 23
Re: CRC8/Maxim function?
« Reply #32 on: July 08, 2021, 07:35:51 pm »
It's working!
« Last Edit: July 12, 2021, 10:34:11 pm by JCDes »

avk

  • Hero Member
  • *****
  • Posts: 825
Re: CRC8/Maxim function?
« Reply #33 on: July 08, 2021, 07:39:57 pm »
Hmm, TMS Scripter lags behind Pascal Script in functionality?
Anyway, I'm glad that you finally succeed.

JCDes

  • New Member
  • *
  • Posts: 23
Re: CRC8/Maxim function?
« Reply #34 on: July 08, 2021, 08:10:06 pm »
Actually it was all of you and your great effort to help me. All I did was play with the syntax that you already had given me. Once again thank you all and specially you avk.

avk

  • Hero Member
  • *****
  • Posts: 825
Re: CRC8/Maxim function?
« Reply #35 on: July 09, 2021, 06:07:28 pm »
You are welcome.

 

TinyPortal © 2005-2018