Exemple #1
0
        public void DecodeCommand(IProtocolPackage <byte[]> package)
        {
            var currentIndex = 0;

            var container = package[StructureNames.Data].ComponentContent;

            for (var i = 0; i < package.Command.CommandDatas.Count; i++)
            {
                var data = package.Command.CommandDatas.First(obj => obj.DataIndex == i);

                if (currentIndex + data.DataLength > container.Length)
                {
                    package.Status = PackageStatus.NoEnoughBuffer;
                    return;
                }

                var component = new PackageComponent <byte[]>
                {
                    ComponentName    = data.DataName,
                    DataType         = data.DataConvertType,
                    ComponentIndex   = data.DataIndex,
                    ComponentContent = container.SubBytes(currentIndex, currentIndex + data.DataLength)
                };

                currentIndex += data.DataLength;

                package.AppendData(component);
            }

            package.DeviceNodeId = (string)DataConverter.DecodeComponentData(package[StructureNames.NodeId]);

            package.Finalization();
        }
        /// <summary>
        /// 解码固定顺序数据段
        /// </summary>
        /// <param name="package"></param>
        /// <param name="container"></param>
        private void DecodeOrderedData(IProtocolPackage <byte[]> package, byte[] container)
        {
            var currentIndex = 0;

            for (var i = 0; i < package.Command.CommandDatas.Count; i++)
            {
                var data = package.Command.CommandDatas.First(obj => obj.DataIndex == i);

                if (currentIndex + data.DataLength > container.Length)
                {
                    package.Status = PackageStatus.NoEnoughBuffer;
                    return;
                }

                var component = new PackageComponent <byte[]>
                {
                    ComponentName    = data.DataName,
                    DataType         = data.DataConvertType,
                    ComponentIndex   = data.DataIndex,
                    ComponentContent = container.SubBytes(currentIndex, currentIndex + data.DataLength)
                };

                currentIndex += data.DataLength;

                package.AppendData(component);
            }
        }
        /// <summary>
        /// 解码自由组合数据段
        /// </summary>
        /// <param name="package"></param>
        /// <param name="container"></param>
        private void DecodeRandomData(IProtocolPackage <byte[]> package, byte[] container)
        {
            var currentIndex = 0;

            while (currentIndex < container.Length)
            {
                var dataIndex = currentIndex + 2;

                var flagIndex = currentIndex + 1;

                var dataFlag = container[flagIndex] & 0x7F; //0x7F = 0111 1111

                var data = package.Command.CommandDatas.First(obj => obj.DataFlag == dataFlag);

                if (currentIndex + data.DataLength > container.Length)
                {
                    package.Status = PackageStatus.NoEnoughBuffer;
                    return;
                }

                var channel = container[currentIndex];

                var component = new PackageComponent <byte[]>
                {
                    ComponentName    = $"{data.DataName}-{channel}",
                    ComponentChannel = channel,
                    CommandData      = data,
                    DataValueType    = data.DataValueType,
                    DataType         = data.DataConvertType,
                    ComponentIndex   = data.DataIndex,
                    ValidFlag        = container[flagIndex],
                    ComponentContent = container.SubBytes(dataIndex, dataIndex + data.DataLength)
                };

                currentIndex = data.DataLength + dataIndex;

                package.AppendData(component);
            }
        }
Exemple #4
0
        protected override void DecodeCommand(IProtocolPackage <string> package)
        {
            var container = package[StructureNames.Data].ComponentContent;

            container = container.Replace("CP=&&", string.Empty).Replace("&&", string.Empty);

            var dataGroups = container.Split(';');

            var commandDataDicts = (from dataGroup in dataGroups
                                    where dataGroup.Contains(",")
                                    from data in dataGroup.Split(',')
                                    select data.Split('='))
                                   .ToDictionary(dataKeyValuePair => dataKeyValuePair[0], dataKeyValuePair => dataKeyValuePair[1]);

            foreach (var commandDataDic in commandDataDicts)
            {
                var commandData = package.Command.CommandDatas.FirstOrDefault(obj => obj.DataName == commandDataDic.Key);
                if (commandData == null)
                {
                    continue;
                }
                var component = new PackageComponent <string>
                {
                    ComponentName    = commandDataDic.Key,
                    DataType         = commandData.DataConvertType,
                    ComponentIndex   = commandData.DataIndex,
                    ComponentContent = commandDataDic.Value
                };
                component.ComponentValue = component.ComponentContent;

                package.AppendData(component);
            }

            package.DeviceNodeId = package[StructureNames.NodeId].ComponentValue;

            package.Finalization();
        }