Computer Interfacing
Discussions about interfacing and electronics
 

CRC16 function in PHP


 

       Computer Interfacing Forum Index -> Error detection and correction
Author Message
dirtyrobinson
New User



Joined: 13 Apr 2014
Posts: 4


Apr 14, 2014 5:54 am

I would prefer a php function (since I know php best) that will return a crc16 value. Would anyone know of any good links that would help me out? Or if its not that difficult, would someone help me write one? I would really like some how-to that would explain what each part of the function does as I'm very interested in how the calculations work.

Reveng gave me this info for my calculation:
Poly = 8005
Init = AC6F
XORout = 0000
refin = true
refout = true
check = 84d9

Any help is greatly appreciated!
dirtyrobinson
New User



Joined: 13 Apr 2014
Posts: 4


Apr 15, 2014 8:17 pm

Would someone be able to explain how I can go about reflecting a crc?

I have a working PHP function that spits out the unreflected value but I'm at a loss of how I can reflect it. This is partly because I haven't quite grasped what refin / refout really means.

Here's the function:
Code:
function crc16($data) {
   $poly = 0x8005;
   $xor = 0x0000;

   $crc = 0xAC6F;
   $len = strlen($data);
   $i = 0;
   while ($len--) {
      $crc ^= ord($data[$i++]) << 8;
      $crc &= 0xffff;
      for ($j = 0; $j < 8; $j++){
         $crc = ($crc & 0x8000) ? ($crc << 1) ^ $poly : $crc << 1;
         $crc &= 0xffff;
      }
   }
   $crc ^= $xor;
   return $crc;
}


For instance:
Input data = 10531050105302E043000001010203
dechex return = C20A
Reflected value (from Hex Workshop & what I need) = D16D

Does this make sense?

       Computer Interfacing Forum Index -> Error detection and correction
Page 1 of 1



Running on php BB © 2001, 2009 php BB Group
   Lammert Bies     Interfacing     Sitemap     Forum