Exemple #1
0
        static public CRCInfo GetCRCInfo(CRCType type)
        {
            CRCInfo param = null;

            switch (type)
            {
            case CRCType.CRC16_IBM:
                param = new CRCInfo(0x8005, 0x0000, true, true, 0x0000);
                break;

            case CRCType.CRC16_MAXIM:
                param = new CRCInfo(0x8005, 0x0000, true, true, 0xFFFF);
                break;

            case CRCType.CRC16_USB:
                param = new CRCInfo(0x8005, 0xFFFF, true, true, 0xFFFF);
                break;

            case CRCType.CRC16_MODBUS:
                param = new CRCInfo(0x8005, 0xFFFF, true, true, 0x0000);
                break;

            case CRCType.CRC16_CCITT:
                param = new CRCInfo(0x1021, 0x0000, true, true, 0x0000);
                break;

            case CRCType.CRC16_CCITT_FALSE:
                param = new CRCInfo(0x1021, 0xFFFF, false, false, 0x0000);
                break;

            case CRCType.CRC16_X25:
                param = new CRCInfo(0x1021, 0xFFFF, true, true, 0xFFFF);
                break;

            case CRCType.CRC16_XMODEM:
                param = new CRCInfo(0x1021, 0x0000, false, false, 0x0000);
                break;

            case CRCType.CRC16_DNP:
                param = new CRCInfo(0x3D65, 0x0000, true, true, 0xFFFF);
                break;

            case CRCType.CRC32:
                param = new CRCInfo(0x04C11DB7, 0xFFFFFFFF, true, true, 0xFFFFFFFF);
                break;

            case CRCType.CRC32_MPEG2:
                param = new CRCInfo(0x04C11DB7, 0xFFFFFFFF, false, false, 0x00000000);
                break;
            }

            return(param);
        }
Exemple #2
0
        static public UInt16 GetCRC16(CRCInfo param, byte[] data)
        {
            UInt16 crc    = (UInt16)param.Init;
            UInt16 Poly   = 0;
            UInt16 XorOut = (UInt16)param.XorOut;

            if (param.RefIn)
            {
                for (int i = 0; i < 16; i++)
                {
                    Poly <<= 1;
                    if ((param.Poly & (1u << i)) != 0)
                    {
                        Poly |= 0x01;
                    }
                }
            }
            else
            {
                Poly = (UInt16)param.Poly;
            }

            foreach (byte b in data)
            {
                UInt16 bValue;
                if (param.RefOut)
                {
                    bValue = Convert.ToUInt16(b);
                }
                else
                {
                    bValue = Convert.ToUInt16((UInt16)b << 8);
                }

                crc = Convert.ToUInt16(crc ^ bValue);
                for (int i = 0; i < 8; i++)
                {
                    if (param.RefOut)
                    {
                        if ((crc & 0x01) != 0)
                        {
                            crc >>= 1;
                            crc  ^= Poly;
                        }
                        else
                        {
                            crc >>= 1;
                        }
                    }
                    else
                    {
                        if ((crc & 0x8000) != 0)
                        {
                            crc <<= 1;
                            crc  ^= Poly;
                        }
                        else
                        {
                            crc <<= 1;
                        }
                    }
                }
            }

            return(Convert.ToUInt16(crc ^ XorOut));
        }