1

I've hit a brick wall here and I need your help. I have a fiscal printer (Datecs DP-50) to integrate on a chrome extension app that makes sales. The problem is that we can't use the driver since the app will run a on a remote server and not on the operators devices and we have to send the data raw. The App coded in PHP using Laravel 5.1 as extra info.

So I have a CRC base 16 with a custom polynom x^15+1 but it's a bit more than that, and I cannot figure it out. I will paste in below the documentation from the manual.

   The two CRC bytes are calculated according to the formula x^15 + 1. In the 
    calculation are included all data bytes plus the byte for block end. Every byte passes 
    through the calculation register from teh MSB to LSB.
    Three working bytes are used - S1, S0 and TR
    S1 - Most significant byte from the CRC ( it is transmitted immediatelly after END)
    S0 - Least significant byte from the CRC ( It is transmitted after S1)
    TR - the current transmitted byte in the block.

    The CRC is calculated as follows:
    1. S1 and S0 are zeroed
    2. TR is loaded with the current transmitted byte. The byte is transmitted.
    3. Points 3.1 and 3.2 are executed 8 times:
    3.1. S1, S0 and TR are shifted one bit to the left.
    3.2. If the carry bit from S1 is 1, the MSB of S1 and LSB of S0 are inverted.
    Points 2 and 3 are executed for all bytes, included in the calculation of the CRC - from 
    the first byte after BEG up to and including byte END.
    4. TR is loaded with 0 and point 3 is executed
    5. TR is loaded with 0 and point 3 is executed
    6. Byte S1 is transmitted
    7. Byte S0 is transmitted

For example the CRC (Only S1 and S0) for "A" string is in hex: fe 09. For "B" => fc 09, for "C" => 7d f6. The complete CRC would be 0d fe 09 for "A". TR from the Serial COM Monitor seems to be a constant and always represented as 0d hex.

Any help decoding this is appreciated. Thanks in advance!

1 Answer 1

2

The description is flawed, aside from the misspellings, in that it misses the crucial detail that the S1, S0, TR registers, in that order, are to be treated as a single 24-bit register when shifting left. If you do that, then you get the results you quote. You need to include the 0x0d in the calculation (the "END" byte).

function crc16($crc, $byte) {
    $crc = ($crc << 8) + $byte;
    for ($k = 0; $k < 8; $k++)
        $crc = ($crc & 0x800000) == 0 ? $crc << 1 : ($crc << 1) ^ 0x800100;
    $crc = ($crc >> 8) & 0xffff;
    return $crc;
}

$crc = 0;
$crc = crc16($crc, ord("A"));
$crc = crc16($crc, 13);
$crc = crc16($crc, 0);
$crc = crc16($crc, 0);
echo dechex($crc);

gives fe09.

1
  • 1
    Sorry about that, my knowledge of low level programming is quite limited and english is not my native language. Yes, I omitted to put the END byte in the calculation, thus my wrong results. I found a js implementation of the the algorythm here also: sunshine2k.de/coding/javascript/crc/crc_js.html . Inspect element for the js code. Thanks Mark! May 3, 2016 at 7:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.