/// <summary>
        /// Convert a serial mesage to business message.
        /// </summary>
        /// <param name="message">Serial message.</param>
        /// <returns>Business message.</returns>
        public MessageFromDto Convert(MessageDto message)
        {
            var result = new MessageFromDto {Header = message.Header};

            if (message.Header == MessageHeader.StartOfFrame)
            {
                result.IsValid = ValidMessage(message.Content);
                if (result.IsValid && message.Content.Count > 3)
                {
                    result.Type = (MessageType)message.Content[2];
                    result.Command = (MessageCommand)message.Content[3];

                    if (result.Type == MessageType.Request && message.Content.Count > 8)
                    {
                        result.ZIdentifier = message.Content[5];
                        result.RequestCommand = (RequestCommandClass) message.Content[7];
                        result.Content = message.Content.Skip(8).Take(message.Content[6] - 1).ToList();
                    }
                    else if (message.Content.Count > 4)
                    {
                        result.Content = message.Content.Skip(4).Take(message.Content.Count - 5).ToList();
                    }
                }
            }
            else
            {
                result.IsValid = true;
                result.Type = MessageType.Response;
                result.Content = message.Content;
            }

            return result;
        }
        /// <summary>
        /// Create message for serial port.
        /// </summary>
        /// <param name="message">Message.</param>
        /// <returns>Message frame.</returns>
        public MessageDto Convert(MessageToDto message)
        {
            var content = CreateFrame(message);
            var result = new MessageDto
            {
                Header = (MessageHeader)content[0],
                Content = content,
                IsComplete = true,
                Size = content.Count
            };

            return result;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Send a message.
        /// </summary>
        /// <param name="port">Port to use.</param>
        /// <param name="message">Message to send.</param>
        /// <returns>Send result.</returns>
        public bool Send(PortDto port, MessageDto message)
        {
            bool result;

            try
            {
                port.RawPort.Write(message.Content.ToArray(), 0, message.Content.Count);
                TraceHelper.TraceFrame(message.Content, true);
                result = true;
            }
            catch
            {
                result = false;
            }

            return result;
        }
 /// <summary>
 /// Send an acknowledgment.
 /// </summary>
 /// <param name="messageProcess">Message processing to use.</param>
 private static void SendAcknowledgment(MessageProcessDto messageProcess)
 {
     Thread.Sleep(MessageConstants.WaitForRoundTimeTrip);
     var message = new MessageDto
     {
         Content = new List<byte>
         {
             messageProcess.MessageFrom.IsValid ? (byte) MessageHeader.Acknowledgment : (byte) MessageHeader.NotAcknowledgment
         }
     };
     PortBusiness.Send(messageProcess.Controller.Port, message);
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Send a message.
        /// </summary>
        /// <param name="port">Port to use containing message.</param>
        /// <param name="message">Message to send.</param>
        /// <returns>Send result.</returns>
        internal static bool Send(PortDto port, MessageDto message)
        {
            bool result;

            using (var ctx = new PortDtoFactory())
            {
                result = ctx.Send(port, message);
            }

            return result;
        }