首页新闻找找看学习计划

CRC校验函数

0
悬赏园豆:20 [已解决问题] 解决于 2019-09-19 08:57

采用多项式X16 + X15 + X2 + 1的CRC16校验,低字节在前,高字节在后,如何编写校验函数?谢谢!

smartgoogle的主页 smartgoogle | 初学一级 | 园豆:45
提问于:2019-09-17 09:38
< >
分享
最佳答案
0

用了多年的,给你个参照:

namespace LModule.Utils
{
    public static class StringExt
    {

        public static string ToContentString(this Stream stream)
        {
            var reader = new StreamReader(stream);
            var text = reader.ReadToEnd();
            return text;
        }

        public static string ToBase64String(this Stream stream)
        {
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);
            stream.Seek(0, SeekOrigin.Begin);
            return Convert.ToBase64String(bytes);
        }

        public static byte[] ToUtf8Buffer(this string content)
        {
            return Encoding.UTF8.GetBytes(content);
        }
        public static string ToISO_8859_1(this string srcText)
        {
            var dst = "";
            var src = srcText.ToCharArray();
            for (var i = 0; i < src.Length; i++)
            {
                var str = @"&#" + (int)src[i] + ";";
                dst += str;
            }
            return dst;
        }
        public static string FromISO_8859_1(this string srcText)
        {
            var dst = "";
            var src = srcText.Split(';');
            for (var i = 0; i < src.Length; i++)
            {
                if (src[i].Length > 0)
                {
                    var str = ((char)int.Parse(src[i].Substring(2))).ToString();
                    dst += str;
                }
            }
            return dst;
        }

        public static IList<byte> EscapeCode(this IList<byte> tmpbytes)
        {
            for (int i = 1; i < tmpbytes.Count - 1; i++)
            {
                if (tmpbytes[i] == 0x5b)
                {
                    tmpbytes[i] = (byte)0x5a;
                    tmpbytes.Insert(i + 1, (byte)0x01);
                    i++;
                }
                else if (tmpbytes[i] == 0x5a)
                {
                    tmpbytes[i] = (byte)0x5a;
                    tmpbytes.Insert(i + 1, (byte)0x02);
                    i++;
                }
                else if (tmpbytes[i] == 0x5d)
                {
                    tmpbytes[i] = (byte)0x5e;
                    tmpbytes.Insert(i + 1, (byte)0x01);
                    i++;
                }
                else if (tmpbytes[i] == 0x5e)
                {
                    tmpbytes[i] = (byte)0x5e;
                    tmpbytes.Insert(i + 1, (byte)0x02);
                    i++;
                }
            }
            return tmpbytes;
        }

        public static IList<byte> UnEscapeCode(this IList<byte> tmpbytes)
        {
            for (int i = 1; i < tmpbytes.Count - 1; i++)
            {
                if (tmpbytes[i] == (byte)0x5a && tmpbytes[i + 1] == (byte)0x01)
                {
                    tmpbytes[i] = (byte)0x5b;
                    tmpbytes.RemoveAt(i + 1);
                }
                else if (tmpbytes[i] == (byte)0x5a && tmpbytes[i + 1] == (byte)0x02)
                {
                    tmpbytes[i] = (byte)0x5a;
                    tmpbytes.RemoveAt(i + 1);
                }
                else if (tmpbytes[i] == (byte)0x5e && tmpbytes[i + 1] == (byte)0x01)
                {
                    tmpbytes[i] = (byte)0x5d;
                    tmpbytes.RemoveAt(i + 1);
                }
                else if (tmpbytes[i] == (byte)0x5e && tmpbytes[i + 1] == (byte)0x02)
                {
                    tmpbytes[i] = (byte)0x5e;
                    tmpbytes.RemoveAt(i + 1);
                }
            }
            return tmpbytes;
        }

        public static ushort CRC16_CCITT(this IList<byte> ucbuf, int offset, int iLen)
        {
            ushort crc = 0xFFFF;          // initial value
            ushort polynomial = 0x1021;   // 0001 0000 0010 0001  (0, 5, 12)

            for (int j = 0; j < iLen; ++j)
            {
                for (int i = 0; i < 8; i++)
                {
                    bool bit = ((ucbuf[j + offset] >> (7 - i) & 1) == 1);
                    bool c15 = ((crc >> 15 & 1) == 1);
                    crc <<= 1;
                    if (c15 ^ bit) crc ^= polynomial;
                }
            }

            crc &= 0xffff;

            return crc;
        }
        
        static readonly Random CodeRandom = new Random();
        private const int CharMin = 0X20;
        private const int CharMax = 0X7F;
        public static string GetGuidString(int len)//len must large than 15
        {
            var guidString = new StringBuilder();
            var dateChars = DateTime.Now.ToString("yyMMddHHmmssfff").ToCharArray();
            if(len < dateChars.Length)throw new Exception($"len must large than {dateChars.Length}");
            foreach (var t in dateChars) guidString.Append(t);
            for (var i = dateChars.Length-1; i < len; i++)guidString.Append((char)CodeRandom.Next(CharMin, CharMax));

            return guidString.ToString();
        }
        public static string GetRandomString(int len)
        {
            var randomString = new StringBuilder();
            if (len < 1) throw new Exception($"len must large than 0");
            for (var i = 0; i < len; i++) randomString.Append((char)CodeRandom.Next(CharMin, CharMax));

            return randomString.ToString();
        }
        public static string GetRandomLenthString(int minLen,int maxLen)
        {
            var len = CodeRandom.Next(minLen, maxLen);
            var randomString = new StringBuilder();
            if (len < 1) throw new Exception($"len must large than 0");
            for (var i = 0; i < len; i++) randomString.Append((char)CodeRandom.Next(CharMin, CharMax));

            return randomString.ToString();
        }
    }
}
收获园豆:20
花飘水流兮 | 专家六级 |园豆:10297 | 2019-09-17 10:33

我需要的是CRC16 Modbus的校验函数

smartgoogle | 园豆:45 (初学一级) | 2019-09-18 14:47

@smartgoogle: 关于modbus之前不知道是不是你,有个c#库,去github找哈,master、slaver现成的

花飘水流兮 | 园豆:10297 (专家六级) | 2019-09-18 14:53
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册