Search code examples
delphiserial-portdelphi-7

COM Port Commands CRC XOR


I have a check printer that I want to connect in Delphi 7 by COM port and operate.

I have a command that I extracted with Serial Port Monitor:

STX "PIRI(781" FS NULL ETX "0B" which is 02 50 49 52 49 28 37 38 31 1c 00 03 30 42 hex

The manual says the following:

CRC (which is the last two digits after the ETX) - packet checksum. It is calculated by the following algorithm: executing XOR for every byte of the block including ETX by excluding STX. The data of the checksum take up two bytes and are a symbolic representation of the numeric in a hexadecimal calculation system.

I tried to an ONLINE CRC calculator and return a 1B result and a 27 numeric.

  • How to do it? For "PIRI(781" FS NULL ETX it should be 0B

Solution

  • The documentation incorrectly identifies the check value as a CRC. It is not. It is simply the exclusive-or of the noted bytes. The exclusive-or of 50 49 52 49 28 37 38 31 1c 00 03 is 0b. You then convert the 0b to hex (with an upper case B, i.e. 0B), and get 30 42.