Example #1
0
File: SendPart.cs Project: wpmyj/c3
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public byte[] ToBytes(DataFieldValueProvider valueProvider)
        {
            if (valueProvider == null)
            {
                throw new ArgumentNullException("valueProvider");
            }

            return(this.DataFieldManager.ToBytes(valueProvider));
        }
Example #2
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public byte[] ToBytes(DataFieldValueProvider valueProvider)
        {
            if (valueProvider == null)
            {
                throw new ArgumentNullException("valueProvider");
            }

            MemoryStream ms = new MemoryStream();

            foreach (DataField df in this.DataFields)
            {
                // 确保df数据写到正确的位置
                //
                if (ms.Length != df.BeginPosition)
                {
                    string errmsg = string.Format(
                        "write datafield '{0}' data being position error, expect: {1}, but was: {2}",
                        df.Name, df.BeginPosition, ms.Length);

                    throw new InvalidDataException(errmsg);
                }

                if (!df.IsCRC)
                {
                    if (df.IsLazy)
                    {
                        df.Value = valueProvider.GetValue(df.Name);
                    }

                    byte[] bs = df.Bytes;
                    if (bs == null)
                    {
                        string errmsg = string.Format("datafield '{0}' Bytes null",
                                                      df.Name);
                        throw new InvalidOperationException(errmsg);
                    }
                    ms.Write(bs, 0, (int)bs.Length);
                }
                else
                {
                    byte[] bs = ms.ToArray();
                    // 2011-12-30 crc begin position
                    //
                    //byte[] crcbs = this.CRCer.Calc(bs, 0, bs.Length);
                    int calcLen = bs.Length - this.CRCBegin;
                    CheckCRCLength(calcLen);
                    byte[] crcbs = this.CRCer.Calc(bs, this.CRCBegin, calcLen);

                    df.Bytes = crcbs;
                    ms.Write(crcbs, 0, (int)crcbs.Length);
                }
            }
            return(ms.ToArray());
        }
Example #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="device"></param>
        /// <returns></returns>
        public override byte[] OnCreateSendBytes(IDevice device)
        {
            //TODO: address string
            //
            if (this.SendPart.IsNeedAddress)
            {
                if (this.SendPart.DataFieldManager[ADDRESS] != null)
                {
                    this.SendPart[ADDRESS] = device.Address;
                }
            }

            DataFieldValueProvider valueProvider = new DataFieldValueProvider(device);

            byte[] bytes = this.SendPart.ToBytes(valueProvider);

            return(bytes);
        }