Computer Interfacing

# CRC-16 Calculation

CRC-16 Problem

Author Message
gil
Guest

 Apr 27, 2014 4:27 pm Hi, i have made a function for CRC-16 on C# but i've been getting different calculation on what the ECR terminal expects for the CRC. Here is the data needed to be checked 000B010E01C60000000000003331303134313030410000042414160649 i'm getting 4e3d for CRC but the expected is supposed to be 23e1 Anyone has a suggestion?
gil2
Guest

Apr 27, 2014 4:30 pm

 Code: public class Crc16     {         const ushort polynomial = 0xA001;         ushort[] table = new ushort[256];         public ushort ComputeChecksum(byte[] bytes)         {             ushort crc = 0;             for (int i = 0; i < bytes.Length; ++i)             {                 byte index = (byte)(crc ^ bytes[i]);                 crc = (ushort)((crc >> 8) ^ table[index]);             }             return crc;         }         public byte[] ComputeChecksumBytes(byte[] bytes)         {             ushort crc = ComputeChecksum(bytes);             return BitConverter.GetBytes(crc);         }         public Crc16()         {             ushort value;             ushort temp;             for (ushort i = 0; i < table.Length; ++i)             {                 value = 0;                 temp = i;                 for (byte j = 0; j < 8; ++j)                 {                     if (((value ^ temp) & 0x0001) != 0)                     {                         value = (ushort)((value >> 1) ^ polynomial);                     }                     else                     {                         value >>= 1;                     }                     temp >>= 1;                 }                 table[i] = value;             }         }     }

Here is the code i am using.. credits to the creator
gil
Guest

 May 02, 2014 6:42 am anyone can help me which CRC to use to get the output shown at above.
Gammatester
Guest

May 05, 2014 10:51 am

As far as one can sys from one data set your CRC16 algorithm is CRC-Buypass:
 Code: ===== Checking for known algorithms ===== Checked data sets CRC1: \$23e1,   Data1: 000B010E01C60000000000003331303134313030410000042414160649 Found known algorithm: CRC-16/BUYPASS CRC=\$23e1  Poly=\$8005  init=\$0000  xorout=\$0000  refin=false  refout=false ===== done =====

Your polynomial 0xA001 is the reflected form of 0x8005, but CRC-16/BUYPASS uses no reflection. Here a C test program with the CRC-16/BUYPASS algorithm and your test data:
 Code: #include #include #include static uint16_t CRC16_BUYPASS(uint8_t *data, size_t len) {   uint16_t crc = 0x0000;   size_t j;   int i;   for (j=len; j>0; j--) {     crc ^= (uint16_t)(*data++) << 8;     for (i=0; i<8; i++) {       if (crc & 0x8000) crc = (crc<<1) ^ 0x8005;       else crc <<= 1;     }   }   return (crc); } int main() {   uint8_t test[9]  = {0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};   uint8_t tgil[29] = {0x00,0x0B,0x01,0x0E,0x01,0xC6,0x00,0x00,0x00,0x00,                       0x00,0x00,0x33,0x31,0x30,0x31,0x34,0x31,0x30,0x30,                       0x41,0x00,0x00,0x04,0x24,0x14,0x16,0x06,0x49};   uint16_t crc;   crc = CRC16_BUYPASS(test, 9);   printf("Rocksoft check value: 0x%04X, test ", crc);   if (crc==0xFEE8) printf("passed.\n"); else printf("failed!\n");   crc = CRC16_BUYPASS(tgil, 29);   printf("CRC gil's data: 0x%04X\n", crc);   return 0; }
And this is the compile/output
 Code: C:\TEST>gcc crc16b.c -o crc16b.exe C:\TEST>crc16b Rocksoft check value: 0xFEE8, test passed. CRC gil's data: 0x23E1
Matt
Guest

 Jul 08, 2014 9:33 am Hi Gammatester which tool can be used to check for known algorithms thanks Regards, Matt
Gammatester
Guest

 Jul 09, 2014 6:29 am I use the GUI program SRP16which searches CRC16 algorithms and has a simple check for about 30 known algorithms, search the web with the keywords SRP16 crc. reveng is a command-line crc finder and calculator not restricted to 16 bit, search for reveng crc.

 Page 1 of 1

Running on php BB © 2001, 2009 php BB Group