Пример #1
0
        /// <summary>
        /// 湿度 01H	 N(5,2)	1AH 百分比
        /// 温度 02H	 N(5,2)	1AH	摄氏度
        /// 湿度阈值 04H	 N(5,2)	1AH	百分比
        /// 温度阈值 05H	 N(5,2)	1AH	摄氏度
        /// 温湿度/温湿度阈值,BCD码,3字节
        /// 示例:01 1A 00 88 66 02 1A 00 12 34
        ///      04 1A 00 55 66 05 1A 00 66 55
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static double Humiture(byte[] data)
        {
            byte[] temp = BytesUtil.SubBytes(data, 2, 3);

            string bcdStr = BCDUtil.ConvertTo(temp);

            return(Convert.ToInt64(bcdStr) / 100.0);
        }
Пример #2
0
        public bool CheckCRC(byte[] crc, int endBeforeLength)
        {
            byte[] checkData = new byte[endBeforeLength];

            Array.Copy(Data, 0, checkData, 0, endBeforeLength);

            string str = BytesUtil.ToHexString(CRCUtil.CRC16(checkData));

            return(str.Equals(BytesUtil.ToHexString(crc)));
        }
Пример #3
0
        public byte[] ToByte()
        {
            string[] code = new string[] { "15", "A5" };

            List <byte> list = new List <byte>();

            list.AddRange(new byte[] { 0x08, 0x08 });
            list.AddRange(BytesUtil.ToHexArray(Code));
            list.AddRange(new byte[] { 0x06, 0x08 });
            list.AddRange(BytesUtil.ToHexArray(code[State.RelayOne]));
            list.AddRange(new byte[] { 0x07, 0x08 });
            list.AddRange(BytesUtil.ToHexArray(code[State.RelayTwo]));
            list.AddRange(new byte[] { 0x09, 0x08 });
            list.AddRange(BytesUtil.ToHexArray(code[State.Dehumidify]));

            return(list.ToArray());
        }
Пример #4
0
        public byte[] ToByte()
        {
            List <byte> list = new List <byte>();

            list.AddRange(new byte[] { 0x08, 0x08 });
            list.AddRange(BytesUtil.ToHexArray(Code));
            list.AddRange(new byte[] { 0x04, 0x1A });
            string _humLimit = (Math.Round(HumidityLimit, 2) * 100).ToString().PadLeft(6, '0');

            list.AddRange(BCDUtil.ConvertFrom(_humLimit, 3));
            list.AddRange(new byte[] { 0x05, 0x1A });
            string _temLimit = (Math.Round(TemperatureLimit, 2) * 100).ToString().PadLeft(6, '0');

            list.AddRange(BCDUtil.ConvertFrom(_temLimit, 3));

            return(list.ToArray());
        }
Пример #5
0
        private byte[] Decode(byte[] data, ref Element element)
        {
            if (data.Length == 0 || !Enum.IsDefined(typeof(DecodeType), data[0]))
            {
                return(new byte[0]);
            }

            int length = GetLength(data[0]);

            if (length == 0 || data.Length < length)
            {
                return(new byte[0]);
            }

            switch ((DecodeType)data[0])
            {
            case DecodeType.Code:
                element.Code = ElementDecodeFunctions.Code(data);
                break;

            case DecodeType.Humidity:
                element.Humidity = ElementDecodeFunctions.Humiture(data);
                break;

            case DecodeType.Temperature:
                element.Temperature = ElementDecodeFunctions.Humiture(data);
                break;

            case DecodeType.State:
                element.State = ElementDecodeFunctions.State(data);
                break;

            case DecodeType.HumidityLimit:
                element.HumidityLimit = ElementDecodeFunctions.Humiture(data);
                break;

            case DecodeType.TemperatureLimit:
                element.TemperatureLimit = ElementDecodeFunctions.Humiture(data);
                break;
            }

            return(BytesUtil.SubBytes(data, length));
        }
Пример #6
0
        public List <Element> ReadAll()
        {
            List <Element> list = new List <Element>();

            do
            {
                //如果数据头不是主从机地址,则取消解析
                if (!(LeftData[0].Equals((byte)DecodeType.Code) && LeftData[1].Equals(0x08)))
                {
                    break;
                }

                Element element = Read(LeftData);

                list.Add(element);

                LeftData = BytesUtil.SubBytes(LeftData, element.DataLength);
            } while (LeftData.Length > 0);

            return(list);
        }
Пример #7
0
        public RecieveMessage Read()
        {
            RecieveMessage message = new RecieveMessage();

            message.Content      = BytesUtil.ToHexString(BaseDecode.Data);
            message.CenterCode   = CenterCode();
            message.ClientCode   = ClientCode();
            message.SendTime     = SendTime();
            message.Serial       = Serial();
            message.FunctionCode = FunctionCode();
            message.DataLength   = DataLength();
            message.CRC          = CRC(message.DataLength + 21);
            message.IsChecked    = false;

            //只有数据主体起始符和结束符位置都正确,才会继续解析数据主体
            if (!BaseDecode.IsChecked)
            {
                return(message);
            }

            //心跳包不进行解析
            if (!message.FunctionCode.Equals("F2"))
            {
                byte[] bodyData = Body(message.DataLength);
                message.Body = bodyData;

                //如果是召测数据或者客户端自报数据,数据主体以采集时间开头,截取后在进行解码
                if (message.FunctionCode.Equals("B0") || message.FunctionCode.Equals("C0"))
                {
                    message.DataTime = DataTime(bodyData);
                    //如果不是以E0E0开头,则说明包体内容错误,无需向下解析
                    if (!BaseDecode.IsChecked)
                    {
                        return(message);
                    }
                    bodyData = BytesUtil.SubBytes(bodyData, 7);
                }

                if (message.FunctionCode.Equals("B3"))
                {
                    ElementB3Decode b3Decode = new ElementB3Decode(bodyData);
                    message.Data = b3Decode.Read();
                }
                else
                {
                    ElementDecode  decode   = new ElementDecode(bodyData);
                    List <Element> dataList = decode.ReadAll();

                    if (message.FunctionCode.Equals("B0") || message.FunctionCode.Equals("C0"))
                    {
                        message.Data = dataList.Select(q => new B0C0Element(q)).ToList();
                    }
                    else if (message.FunctionCode.Equals("B1"))
                    {
                        message.Data = dataList.Select(q => new B1Element(q)).ToList();
                    }
                    else if (message.FunctionCode.Equals("B2"))
                    {
                        message.Data = dataList.Select(q => new B2Element(q)).ToList();
                    }
                }
            }

            bool isChecked = BaseDecode.IsChecked;

            //如果以上都通过,则校验最后的CRC码
            if (isChecked)
            {
                isChecked = BaseDecode.CheckCRC(message.CRC, message.DataLength + 21);
            }

            message.IsChecked = isChecked;

            return(message);
        }