CRC校验至少是2个字节吧,也有4个字节的
看你的接收方协议而定

解决方案 »

  1.   

    如果是SUM校验,分1个字节和2个字节
    如果是CRC校验,只分为CRC16和CRC32,没有CRC8给你段代码
            public void GetCheck(int ByteLength, ref byte[] ombuffer, string CheckMode)
            {
                int i, j = 0;
                byte hi, lo, c1, c2;
                switch (CheckMode)
                {
                    case "CRC16":
                        hi = 0xff;
                        lo = 0xff;
                        for (i = 0; i < ByteLength; i++)
                        {
                            lo = (byte)(lo ^ ombuffer[i]);
                            for (j = 0; j < 8; j++)
                            {
                                c1 = lo;
                                c2 = hi;
                                lo = (byte)(lo >> 1);
                                hi = (byte)(hi >> 1);
                                if ((c2 & 0x01) != 0)
                                {
                                    lo = (byte)(lo | 0x80);
                                }
                                if ((c1 & 0x01) != 0)
                                {
                                    hi = (byte)(hi ^ 0xa0);
                                    lo = (byte)(lo ^ 0x01);
                                }
                            }
                        }
                        ombuffer[ByteLength] = lo;
                        ombuffer[ByteLength + 1] = hi;
                        break;
                    case "SUM":
                        lo = 0x00;
                        for (i = 0; i < ByteLength; i++)
                        {
                            lo += ombuffer[i];
                        }
                        ombuffer[ByteLength] = lo;
                        break;
                }        }        public bool CheckCRC(int ByteLength, byte[] imbuffer)
            {
                int i, j, k = 0;
                byte hi, lo, c1, c2;
                hi = 0xff;
                lo = 0xff;
                if (ByteLength < 4)
                {
                    return false;
                }
                for (i = 0; i < (ByteLength - 2); i++)
                {
                    lo = (byte)(lo ^ imbuffer[k]);
                    for (j = 0; j < 8; j++)
                    {
                        c1 = lo;
                        c2 = hi;
                        lo = (byte)(lo >> 1);
                        hi = (byte)(hi >> 1);
                        if ((c2 & 0x01) != 0)
                        {
                            lo = (byte)(lo | 0x80);
                        }
                        if ((c1 & 0x01) != 0)
                        {
                            hi = (byte)(hi ^ 0xa0);
                            lo = (byte)(lo ^ 0x01);
                        }
                    }
                    k++;
                }
                c1 = imbuffer[k];
                k++;
                c2 = imbuffer[k];            if ((c1 == lo) && (c2 == hi))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }