Контрольная сумма команды crc вычисляет 8/16/32 биты CRC и его формат следующим образом:
Команда возвращает значение CRC после вычисления с параметрами.
Параметр | Описание |
---|---|
bits | 8 - 8 bits CRC 16 - 16 bits CRC 32 - 32 bits CRC |
$msg | the original message to be computed |
init | CRC initial value. If it is omitted the default value is: 8 bits - ff, 16 bits - 1d0f, 32 bits - ffffffff |
div | The divisor(polynomial) to be used for CRC calculation. If it is omitted the default value is: 8 bits - e0, 16 bits - 1021, 32 bits - edb88320 |
msb/lsb | the CRC calculation order msb: calculated from the MSB to LSB lsb: calculated from the LSB to MSB If it is omitted the default value is: 8 bits - lsb, 16 bits - msb, 32bits - lsb |
Ниже приведен пример кода для каждого типа CRC.
<?php
$string = "123456789";
printf("CRC-16-ANSI : %04x\r\n", (int)system("crc 16 %1 0000 a001 lsb", $string));
printf("CRC-16-Modbus : %04x\r\n", (int)system("crc 16 %1 ffff a001 lsb", $string));
printf("CRC-CCITT FFFF: %04x\r\n", (int)system("crc 16 %1 ffff 1021 msb", $string));
printf("CRC-CCITT 1D0F: %04x\r\n", (int)system("crc 16 %1 1d0f 1021 msb", $string));
printf("CRC-CCITT XModem : %04x\r\n", (int)system("crc 16 %1 0000 1021 msb", $string));
$crc16_out = (int)system("crc 16 123456789 %1 8408 lsb", $string);
$crc16_out = bin2int(int2bin($crc16_out, 2, true), 0, 2);
printf("CRC-CCITT Kermit : %04x\r\n", $crc16_out);
$crc16_out = (int)system("crc 16 123456789 ffff 8408 lsb");
$crc16_out = $crc16_out ^ 0xffff;
printf("CRC-CCITT PPP : %04x\r\n", $crc16_out);
$crc16_out = ~(int)system("crc 16 %1 0000 a6bc lsb", $string);
$crc16_out = bin2int(int2bin($crc16_out, 2, true), 0, 2);
printf("CRC-16-DNP : %04x\r\n", $crc16_out);
?>