Ejemplo n.º 1
0
        public void Test1()
        {
            var buffer = new byte[] { 0x00, 0x1E, 0x01, 0x1A, 0x00, 0x00, 0x01, 0x01 };

            var actual = CrcTool.CaculateCCITT16(buffer, 0, buffer.Length);

            Assert.AreEqual(0x1089, actual);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 解析指定的字节流。
        /// </summary>
        public void ParseBytes(byte[] bytes)
        {
            if (bytes == null || bytes.Length < AleFrame.HeadLength)
            {
                throw new AleFrameParsingException("无法将指定的字节流解析为AleFrame,长度不够。");
            }

            int startIndex = 0;

            // 包长度
            var pktLen = RsspEncoding.ToHostUInt16(bytes, startIndex);

            startIndex += AleFrame.SizeofHeadLen;

            // 版本
            this.Version = bytes[startIndex++];

            // 应用类型
            this.ApplicationType = bytes[startIndex++];

            // 传输序列号
            this.SequenceNo = RsspEncoding.ToHostUInt16(bytes, startIndex);
            startIndex     += 2;

            // N/R标志
            this.IsNormal = (bytes[startIndex++] == 1);

            // 包类型
            this.FrameType = (AleFrameType)bytes[startIndex++];

            // 校验和
            if (this.FrameType != AleFrameType.KAA && this.FrameType != AleFrameType.KANA) // 生命信息不需要校验
            {
                //var actualCrcValue = BitConverter.ToUInt16(bytes, startIndex);
                var actualCrcValue   = RsspEncoding.ToHostUInt16(bytes, startIndex);
                var expectedCrcValue = CrcTool.CaculateCCITT16(bytes, 0, 8);
                if (actualCrcValue != expectedCrcValue)
                {
                    throw new AleFrameParsingException(string.Format("解析Ale协议帧时发生异常,CRC检验不一致,期望值是{0},实际值是{1}。",
                                                                     expectedCrcValue, actualCrcValue));
                }
            }
            startIndex += 2;

            // 用户数据
            var userDataLen = pktLen - (AleFrame.HeadLength - AleFrame.SizeofHeadLen);

            if (userDataLen < 0)
            {
                throw new AleFrameParsingException(string.Format("将字节流解析为ALE帧时发生异常,用户数据长度为负值{0}。", userDataLen));
            }
            this.UserData = AleUserData.Parse(this.FrameType, bytes, startIndex, startIndex + userDataLen - 1);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 获取序列化后的字节流。
        /// </summary>
        public byte[] GetBytes()
        {
            var bytes      = new byte[AleFrame.HeadLength + this.UserDataLength];
            var startIndex = 0;

            // 包长度
            var tempBuf = RsspEncoding.ToNetUInt16(this.PacketLength);

            Array.Copy(tempBuf, 0, bytes, startIndex, tempBuf.Length);
            startIndex += AleFrame.SizeofHeadLen;

            // 版本
            bytes[startIndex++] = this.Version;

            // 应用类型
            bytes[startIndex++] = this.ApplicationType;

            // 传输序列号
            tempBuf = RsspEncoding.ToNetUInt16(this.SequenceNo);
            Array.Copy(tempBuf, 0, bytes, startIndex, tempBuf.Length);
            startIndex += 2;

            // N/R标志
            bytes[startIndex++] = (byte)(this.IsNormal ? 1 : 0);

            // 包类型
            bytes[startIndex++] = (byte)this.FrameType;

            // 校验和
            var crcValue = CrcTool.CaculateCCITT16(bytes, 0, 8);

            //tempBuf = BitConverter.GetBytes(crcValue);
            tempBuf = RsspEncoding.ToNetUInt16(crcValue);
            Array.Copy(tempBuf, 0, bytes, startIndex, tempBuf.Length);
            startIndex += 2;

            // 用户数据
            var userData = this.UserData.GetBytes();

            if (userData != null)
            {
                Array.Copy(userData, 0, bytes, startIndex, this.UserDataLength);
            }

            return(bytes);
        }