Exemple #1
0
        public byte RequestStatus(byte boardID = 0)
        {
            var message = new ADMMessage(MessageTags.CreateTag());

            message.Type     = Messaging.MessageType.STATUS_REQUEST;
            message.TargetID = boardID;
            SendMessage(message);
            return(message.Tag);
        }
Exemple #2
0
        public byte Ping()
        {
            var message = new ADMMessage(MessageTags.CreateTag());

            message.Type     = Messaging.MessageType.PING;
            message.TargetID = 0;
            SendMessage(message);
            return(message.Tag);
        }
Exemple #3
0
        public byte Initialise()
        {
            ADMMessage message = new ADMMessage(MessageTags.CreateTag());

            message.TargetID = 0;
            message.Type     = Messaging.MessageType.INITIALISE;
            SendMessage(message);
            return(message.Tag);
        }
Exemple #4
0
        public byte SendCommand(byte targetID, ArduinoCommand command, List <Object> extraArgs = null, byte tag = 0)
        {
            var message = new ADMMessage();

            message.LittleEndian = LittleEndian;
            message.Type         = Messaging.MessageType.COMMAND;
            message.TargetID     = targetID;
            message.Tag          = tag == 0 ? MessageTags.CreateTag() : tag;
            message.CommandID    = command.ID;

            List <Object> allArgs = command.Arguments;

            if (extraArgs != null && extraArgs.Count > 0)
            {
                allArgs.AddRange(extraArgs);
            }

            foreach (Object arg in allArgs)
            {
                byte[] b;
                if (arg is String)
                {
                    b = Chetch.Utilities.Convert.ToBytes((String)arg);
                }
                else if (arg.GetType().IsValueType)
                {
                    b = Chetch.Utilities.Convert.ToBytes((ValueType)arg, LittleEndian);
                }
                else
                {
                    throw new Exception("Unable to process type " + arg.GetType());
                }
                message.AddArgument(b);
            }

            return(SendMessage(message));
        }
Exemple #5
0
        public byte SendMessage(ADMMessage message)
        {
            if (message == null)
            {
                return(0);
            }

            if (Monitor.TryEnter(SendMessageLock, SEND_MESSAGE_LOCK_TIMEOUT))
            {
                try
                {
                    bool ready2send;
                    long msSinceLastSent = -1;

                    //loop waiting for a message response
                    do
                    {
                        msSinceLastSent = (DateTime.Now.Ticks - LastMessageSentOn.Ticks) / TimeSpan.TicksPerMillisecond;
                        if (LastMessageSent == null || MessageReceivedSuccess)
                        {
                            ready2send = true;
                        }
                        else
                        {
                            ready2send = msSinceLastSent > MESSAGE_RECEIVED_TIMEOUT;
                            if (!ready2send)
                            {
                                _sleep(10); //crappy idea to reduce load on cpu
                            }
                            else
                            {
                                //so here we have waited long enough for the previous sent message which has failed to arrive
                                //so we proceed with sending anyway
                                Tracing?.TraceEvent(TraceEventType.Warning, 0, "SendMessage {0}: Sending {1} timed out waiting to receive message {2} tag {3}", PortAndNodeID, message.Type, LastMessageSent.Type, LastMessageSent.Tag);
                            }
                        }
                    } while (!ready2send);

                    //store old values before sending (rollback if exception thrown)
                    //This unusual approach is because it appeared like messages were being received before the SendString could exit
                    ADMMessage lastMessageSent   = LastMessageSent;
                    DateTime   lastMessageSentOn = LastMessageSentOn;
                    try
                    {
                        lock (MessageStatsLock)
                        {
                            MessagesSent++;
                            LastMessageSent        = message;
                            LastMessageSentOn      = DateTime.Now;
                            MessageReceivedSuccess = false;
                        }
                        if (message.Tag == 0)
                        {
                            message.Tag = MessageTags.CreateTag();
                        }
                        message.SenderID = BoardID;
                        //Console.WriteLine("-------------> {0}: Sending message {1} tag {2} target {3}." , PortAndNodeID, message.Type, message.Tag, message.TargetID);
                        SendString(message.Serialize());
                    }
                    catch (Exception e)
                    {
                        lock (MessageStatsLock) //rollback
                        {
                            MessagesSent--;
                            LastMessageSent   = lastMessageSent;
                            LastMessageSentOn = lastMessageSentOn;
                        }
                        String errMsg = String.Format("{0} SendMessage: sending {1} (Tag={2}) produced exception {3} {4}", PortAndNodeID, message.Type, message.Tag, e.GetType(), e.Message);
                        Tracing?.TraceEvent(TraceEventType.Error, 0, errMsg);
                        throw e;
                    }
                }
                finally
                {
                    Monitor.Exit(SendMessageLock);
                }
            } //end attempt to try get lock
            else
            {
                //we waited too long to get the lock...
                throw new SendFailedException("ArduinoDeviceManager::SendMessage ... waited to long to obtain lock");
            }
            return(message.Tag);
        }