public class ModbusCrc
    {
        /// <summary>
        /// 获取crc
        /// int
        /// 默认值ushort最小值
        /// </summary>
        /// <param name="Data"></param>
        /// <returns></returns>
        public static ushort ICRC16(byte[] Data)
        {
            ushort crc = ushort.MinValue;
            try
            {
                crc = 0xffff;
                ushort polynom = 0xA001;

                for (int i = 0; i < Data.Length; i++)
                {
                    crc ^= Data[i];
                    for (int j = 0; j < 8; j++)
                    {
                        if ((crc & 0x01) == 0x01)
                        {
                            crc >>= 1;
                            crc ^= polynom;
                        }
                        else
                        {
                            crc >>= 1;
                        }
                    }
                }
            }
            catch
            {

            }
            return crc;
        }


        /// <summary>
        /// 获取crc
        /// int
        /// 默认值ushort最小值
        /// </summary>
        /// <param name="Data"></param>
        /// <returns></returns>
        public static ushort ICRC16(byte[] Data, int lenght)
        {
            ushort crc = ushort.MinValue;
            try
            {
                crc = 0xffff;
                ushort polynom = 0xA001;
                var Temp = lenght;
                if (lenght > Data.Length)
                {
                    Temp = Data.Length;
                }

                for (int i = 0; i < Temp; i++)
                {
                    crc ^= Data[i];
                    for (int j = 0; j < 8; j++)
                    {
                        if ((crc & 0x01) == 0x01)
                        {
                            crc >>= 1;
                            crc ^= polynom;
                        }
                        else
                        {
                            crc >>= 1;
                        }
                    }
                }
            }
            catch
            {

            }
            return crc;
        }

        /// <summary>
        /// 获取crc
        /// 16进制
        /// 默认值null
        /// </summary>
        /// <param name="Data"></param>
        /// <returns></returns>
        public static string SCRC16(byte[] Data)
        {

            string result = null;
            try
            {
                int crc = 0xffff;
                ushort polynom = 0xA001;
                for (int i = 0; i < Data.Length; i++)
                {
                    crc ^= Data[i];
                    for (int j = 0; j < 8; j++)
                    {
                        if ((crc & 0x01) == 0x01)
                        {
                            crc >>= 1;
                            crc ^= polynom;
                        }
                        else
                        {
                            crc >>= 1;
                        }
                    }
                }
                result = crc.ToString("x8");
            }
            catch { }
            return result;
        }
        /// <summary>
        /// 获取crc
        /// 整体
        /// 默认值null
        /// </summary>
        /// <param name="Data"></param>
        /// <returns></returns>
        public static byte[] LCRC16(byte[] Data)
        {

            byte[] result = null;
            try
            {
                ushort crc = 0xffff;
                ushort polynom = 0xA001;
                result = new byte[Data.Length + 2];
                for (int i = 0; i < Data.Length; i++)
                {
                    crc ^= Data[i];
                    result[i] = Data[i];
                    for (int j = 0; j < 8; j++)
                    {
                        if ((crc & 0x01) == 0x01)
                        {
                            crc >>= 1;
                            crc ^= polynom;
                        }
                        else
                        {
                            crc >>= 1;
                        }
                    }
                }

                result[Data.Length] = (byte)(crc & 0xff);
                result[Data.Length + 1] = (byte)((crc >> 8) & 0xff);
            }
            catch
            {
            }
            return result;
        }

        /// <summary>
        /// 获取crc
        /// 比较
        /// 默认值false
        /// </summary>
        /// <param name="Data"></param>
        /// <returns></returns>
        public static bool BCRC16(byte[] Data)
        {
            bool result = false;
            try
            {
                ushort crc = 0xffff;
                ushort polynom = 0xA001;
                if (Data.Length > 2)
                {
                    for (int i = 0; i < Data.Length - 2; i++)
                    {
                        crc ^= Data[i];
                        for (int j = 0; j < 8; j++)
                        {
                            if ((crc & 0x01) == 0x01)
                            {
                                crc >>= 1;
                                crc ^= polynom;
                            }
                            else
                            {
                                crc >>= 1;
                            }
                        }
                    }
                }

                if (Data[Data.Length - 2] == (byte)(crc & 0xff) && Data[Data.Length - 1] == (byte)((crc >> 8) & 0xff))
                {
                    result = true;
                }
            }
            catch
            {
            }
            return result;
        }

    }

 

posted on 2022-04-02 13:13  金科许俊  阅读(256)  评论(0)    收藏  举报