Пример #1
0
        public static bool validate(byte[] content)
        {
            var buffer           = Unpooled.WrappedBuffer(content);
            var waitValidateData = new byte[buffer.ReadableBytes - 2];

            buffer.ReadBytes(waitValidateData);

            var crc = new byte[2];

            buffer.ReadBytes(crc);

            var calCrc = CRCUtil.CalCrc(waitValidateData);

            return(BytesUtil.ByteArrayEquals(calCrc, crc));
        }
Пример #2
0
        public static byte[] Split(IByteBuffer buffer)
        {
            var readableSize = buffer.ReadableBytes;

            if (readableSize == 0)
            {
                return(null);
            }
            int startIndex = -1;

            byte[] starter = new byte[2];
            for (int i = 0; i < readableSize - 1; i++)
            {
                starter[0] = buffer.GetByte(i);
                starter[1] = buffer.GetByte(i + 1);
                if (BytesUtil.ByteArrayEquals(starter, LaserDefault.Starter))
                {
                    startIndex = i;
                    break;
                }
            }
            //至少得有13个字节 帧头(2) + 长度(2) + 硬件类型(1) + MAC地址(6) + CRC校验(2)
            if (startIndex == -1 || readableSize < 13)
            {
                return(null);
            }

            buffer.SetReaderIndex(startIndex);
            byte[] srcLength = new byte[2];
            buffer.GetBytes(2, srcLength);
            int length = BytesUtil.Bytes2Int16(srcLength);

            //真正的包长度 = 帧头(2) + 长度(2) + 【长度值 硬件类型(1) + MAC地址(6) + 业务数据(n)】 + CRC校验(2)
            byte[] frame       = null;
            int    frameLength = length + 6;

            if (readableSize >= frameLength)
            {
                frame = new byte[frameLength];
                buffer.ReadBytes(frame);
            }
            return(frame);
        }