Пример #1
0
 /// <summary>
 /// This method fires when a message has been completed.
 /// </summary>
 /// <param name="e"> The message event args. </param>
 protected virtual void FireOnMessageCompleted(ByteMessageEventArgs e)
 {
     if (this.OnMessageCompleted != null)
     {
         this.OnMessageCompleted(this, e);
     }
 }
Пример #2
0
        /// <summary>
        /// This method gets the whole message.
        /// </summary>
        /// <param name="e"> The byte message. </param>
        /// <param name="index"> The start index of the header. </param>
        /// <param name="length"> The start of the message. </param>
        private void GetMessage(ByteMessageEventArgs e, int index, int length)
        {
            int tempLength = this.messageLength;

            for (int i = 0; i < tempLength; i++)
            {
                if (i + index + length < e.Message.Length)
                {
                    this.messageArray.Add(e.Message[i + index + length]);
                    this.fullMessage.Add(e.Message[i + index + length]);
                    this.messageLength--;
                }
            }

            if (this.messageLength == 0)
            {
                if (this.IsCheckSumCorrect())
                {
                    this.messageArray.RemoveAt(this.messageArray.Count - 1);
                    this.FireOnMessageCompleted(new ByteMessageEventArgs(this.messageArray.ToArray()));
                }

                this.fullMessage.Clear();
                this.headerFound = false;
                this.messageArray.Clear();
            }
        }
Пример #3
0
 /// <summary>
 /// This method gets a byte  array and then looks for either the header or terminator.
 /// </summary>
 /// <param name="sender"> The object sender. </param>
 /// <param name="e"> The message event args. </param>
 public void BuildMessage(object sender, ByteMessageEventArgs e)
 {
     if (!this.headerFound)
     {
         this.LookForHeader(e);
     }
     else
     {
         this.GetMessage(e, 0, 0);
     }
 }
Пример #4
0
        /// <summary>
        /// This method creates the message length.
        /// </summary>
        /// <param name="e"> The message. </param>
        /// <param name="index"> The index of the message. </param>
        private void SearchForLength(ByteMessageEventArgs e, int index)
        {
            List <byte> lengthArray = new List <byte>();

            for (int i = index + 8; i < 100; i++)
            {
                lengthArray.Add(e.Message[i]);
            }

            this.fullMessage.AddRange(lengthArray);
            this.messageLength = BitConverter.ToInt32(lengthArray.ToArray(), 0) + 1;
        }
Пример #5
0
 /// <summary>
 /// This method looks for a header in the message.
 /// </summary>
 /// <param name="e"> The byte message. </param>
 private void LookForHeader(ByteMessageEventArgs e)
 {
     for (int i = 0; i < e.Message.Length; i++)
     {
         byte[] searchbuffer = this.CopyByte(e.Message, i, 8);
         if (this.header.SequenceEqual(searchbuffer))
         {
             this.fullMessage.AddRange(this.header);
             this.headerFound = true;
             this.SearchForLength(e, i);
             this.GetMessage(e, i, 108);
             return;
         }
     }
 }