Ejemplo n.º 1
0
 /// <summary>
 /// Close all ports.
 /// </summary>
 /// <param name="port">Port to close.</param>
 internal static void Close(PortDto port)
 {
     using (var ctx = new PortDtoFactory())
     {
         ctx.Close(port);
     }
 }
 /// <summary>
 /// Close messaging.
 /// </summary>
 /// <param name="port">Port to close..</param>
 internal static void Close(PortDto port)
 {
     if (Queue != null)
     {
         Queue.TaskContinue = false;
         Queue.TaskReceive = null;
     }
     PortBusiness.Close(port);
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Get the next complete message.
        /// </summary>
        /// <param name="port">Port to use.</param>
        /// <return>Received message.</return>
        internal static MessageDto Receive(PortDto port)
        {
            MessageDto result;
            do
            {
                using (var ctx = new PortDtoFactory())
                {
                    result = ctx.Receive(port);
                }
            } while (result.IsComplete == false && result.Size != 0);

            return result;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Close a port.
        /// </summary>
        /// <param name="port">Port to close.</param>
        /// <returns>Closed port.</returns>
        public PortDto Close(PortDto port)
        {
            try
            {
                port.RawPort?.Close();
                port.IsOpen = false;
            }
            catch
            {
                port.IsOpen = false;
            }

            return port;
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Connect a port.
        /// </summary>
        /// <param name="port">Port to connect.</param>
        /// <returns>Connected port.</returns>
        public PortDto Connect(PortDto port)
        {
            try
            {
                port.RawPort = new SerialPort(port.Name)
                {
                    BaudRate = 115200,
                    ReceivedBytesThreshold = 1 // API flush when 1 octet is in send buffer
                };
                port.RawPort.Open();
                port.IsOpen = port.RawPort.IsOpen;
            }
            catch
            {
                port.IsOpen = false;
            }

            return port;
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Connect to port.
        /// </summary>
        /// <param name="port">Port to use.</param>
        /// <returns>Port.</returns>
        internal static PortDto Connect(PortDto port)
        {
            var portName = port.Name?.ToUpperInvariant() ?? string.Empty;

            if (!string.IsNullOrEmpty(portName))
            {
                if (port.IsOpen == false)
                {
                    using (var ctx = new PortDtoFactory())
                    {
                        port = ctx.Connect(port);
                    }
                }
            }
            else
            {
                port.IsOpen = false;
            }

            return port;
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Try to get next data.
        /// </summary>
        /// <param name="port">Port to use.</param>
        /// <returns>Completed message.</returns>
        public MessageDto Receive(PortDto port)
        {
            ReadBytes(port.RawPort).ForEach(port.ReadBuffer.Enqueue);

            // reinitialize message
            if (port.ReceiveMessage.IsComplete)
            {
                port.ReceiveMessage = new MessageDto();
            }

            // complete message
            if (port.ReadBuffer.Count != 0)
            {
                port.ReceiveMessage = BuildMessage(port);
            }

            return port.ReceiveMessage;
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Build the received message.
        /// </summary>
        /// <param name="port">Port to use.</param>
        /// <returns>Recevied message.</returns>
        private MessageDto BuildMessage(PortDto port)
        {
            var buffer = port.ReadBuffer;
            var message = port.ReceiveMessage;

            // new message
            if (message.Content.Count == 0)
            {
                message.Content.Add(buffer.Dequeue());
            }

            // create a frame
            message.Header = (MessageHeader)message.Content[0];
            if (message.Header == MessageHeader.StartOfFrame)
            {
                // message size
                if (message.Size == 0 && buffer.Count > 0)
                {
                    message.Content.Add(buffer.Dequeue());
                    message.Size = message.Content[1] + 2; // frame size + header byte + size byte
                }

                // message data
                while (message.Content.Count != message.Size && buffer.Count != 0)
                {
                    message.Content.Add(buffer.Dequeue());
                }
                message.IsComplete = message.Content.Count == message.Size;
            }
            // create acknowledgment
            else if (message.Header == MessageHeader.Acknowledgment || message.Header == MessageHeader.NotAcknowledgment)
            {
                message.IsComplete = true;
            }
            if (message.IsComplete) TraceHelper.TraceFrame(message.Content, false);

            return message;
        }
Ejemplo n.º 9
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;
        }
Ejemplo n.º 10
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;
        }