Recent

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

avk

  • Hero Member
  • *****
  • Posts: 825
Re: CRC8/Maxim function?
« Reply #15 on: July 06, 2021, 11:20:51 am »
In my previous post, I tried to draw your attention to the fact that all the functions mentioned above expect a sequence of bytes as input, and not their hexadecimal codes.
And by the way, have you tried passing the test string '123456789' to your instance of the CRC function? What result does it return?

MarkMLl

  • Hero Member
  • *****
  • Posts: 8525
Re: CRC8/Maxim function?
« Reply #16 on: July 06, 2021, 11:46:50 am »
There's also a possibility that, since it's apparently not FPC, the compiler is making different size assumptions etc. e.g. that cardinals are 8-bit unsigned.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

JCDes

  • New Member
  • *
  • Posts: 23
Re: CRC8/Maxim function?
« Reply #17 on: July 06, 2021, 06:17:40 pm »
It is calculating as if the data was a string not bytes. "0000000500000000000000000000" calculates to "03" which is perfect for string input.
How can I get it to interpret the input as bytes $00 $00 $00 $05 $00 $00 $00 $00 $00 $00 $00 $00 $00 $00 to get "AA" result??

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: CRC8/Maxim function?
« Reply #18 on: July 06, 2021, 07:05:10 pm »
A quick way would be to add a first step to convert the input string to another one with each pair of digits from the original converted to the corresponding character, e.g. with something like this (untested) function:
Code: Pascal  [Select][+][-]
  1. function Recode(const Origin: String): String;
  2. var
  3.   sPos: Integer = 1;
  4. begin
  5.   Result := '';
  6.   while sPos < Length(Origin) do begin
  7.     Result := Result + Chr(StrToInt('$'+ Original.SubString(sPos,2)));
  8.     Inc(sPos, 2)
  9.   end;
  10. end;
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

JCDes

  • New Member
  • *
  • Posts: 23
Re: CRC8/Maxim function?
« Reply #19 on: July 06, 2021, 08:23:49 pm »
Error: Object or record required to access method "Substring". on this line: Result := Result + Chr(StrToInt('$'+ Origin.SubString(sPos,2)));

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: CRC8/Maxim function?
« Reply #20 on: July 06, 2021, 09:24:44 pm »
Error: Object or record required to access method "Substring". on this line: Result := Result + Chr(StrToInt('$'+ Origin.SubString(sPos,2)));

Do you have SysUtils in your uses clause? SubString() is a method of TStringHelper, the string type helper defined there.

If for some reason you don't want to use it you can use the standard Copy() function; it's basically the same:
Code: Pascal  [Select][+][-]
  1. Result := Result + Chr(StrToInt('$'+ Copy(Origin, sPos, 2)));
« Last Edit: July 06, 2021, 09:27:47 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

JCDes

  • New Member
  • *
  • Posts: 23
Re: CRC8/Maxim function?
« Reply #21 on: July 06, 2021, 11:56:31 pm »
Expected 1 parameter error on line: Inc(sPos, 2)

JCDes

  • New Member
  • *
  • Posts: 23
Re: CRC8/Maxim function?
« Reply #22 on: July 07, 2021, 12:15:41 am »
This is the code that is calculating the data as a string correctly:
Code: [Select]
function Crc8Maxim(Buffer: string):Cardinal;
var
  CrcTable: array of Cardinal;
  I: LongInt;
begin
  CrcTable := [
    $00, $5e, $bc, $e2, $61, $3f, $dd, $83, $c2, $9c, $7e, $20, $a3, $fd, $1f, $41,
    $9d, $c3, $21, $7f, $fc, $a2, $40, $1e, $5f, $01, $e3, $bd, $3e, $60, $82, $dc,
    $23, $7d, $9f, $c1, $42, $1c, $fe, $a0, $e1, $bf, $5d, $03, $80, $de, $3c, $62,
    $be, $e0, $02, $5c, $df, $81, $63, $3d, $7c, $22, $c0, $9e, $1d, $43, $a1, $ff,
    $46, $18, $fa, $a4, $27, $79, $9b, $c5, $84, $da, $38, $66, $e5, $bb, $59, $07,
    $db, $85, $67, $39, $ba, $e4, $06, $58, $19, $47, $a5, $fb, $78, $26, $c4, $9a,
    $65, $3b, $d9, $87, $04, $5a, $b8, $e6, $a7, $f9, $1b, $45, $c6, $98, $7a, $24,
    $f8, $a6, $44, $1a, $99, $c7, $25, $7b, $3a, $64, $86, $d8, $5b, $05, $e7, $b9,
    $8c, $d2, $30, $6e, $ed, $b3, $51, $0f, $4e, $10, $f2, $ac, $2f, $71, $93, $cd,
    $11, $4f, $ad, $f3, $70, $2e, $cc, $92, $d3, $8d, $6f, $31, $b2, $ec, $0e, $50,
    $af, $f1, $13, $4d, $ce, $90, $72, $2c, $6d, $33, $d1, $8f, $0c, $52, $b0, $ee,
    $32, $6c, $8e, $d0, $53, $0d, $ef, $b1, $f0, $ae, $4c, $12, $91, $cf, $2d, $73,
    $ca, $94, $76, $28, $ab, $f5, $17, $49, $08, $56, $b4, $ea, $69, $37, $d5, $8b,
    $57, $09, $eb, $b5, $36, $68, $8a, $d4, $95, $cb, $29, $77, $f4, $aa, $48, $16,
    $e9, $b7, $55, $0b, $88, $d6, $34, $6a, $2b, $75, $97, $c9, $4a, $14, $f6, $a8,
    $74, $2a, $c8, $96, $15, $4b, $a9, $f7, $b6, $e8, $0a, $54, $d7, $89, $6b, $35];

  Result := 0;
  for I := 1 to Length(Buffer) do
    Result := CrcTable[Result xor Ord(Buffer[I])];
end;

This is how I'm reading the data from the hex editor:
Code: [Select]
CS := IntToHex(GetByteHexEdit( $00),2) + IntToHex(GetByteHexEdit( $01),2) + IntToHex(GetByteHexEdit( $02),2)
+ IntToHex(GetByteHexEdit( $03),2) + IntToHex(GetByteHexEdit( $04),2) + IntToHex(GetByteHexEdit( $05),2)
+ IntToHex(GetByteHexEdit( $06),2) + IntToHex(GetByteHexEdit( $07),2) + IntToHex(GetByteHexEdit( $08),2)
+ IntToHex(GetByteHexEdit( $09),2) + IntToHex(GetByteHexEdit( $0A),2) + IntToHex(GetByteHexEdit( $0B),2)
+ IntToHex(GetByteHexEdit( $0C),2) + IntToHex(GetByteHexEdit( $0D),2);

Ir reads the data I need and gets processed but it gets processed as a string not as bytes. If I go to http://www.sunshine2k.de/coding/javascript/crc/crc_js.html and set to crc-maxim in the predifined section and select "string" I get the same result but the result I need is the one for the "bytes" setting.

"0000000500000000000000000000"
I get "03" but I should be getting "AA"

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: CRC8/Maxim function?
« Reply #23 on: July 07, 2021, 12:36:42 am »
Expected 1 parameter error on line: Inc(sPos, 2)

A wild guess: Some other unit in your uses is hijacking the one declared in System. Replace with:
Code: Pascal  [Select][+][-]
  1. System.Inc(sPos, 2)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

JCDes

  • New Member
  • *
  • Posts: 23
Re: CRC8/Maxim function?
« Reply #24 on: July 07, 2021, 12:55:15 am »
Error, it took "system" as an undeclared variable. This tms scripter is killing me. I really appreciate you guys  helping me even if I'm not using fpc. I'm using tms scripter and it seems like the pascal syntax is limited. This is all the documentation that comes with it. https://doc.tmssoftware.com/biz/scripter/scripter-user-guide.pdf
« Last Edit: July 07, 2021, 01:05:16 am by JCDes »

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: CRC8/Maxim function?
« Reply #25 on: July 07, 2021, 03:45:24 am »
This tms scripter is killing me.

Oops! Is it an script? No wonder it barked at the type helper, then! Sorry, didn't read enough of the thread ... :-[

If all else is working then try replacing the single Inc(sPos, 2) with either:
Code: Pascal  [Select][+][-]
  1. Inc(sPos);
  2. Inc(sPos);
or with:
Code: Pascal  [Select][+][-]
  1. sPos := sPos + 2;
Either of them should work, according to the documentation and the error message. :-\

ETA: I don't want to sound snarky but ... rather than copy-pasting you should try to understand what our code is trying to do so you can replace it with the appropiate one for TMS Script. In this particular case it should have been clear that it's increasing sPos by 2, so the solution(s) should have been obvious to you ... Just saying :-X
« Last Edit: July 07, 2021, 03:52:22 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

avk

  • Hero Member
  • *****
  • Posts: 825
Re: CRC8/Maxim function?
« Reply #26 on: July 07, 2021, 06:33:44 am »
Hmm, what if something like that?
Code: Pascal  [Select][+][-]
  1.  CS := Char(GetByteHexEdit( 0)) + Char(GetByteHexEdit( 1)) + Char(GetByteHexEdit( 2)) +
  2.        Char(GetByteHexEdit( 3)) + Char(GetByteHexEdit( 4)) + Char(GetByteHexEdit( 5)) +
  3.        Char(GetByteHexEdit( 6)) + Char(GetByteHexEdit( 7)) + Char(GetByteHexEdit( 8)) +
  4.        Char(GetByteHexEdit( 9)) + Char(GetByteHexEdit(10)) + Char(GetByteHexEdit(11)) +
  5.        Char(GetByteHexEdit(12)) + Char(GetByteHexEdit(13));
  6.  
That is, you can try to get a byte sequence at once and do without further conversions.
« Last Edit: July 07, 2021, 07:00:14 am by avk »

Thaddy

  • Hero Member
  • *****
  • Posts: 18711
  • To Europe: simply sell USA bonds: dollar collapses
Re: CRC8/Maxim function?
« Reply #27 on: July 07, 2021, 10:03:14 am »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

JCDes

  • New Member
  • *
  • Posts: 23
Re: CRC8/Maxim function?
« Reply #28 on: July 07, 2021, 09:08:49 pm »
I really appreciate your help. The problem is that this TMS Scripter that I'm using errors out on:

 Const CrcTable:Array[0..255] of Cardinal = <------- Syntax error
inc(I,2) <----- This one says expected 1 parameter only

it allows

var  CRCTable:array;
begin
crctable := [the table here];


MarkMLl

  • Hero Member
  • *****
  • Posts: 8525
Re: CRC8/Maxim function?
« Reply #29 on: July 08, 2021, 10:09:16 am »
I really appreciate your help. The problem is that this TMS Scripter that I'm using errors out on:

 Const CrcTable:Array[0..255] of Cardinal = <------- Syntax error
inc(I,2) <----- This one says expected 1 parameter only

it allows

var  CRCTable:array;
begin
crctable := [the table here];

Isn't that table generated by a function you gave in your first posting? Use that to generate the CRC table as a variable (which will be standard 1970s-style Pascal) rather than using language extensions to predefine it as a constant.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018