Beispiel #1
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="df"></param>
 public void Remove(DataField df)
 {
     this.DataFieldManager.DataFields.Remove(df);
 }
Beispiel #2
0
        /// <summary>
        /// 检查bytes是否匹配dataFieldCollection
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public IParseResult CheckBytes(string receivePartName, byte[] bytes)
        {
            if (bytes == null)
            {
                IParseResult pr = new LengthErrorResult(receivePartName, this.DataFields.BytesLength, 0);
                pr.Name = receivePartName;
                return(pr);
            }

            // 1. check length
            //
            // 2010-06-08 return CheckLength max length while fail
            //
            //if (!this.DataFields.CheckLength(bytes))
            //    return new LengthErrorResult(receivePartName, this.DataFields.BytesLength, bytes.Length);
            int dfsBytesLength = this.DataFields.Length;

            if (bytes.Length < dfsBytesLength)
            {
                IParseResult pr = new LengthErrorResult(receivePartName, dfsBytesLength, bytes.Length);
                pr.Name = receivePartName;
                return(pr);
            }

            // 1.1 check match check bytes
            //
            foreach (DataField df in this.DataFields)
            {
                if (df.IsMatchCheck)
                {
                    log.Debug("MatchCheck datafield: {0}", df.Name);

                    byte[] bs = df.GetMatch(bytes);
                    log.Debug("Expected: {0}, Actual: {1}",
                              HexStringConverter.Default.ConvertToObject(df.Bytes),
                              HexStringConverter.Default.ConvertToObject(bs));

                    bool eq = BytesValueComparer.AreEqual(df.Bytes, bs);
                    if (!eq)
                    {
                        return(new DataErrorResult(receivePartName, df.Bytes, bs));
                    }
                }
            }
            // 2. check crc
            // get crcfg
            //
            DataField crcdf = this.DataFields.CRCDataField;

            if (crcdf != null)
            {
                // 2011-01-05 crc calc error
                //
                //
                // byte[] crcbs = this.CRCer.Calc(bytes, 0, this.DataFields.Length - crcdf.DataLength);
                int bsLengthForCRC = crcdf.BeginPosition;

                // 2011-12-30 crc calc begin position
                //
                //byte[] crcbs = this.CRCer.Calc(bytes, 0, bsLengthForCRC);
                int calcLen = crcdf.BeginPosition - this.CRCBegin;
                CheckCRCLength(calcLen);

                byte[] crcbs = this.CRCer.Calc(bytes, this.CRCBegin, calcLen);

                crcdf.Bytes = crcbs;
                if (!crcdf.Match(bytes, 0))
                {
                    return(new CRCErrorResult(receivePartName, crcbs, crcdf.GetMatch(bytes, 0)));
                }
            }

            // 3. check need match df
            //
            return(new SuccessResult(receivePartName));
        }
Beispiel #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="datafieldnode"></param>
        /// <returns></returns>
        static internal DataField CreateDataField(XmlNode datafieldnode)
        {
            string name   = string.Empty;
            string factor = string.Empty;

            object[]        convertArgs  = null;
            int             begin        = 0;
            int             length       = 0;
            IBytesConverter ibc          = null;
            bool            iscrc        = false;
            bool            isMatchCheck = false;

            byte[] bytes        = null;
            bool   littleEndian = true;
            bool   isLazy       = false;

            string str = string.Empty;

            XmlElement el = datafieldnode as XmlElement;

            name = GetAttribute(el, DeviceDefineNodeNames.DataFieldName);

            str   = GetAttribute(el, DeviceDefineNodeNames.Begin);
            begin = int.Parse(str);

            str    = GetAttribute(el, DeviceDefineNodeNames.Length);
            length = int.Parse(str);


            if (HasBytesConverterChildNode(datafieldnode))
            {
                XmlNode bytesConverterNode = datafieldnode.SelectSingleNode(
                    DeviceDefineNodeNames.BytesConverter);
                BytesConverterConfig cfg = CreateBytesConverterConfig(bytesConverterNode);
                ibc = GetBytesConverter(cfg);
            }
            else
            {
                // converter
                //
                str = GetAttribute(el, DeviceDefineNodeNames.Converter, false);
                //if (str == null || str.Length == 0)
                //{
                //    str = "Xdgk.Communi.OriginalConverter";
                //}

                //
                //
                factor = GetAttribute(el, DeviceDefineNodeNames.Factor, true);
                if (factor != null && factor.Length > 0)
                {
                    try
                    {
                        float n = Convert.ToSingle(factor);
                        convertArgs = new object[] { n };
                    }
                    catch (FormatException formatEx)
                    {
                        string s = string.Format("Invalid Factor '{0}'", factor);
                        throw new ConfigException(s, formatEx);
                    }
                }
                ibc = GetBytesConvert(str, convertArgs);
            }

            //
            //
            str = GetAttribute(el, DeviceDefineNodeNames.LittleEndian, true);
            if (str.Length > 0)
            {
                littleEndian = bool.Parse(str);
            }
            ibc.IsLittleEndian = littleEndian;

            str = GetAttribute(el, DeviceDefineNodeNames.Bytes, true);
            if (str != null && str.Length > 0)
            {
                bytes = HexStringConverter.Default.ConvertToBytes(str);
            }

            str = GetAttribute(el, DeviceDefineNodeNames.Crc, true);
            if (str != null && str.Length > 0)
            {
                iscrc = bool.Parse(str);
            }

            str = GetAttribute(el, DeviceDefineNodeNames.MatchCheck, true);
            if (str != null && str.Length > 0)
            {
                isMatchCheck = bool.Parse(str);
            }

            str = GetAttribute(el, DeviceDefineNodeNames.Lazy, true);
            if (str != null && str.Length > 0)
            {
                isLazy = bool.Parse(str);
            }

            DataField df = new DataField(name, begin, length, (IBytesConverter)ibc);

            df.IsMatchCheck = isMatchCheck;
            if (bytes != null)
            {
                df.Bytes = bytes;
            }

            if (df.IsMatchCheck &&
                (df.Bytes == null || df.Bytes.Length == 0))
            {
                throw new Exception("must set bytes while matchCheck == true");
            }
            df.IsCRC  = iscrc;
            df.IsLazy = isLazy;

            return(df);
        }