Am 14.07.15 um 12:37 schrieb
david.s...@teamsimoco.com:
> Hi. Hoping for a little advise with the CRC16 package, I'm trying to get DNP3 CRCs out but what ever I do it comes back incorrect.
>
> I've tried this:
> crc::crc16 -format 0x%X -seed 1001111010110010 E0C0810000 which gave 0xF4E4
This interprets the seed as a big decimal number.
>
> and this:
> crc::crc16 -format 0x%X -seed 0x9E2B E0C0810000 which gave 0x6DC0
I believe that with "E0C0810000" you mean 5 bytes with the values 0xE0,
0xC0, ... ? Your call interprets the message as 10 bytes, with the
values 'E', '0', 'C', .... since the crc package expects a binary string
in the message. Therefore, that should be
crc::crc16 -format 0x%X -seed 0x9E2B [binary format H* E0C0810000]
I think for your "real" application, the data will come in from a file
or a variable already in binary, so probably you won't need the binary
format there - unless you really get a string with hexadecimal digits.
> What I understand to be correct DNO-CRC for E0C0810000 is 0xC1F0
>
> The 9E2B and the 1001111010110010 are what I believe are the Hex and Bin seeds for DNP3 but I may be wrong.
I can't comment on this one.
> The Polynomial is :
> x^16 + x^13 + x^12 + x^11 + x^10 + x^8 + x^6 + x^5 + x^2 + 1
>
> Can anyone help please with correct syntax. Thanks in advance.
Here you are unlucky: poking into the implementation of crc16,
https://github.com/tcltk/tcllib/blob/master/modules/crc/crc16.tcl#L51
your polynomial is not in the predefined ones. Nevertheless it should be
possible to do it by writing a procedure analogous to the predefined
ones. Just copy this
https://github.com/tcltk/tcllib/blob/master/modules/crc/crc16.tcl#L181
and replace your polynomial there, e.g.
proc DNO-CRC {s {seed 0x9E2B}} {
variable DNOtable
if {![info exists DNOtable]} {
set poly [expr {(1<<16) | (1<<13) | (1<<12) | 1<<11 | 1<<10 | 1<<8 |
1<<6 | 1<<5 | 1<<2 | 1}]
set DNOtable [crc::Crc_table 16 $poly 1]
}
return [crc::Crc $s 16 DNOtable $seed 0 1]
}
This still comes back with a different answer:
(Sources) 77 % DNO-CRC [binary format H* E0C0810000]
28022
(Sources) 78 % format %X 28022
6D76
Maybe it has to do with the various flags passed into the table
generator and Crc. My knowledge of CRCs is too weak, though, to debug it
further.
Christian