/// <summary>
        /// This is the thread which handles sending messages to kepler
        /// </summary>
        private void KeplerTransmitter()
        {
            KeplerCommand CurrentCommand = null;

            while (KeplerSerialPort.IsOpen)
            {
                //Check for task canellation
                if (ReceiverCancellationToken.IsCancellationRequested)
                {
                    return;
                }
                //If the tx queue isn't empty
                if (!TransmitterQueue.IsEmpty)
                {
                    //Try and get a message from the non-empty queue
                    var success = TransmitterQueue.TryDequeue(out CurrentCommand);

                    if (success && CurrentCommand != null)
                    {
                        //Write the message to the serial port. .NET handes the rest.
                        try
                        {
                            KeplerSerialPort.Write(CurrentCommand.Message, 0, CurrentCommand.Message.Length);
                        }catch (Exception e)
                        {
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Sens a messsage to kepler
        /// </summary>
        /// <param name="Command">KeplerCommand object containing the message to be sent</param>
        public void SendMessage(KeplerCommand Command)
        {
#if DEBUG
            Debug.WriteLine(DateTime.UtcNow + " -- Sent: " + Helpers.ByteArrayToString(Command.Message));
#endif
            //Add the command to the tx queue
            TransmitterQueue.Enqueue(Command);
        }
Пример #3
0
 /// <summary>
 /// Creates a new periodic message
 /// </summary>
 /// <param name="Message">Message to send</param>
 /// <param name="Protocol">Protocol to use</param>
 /// <param name="TimeInterval">Time between messages</param>
 public PeriodicMessage(KeplerCommand Message, IProtocol Protocol, double TimeInterval)
 {
     //Set the protocol
     this.Protocol = Protocol;
     //Set the single message (J2534 Compatability)
     this.Message = Message;
     //Create the TX timer
     PeriodicMessageTimer = new Timer(TimeInterval);
     //Set the event handler
     PeriodicMessageTimer.Elapsed += PeriodicMessageTimer_Elapsed;
     //Write the message to the TX queue
     Protocol.WriteMessages(new List <KeplerCommand>()
     {
         Message
     });
     //Start the timer
     PeriodicMessageTimer.Start();
 }
 public InterfaceErrorEventArgs(KeplerCommand ErrorMessage)
 {
     this.ErrorMessage = ErrorMessage;
 }
        /// <summary>
        /// This is the thread which handles receiving messages from kepler
        /// </summary>
        private void KeplerReceiver()
        {
            while (KeplerSerialPort.IsOpen)
            {
                try
                {
                    //Check for task canellation
                    if (TransmitterCancellationToken.IsCancellationRequested)
                    {
                        return;
                    }
                    //Get a byte

                    //TODO: CHECK THIS! THREAD SAFE?? HAMMERING SERIAL??
                    var CurrByte = KeplerSerialPort.ReadByte();
                    //Check if start byet
                    if (CurrByte == Properties.StartByte)
                    {
                        //Build Kepler haeder
                        var IncomingCommand = new KeplerCommand();
                        var LengthHigh      = KeplerSerialPort.ReadByte();
                        var LengthLow       = KeplerSerialPort.ReadByte();
                        var Length          = (short)((LengthHigh << 8) | LengthLow);
                        IncomingCommand.Message = new byte[Length];
                        //Get the rest of the message

                        //TODO: TIME OUT?
                        KeplerSerialPort.Read(IncomingCommand.Message, 0, Length);

                        //Add timestamp
                        IncomingCommand.Timestamp = (DateTime.UtcNow - StartTime).TotalMilliseconds * 1000;
                        if (IncomingCommand.Message[0] == 0xAA)
                        {
                            var    Protocol      = IncomingCommand.Message[1];
                            byte[] ActualMessage = new byte[IncomingCommand.Message.Length - 2];
                            Buffer.BlockCopy(IncomingCommand.Message, 2, ActualMessage, 0, ActualMessage.Length);
                            IncomingCommand.Message        = ActualMessage;
                            IncomingCommand.DataSize       = ActualMessage.Length;
                            IncomingCommand.ExtraDataIndex = ActualMessage.Length;
                            //Add it to the RX queue

                            switch (Protocol & 0x03)
                            {
                            case 1:
                                IncomingCommand.ProtocolID = 1;
                                try
                                {
                                    kepler.OpenChannels[(int)IncomingCommand.ProtocolID].ReceiverQueue.Enqueue(IncomingCommand);
                                }
                                catch (Exception e)
                                {
                                    Debug.WriteLine("Channel 1 Not found");
                                    //Channel not found error
                                    //TODO: HANDLE CHANNEL NOT OPEN
                                }
                                break;

                            case 2:
                                IncomingCommand.ProtocolID = 5;
                                try
                                {
                                    kepler.OpenChannels[(int)IncomingCommand.ProtocolID].ReceiverQueue.Enqueue(IncomingCommand);
                                }
                                catch (Exception e)
                                {
                                    Debug.WriteLine("Channel 5 Not found");

                                    //Channel not found error
                                    //TODO: HANDLE CHANNEL NOT OPEN
                                }
                                break;

                            case 3:
                                IncomingCommand.ProtocolID = 6;
                                try
                                {
                                    kepler.OpenChannels[(int)IncomingCommand.ProtocolID].ReceiverQueue.Enqueue(IncomingCommand);
                                }
                                catch (Exception e)
                                {
                                    Debug.WriteLine("Channel 6 Not found");

                                    //Channel not found error
                                    //TODO: HANDLE CHANNEL NOT OPEN
                                }
                                break;
                            }
                        }
                        else
                        {
                            IncomingCommand.ProtocolID = 0;
                            try
                            {
                                if (IncomingCommand.DataSize >= 4 && IncomingCommand.Message[3] == 0xEF)
                                {
                                    (kepler.OpenChannels[(int)IncomingCommand.ProtocolID] as Protocols.SystemProtocol).ErrorRecieved(IncomingCommand);
                                }
                                else
                                {
                                    kepler.OpenChannels[(int)IncomingCommand.ProtocolID].ReceiverQueue.Enqueue(IncomingCommand);
                                }
                            }
                            catch (Exception e)
                            {
                                //Channel not found error
                            }
                        }
                        //  ReceiverQueue.Enqueue(IncomingCommand);

#if DEBUG
                        Debug.WriteLine(DateTime.UtcNow + " Queued Message from " + kepler.OpenChannels[(int)IncomingCommand.ProtocolID].ProtocolName + " " + Helpers.ByteArrayToString(IncomingCommand.Message));
                        // Debug.WriteLine(ReceiverQueue.Count + " messages in RX queue");
#endif
                        //Notify we got a message (Message not attached to event as it is in the queue)
                        OnMessageReceived(this, new KeplerEventArgs {
                            ProtocolID = (int)IncomingCommand.ProtocolID
                        });
                    }
                }
                catch (Exception e)
                {
                    Debug.WriteLine("Serial Port Error");
                    //Serial Port Error
                    //TODO: SERIAL PORT HANDLE ERROR
                }
            }
        }