Exemplo n.º 1
0
        /// <summary>
        /// Generates binary image of the <see cref="SerializableMeasurement"/> and copies it into the given buffer, for <see cref="ISupportBinaryImage.BinaryLength"/> bytes.
        /// </summary>
        /// <param name="buffer">Buffer used to hold generated binary image of the source object.</param>
        /// <param name="startIndex">0-based starting index in the <paramref name="buffer"/> to start writing.</param>
        /// <returns>The number of bytes written to the <paramref name="buffer"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <paramref name="startIndex"/> or <see cref="ISupportBinaryImage.BinaryLength"/> is less than 0 -or-
        /// <paramref name="startIndex"/> and <see cref="ISupportBinaryImage.BinaryLength"/> will exceed <paramref name="buffer"/> length.
        /// </exception>
        /// <remarks>
        /// <para>
        /// Field:      Bytes:   <br/>
        /// ---------   ---------<br/>
        ///  Key ID         4    <br/>
        /// SourceLen       4    <br/>
        ///  Source     SourceLen<br/>
        /// Signal ID      16    <br/>
        ///  TagLen         4    <br/>
        ///   Tag        TagLen  <br/>
        ///   Value         8    <br/>
        ///   Adder         8    <br/>
        /// Multiplier      8    <br/>
        ///   Ticks         8    <br/>
        ///   Flags         4    <br/>
        /// </para>
        /// <para>
        /// Constant Length = 64<br/>
        /// Variable Length = SourceLen + TagLen
        /// </para>
        /// </remarks>
        public int GenerateBinaryImage(byte[] buffer, int startIndex)
        {
            int length = BinaryLength;

            buffer.ValidateParameters(startIndex, length);

            byte[] bytes;
            int    size, index = startIndex;
            string source  = Key.Source.ToNonNullString();
            string tagName = TagName.ToNonNullString();

            // Encode key ID
            BigEndian.CopyBytes(Key.ID, buffer, index);
            index += 4;

            // Encode key source string length
            bytes = m_encoding.GetBytes(source);
            size  = bytes.Length;
            BigEndian.CopyBytes(size, buffer, index);
            index += 4;

            // Encode key source string
            if (size > 0)
            {
                Buffer.BlockCopy(bytes, 0, buffer, index, size);
                index += size;
            }

            // Encode signal ID
            EndianOrder.BigEndian.CopyBytes(ID, buffer, index);
            index += 16;

            // Encode tag name string length
            bytes = m_encoding.GetBytes(tagName);
            size  = bytes.Length;
            BigEndian.CopyBytes(size, buffer, index);
            index += 4;

            // Encode tag name string
            if (size > 0)
            {
                Buffer.BlockCopy(bytes, 0, buffer, index, size);
                index += size;
            }

            // Encode value
            BigEndian.CopyBytes(Value, buffer, index);
            index += 8;

            // Encode adder
            BigEndian.CopyBytes(Adder, buffer, index);
            index += 8;

            // Encode multiplier
            BigEndian.CopyBytes(Multiplier, buffer, index);
            index += 8;

            // Encode timestamp
            BigEndian.CopyBytes((long)Timestamp, buffer, index);
            index += 8;

            // Encode state flags
            BigEndian.CopyBytes((uint)StateFlags, buffer, index);

            return(length);
        }