Example #1
0
        /// <summary>
        /// 通过“消息ID”和“参数列表”获取消息报文字节数组
        /// </summary>
        /// <param name="msgId">
        /// 消息ID
        ///     <para>UInt16类型,长度为2个字节</para>
        /// </param>
        /// <param name="pmtList">参数列表</param>
        /// <returns></returns>
        protected Byte[] GetDatagram(MessageId msgId, List <Parameter> pmtList)
        {
            // 获取消息体对象
            MessageBody mb = new MessageBody(
                msgId,
                DevId,
                pmtList);

            // 获取消息体字节数组
            Byte[] msgBody = mb.GetBody();

            // 获取消息头对象
            MessageHead mh = new MessageHead(
                (UInt16)(msgBody.Length),
                MessageType.ServerToDevice,
                Counter.Instance.SeqNumber++,
                Crc.GetCrc(msgBody));

            // 返回消息报文字节数组
            return(new Datagram(mh, mb).GetDatagram());
        }
Example #2
0
        /// <summary>
        /// 通过“消息ID”和“参数列表”获取消息报文字节数组
        /// </summary>
        /// <param name="msgId">
        /// 消息ID
        ///     <para>UInt16类型,长度为2个字节</para>
        /// </param>
        /// <param name="pmtList">参数列表</param>
        /// <returns></returns>
        protected Byte[] GetDatagram(MessageId msgId, List<Parameter> pmtList)
        {
            // 获取消息体对象
            MessageBody mb = new MessageBody(
                msgId,
                DevId,
                pmtList);

            // 获取消息体字节数组
            Byte[] msgBody = mb.GetBody();

            // 获取消息头对象
            MessageHead mh = new MessageHead(
                MessageType,
                (UInt16)(msgBody.Length),
                Counter.Instance.SeqNumber++,
                Crc.GetCrc(msgBody));

            // 返回消息报文字节数组
            return new Datagram(mh, mb).GetDatagram();
        }
Example #3
0
        /// <summary>
        /// 获取消息报文对象列表
        /// </summary>
        /// <param name="dataArray">消息报文字节数组</param>
        /// <returns></returns>
        public static List<Datagram> GetDatagramList(Byte[] dataArray)
        {
            if (dataArray.Length < 22)
            {
                throw new AirException("命令格式错误。", ResponseCode.CommandFormatError);
            }

            List<Byte[]> byteArrayList = new List<byte[]>();
            List<Byte[]> newByteArrayList = new List<byte[]>();

            GetByteArrayList(dataArray, 0, ref byteArrayList);
            Descaping(byteArrayList, ref newByteArrayList);

            MessageHead mh = new MessageHead();
            MessageBody mb = new MessageBody();
            Datagram d = new Datagram();
            List<Datagram> datagramList = new List<Datagram>();

            foreach (var byteArray in newByteArrayList)
            {
                if (!Enum.IsDefined(typeof(MessageType), byteArray[0]))
                {
                    throw new AirException("不支持该类型的命令。", ResponseCode.NonsupportType);
                }

                mh.Type = (MessageType)byteArray[0];
                mh.Length = (UInt16)((byteArray[1] << 8) + byteArray[2]);
                mh.SeqNumber = (UInt32)((byteArray[3] << 24) + (byteArray[4] << 16) + (byteArray[5] << 8) + byteArray[6]);
                mh.Reserved = (UInt32)((byteArray[7] << 16) + (byteArray[8] << 8) + byteArray[9]);
                mh.Crc = (UInt16)((byteArray[10] << 8) + byteArray[11]);

                if (!Enum.IsDefined(typeof(MessageId), (UInt16)((byteArray[12] << 8) + byteArray[13])))
                {
                    throw new AirException("不支持该操作。", ResponseCode.NonsupportOperation);
                }

                mb.MsgId = (MessageId)((byteArray[12] << 8) + byteArray[13]);
                mb.DevId = ((UInt64)byteArray[14] << 56) + ((UInt64)byteArray[15] << 48) + ((UInt64)byteArray[16] << 40) + ((UInt64)byteArray[17] << 32)
                    + ((UInt64)byteArray[18] << 24) + ((UInt64)byteArray[19] << 16) + ((UInt64)byteArray[20] << 8) + byteArray[21];

                List<Parameter> pmtList = new List<Parameter>();
                GetParameterList(byteArray, 22, ref pmtList);

                if (pmtList.Count > 0)
                {
                    mb.PmtList = pmtList;

                    if (Crc.GetCrc(mb.GetBody()) != mh.Crc)
                    {
                        throw new AirException("消息体CRC校验错误。", ResponseCode.CrcCheckError);
                    }
                }
                else
                {
                    throw new AirException("参数格式错误。", ResponseCode.ParameterFormatError);
                }

                d.Head = mh;
                d.Body = mb;
                datagramList.Add(d);
            }

            return datagramList;
        }
Example #4
0
        /// <summary>
        /// 获取消息报文对象列表
        /// </summary>
        /// <param name="dataArray">消息报文字节数组</param>
        /// <returns></returns>
        public static List <Datagram> GetDatagramList(Byte[] dataArray)
        {
            if (dataArray.Length < 22)
            {
                throw new AirException("命令格式错误。", ResponseCode.CommandFormatError);
            }

            List <Byte[]> byteArrayList    = new List <byte[]>();
            List <Byte[]> newByteArrayList = new List <byte[]>();

            GetByteArrayList(dataArray, 0, ref byteArrayList);
            Descaping(byteArrayList, ref newByteArrayList);

            MessageHead     mh           = new MessageHead();
            MessageBody     mb           = new MessageBody();
            Datagram        d            = new Datagram();
            List <Datagram> datagramList = new List <Datagram>();

            foreach (var byteArray in newByteArrayList)
            {
                if (!Enum.IsDefined(typeof(MessageType), byteArray[0]))
                {
                    throw new AirException("不支持该类型的命令。", ResponseCode.NonsupportType);
                }

                mh.Type      = (MessageType)byteArray[0];
                mh.Length    = (UInt16)((byteArray[1] << 8) + byteArray[2]);
                mh.SeqNumber = (UInt32)((byteArray[3] << 24) + (byteArray[4] << 16) + (byteArray[5] << 8) + byteArray[6]);
                mh.Reserved  = (UInt32)((byteArray[7] << 16) + (byteArray[8] << 8) + byteArray[9]);
                mh.Crc       = (UInt16)((byteArray[10] << 8) + byteArray[11]);

                if (!Enum.IsDefined(typeof(MessageId), (UInt16)((byteArray[12] << 8) + byteArray[13])))
                {
                    throw new AirException("不支持该操作。", ResponseCode.NonsupportOperation);
                }

                mb.MsgId = (MessageId)((byteArray[12] << 8) + byteArray[13]);
                mb.DevId = ((UInt64)byteArray[14] << 56) + ((UInt64)byteArray[15] << 48) + ((UInt64)byteArray[16] << 40) + ((UInt64)byteArray[17] << 32)
                           + ((UInt64)byteArray[18] << 24) + ((UInt64)byteArray[19] << 16) + ((UInt64)byteArray[20] << 8) + byteArray[21];

                List <Parameter> pmtList = new List <Parameter>();
                GetParameterList(byteArray, 22, ref pmtList);

                if (pmtList.Count > 0)
                {
                    mb.PmtList = pmtList;

                    if (Crc.GetCrc(mb.GetBody()) != mh.Crc)
                    {
                        throw new AirException("消息体CRC校验错误。", ResponseCode.CrcCheckError);
                    }
                }
                else
                {
                    throw new AirException("参数格式错误。", ResponseCode.ParameterFormatError);
                }

                d.Head = mh;
                d.Body = mb;
                datagramList.Add(d);
            }

            return(datagramList);
        }