public override void Interprete(ARCPO_Packet pPacket)
        {
            char vKey = this.GetPacketKey(pPacket);

            switch (vKey) {
                case 'N':
                    RunNotepad();
                    break;
                case 'X':
                    RunCalculatrice();
                    break;
                case 'C':
                    RunCmd();
                    break;
                case 'Z':
                    RunCharmap();
                    break;
                default:
                    //is it a known custom command?
                    if (this.CommandRunDict.ContainsKey(vKey.ToString())) {
                        RunCustom(pPacket);
                    }
                    break;
            }
        }
        /// <summary>
        /// DEBUG purpose :Sends a packet and returns as string whatever is answered by arduino
        /// </summary>
        /// <param name="pPacket"></param>
        /// <returns></returns>
        public string DEBUG_SendPacketAndReturnLineContent(ARCPO_Packet pPacket)
        {
            pPacket.mExpectAcknowledge = true;
            bool vSent = pPacket.SendPacket(this.mPort);

            Thread.Sleep(1000);
            if (this.mPort.BytesToRead > 0)
            {
                return(this.mPort.ReadExisting());
            }
            else
            {
                return(null);
            }
        }
        /// <summary>
        /// Body of the thread that polls the messages.
        /// It will poll data EVEN IF nobody is registered on the event!
        /// </summary>
        protected void PollMessageThread(object pParam)
        {
            while (this.mPollMessages)
            {
                if (!this.mWaitingForAck)
                {
                    //if not waiting for a ACK
                    //small probabily of thread collision, but let's see like that first... (should lock the variable before)
                    lock (mWaitingForAckSynchroKey) {
                        if (!this.mWaitingForAck)
                        {
                            //synchro : lock the line
                            lock (this.mPollingMessageSynchroKey) {
                                try {
                                    bool         vReceiveOk = false;
                                    ARCPO_Packet vPacket    = new ARCPO_Packet();
                                    vReceiveOk = vPacket.ReadPacket(this.mPort);

                                    if (vReceiveOk && this.PacketReceived != null)
                                    {
                                        //received + someone waiting for notification : raise the event
                                        ARCPO_ReceivedEventArgs vE = new ARCPO_ReceivedEventArgs(vPacket);
                                        this.PacketReceived(this, vE);
                                    }
                                }
                                catch (Exception ex) {
                                    //nothing (for now)
                                }
                            }
                        }
                    }
                }

                //get some rest
                Thread.Sleep(RECEIVE_POLLING_IN_MS);
            }
        }
Пример #4
0
        private void SendPacketLed(int pLed)
        {
            ARCPO_Packet vP = new ARCPO_Packet();
            int vPacketId = mPacketCounter;
            vP.mType = 199;
            vP.mID = (byte)(mPacketCounter++ % 256);
            vP.mSubType = (byte)pLed;
            vP.mExpectAcknowledge = ckbAck.Checked; ;
            vP.ContentString = "Led : " + pLed;

            this.SendPacket(vP);
        }
Пример #5
0
        private void SendPacket(ARCPO_Packet pP)
        {
            bool vSentSuccess = false;

            for (int i = 0; !vSentSuccess && i < 3; i++) {
                textBox1.Text += ">>Sent message " + pP.mID + " : ";

                vSentSuccess = mConnector.SendPacket(pP);

                textBox1.Text += vSentSuccess + "\r\n";
            }
        }
Пример #6
0
        private void SendPacket(string pContent, byte pType, byte pSubType)
        {
            ARCPO_Packet vP = new ARCPO_Packet();
            int vPacketId = mPacketCounter;
            vP.mType = pType;
            vP.mID = (byte)(mPacketCounter++ % 256);
            vP.mSubType = pSubType;
            vP.mExpectAcknowledge = ckbAck.Checked; ;
            vP.ContentString = pContent;

            this.SendPacket(vP);
        }
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="pPacket"></param>
 public ARCPO_ReceivedEventArgs(ARCPO_Packet pPacket)
 {
     this.mReceivedTime = DateTime.Now;
     this.mPacket       = pPacket;
 }
        public override void Interprete(ARCPO_Packet pPacket)
        {
            //is it a known custom command?
            if (this.CommandRunDict.ContainsKey(this.GetPacketKey(pPacket).ToString())) {
                CommandRun vCR = this.CommandRunDict[this.GetPacketKey(pPacket).ToString()];

                //Idea: use name (dictionnary of known values) and fallback interpretes ... later.
                this.SendWMAppCommand(Convert.ToInt32(vCR.mCommand));
            }
        }
Пример #9
0
        private bool SendPacket(ARCPO_Packet pP)
        {
            bool vSentSuccess = false;

            //for (int i = 0; !vSentSuccess && i < 3; i++)
            //{
                vSentSuccess = mConnector.SendPacket(pP);
            //}

            return vSentSuccess;
        }
Пример #10
0
 /// <summary>
 /// DEBUG purpose :Sends a packet and returns as string whatever is answered by arduino
 /// </summary>
 /// <param name="pPacket"></param>
 /// <returns></returns>
 public string DEBUG_SendPacketAndReturnLineContent(ARCPO_Packet pPacket)
 {
     pPacket.mExpectAcknowledge = true;
     bool vSent = pPacket.SendPacket(this.mPort);
     Thread.Sleep(1000);
     if (this.mPort.BytesToRead > 0)
     {
         return this.mPort.ReadExisting();
     }
     else {
         return null;
     }
 }
 /// <summary>
 /// Returns the key to use to identify the intepreter
 /// </summary>
 /// <param name="pPacket"></param>
 /// <returns></returns>
 protected char GetPacketKey(ARCPO_Packet pPacket)
 {
     //is a ardTouch packet ?
     if (pPacket.mType == Constants.ARDTOUCH_TYPE && pPacket.mSubType == Constants.ARDTOUCH_SUBTYPE) {
         //ardTouch packet : the key is the first char of the body
         return (pPacket.mContent!= null && pPacket.mContent.Length > 0 ? pPacket.ContentString[0] : (char)0);
     }
     else {
         //generic, use type
         return (char)pPacket.mType;
     }
 }
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="pPacket"></param>
 public ARCPO_ReceivedEventArgs(ARCPO_Packet pPacket)
 {
     this.mReceivedTime = DateTime.Now;
     this.mPacket = pPacket;
 }
 public abstract void Interprete(ARCPO.ARCPO_Packet pPacket);
        /// <summary>
        /// Runs an extended command, where the action is not defined only by the command type, but by also other data within the packet
        /// </summary>
        /// <param name="pPacket"></param>
        private void RunExtendedCommand(CommandRun pCR, ARCPO_Packet pPacket)
        {
            //search for the matching commandExt
            CommandRunExt vCRE = null;
            foreach (CommandRunExt vIter in pCR.mSubCommands) {
                if (
                    (string.IsNullOrEmpty(vIter.mPacketSubtype) || vIter.mPacketSubtype[0] == (char)pPacket.mSubType)
                    &&
                    (string.IsNullOrEmpty(vIter.mPacketBody) || vIter.mPacketBody == pPacket.ContentString)
                    ) {
                    //found
                    vCRE = vIter;
                    break;
                }
            }

            if (vCRE != null) {
                RunCustom(vCRE);
            }
        }
        /// <summary>
        /// Runs a custom command (dispatch to submethods according packet type)
        /// </summary>
        /// <param name="commandRun"></param>
        private void RunCustom(ARCPO_Packet pPacket)
        {
            CommandRun vCR = this.CommandRunDict[this.GetPacketKey(pPacket).ToString()];

            if (vCR.IsExtended) {
                this.RunExtendedCommand(vCR, pPacket);
            }
            else {
                this.RunCustom(vCR);
            }
        }
Пример #16
0
 /// <summary>
 /// Logs a message
 /// </summary>
 /// <param name="pTime"></param>
 /// <param name="pReceivedOrSent">true received, false sent</param>
 /// <param name="pPacket"></param>
 public void LogMessage(DateTime pTime, bool pReceivedOrSent, ARCPO_Packet pPacket)
 {
     txbLog.Text = (pReceivedOrSent ? "[>I N] " : "[<OUT] ") + pTime.ToLongTimeString() + " : " + pPacket.ToString() + "\r\n" + txbLog.Text;
 }
Пример #17
0
        /// <summary>
        /// Sends a packet, retrying in case it fails and returns if ok
        /// </summary>
        /// <param name="pPacket"></param>
        /// <returns></returns>
        public bool SendPacket(ARCPO_Packet pPacket)
        {
            if (!this.IsSerialConnected() && !this.ConnectSerial())
            {
                return(false);
            }

            bool vOk       = false;
            int  vTryCount = 0;

            while (!vOk && vTryCount < this.RetryCount)
            {
                //force the expect acknowledge except if no retry asked
                pPacket.mExpectAcknowledge = (RetryCount > 0 ? true : pPacket.mExpectAcknowledge);

                //try to make sure we release the wait for ack flag
                try {
                    if (pPacket.mExpectAcknowledge)
                    {
                        lock (mWaitingForAckSynchroKey) {
                            this.mWaitingForAck = true;
                        }
                    }

                    bool vSent = pPacket.SendPacket(this.mPort);

                    if (vSent && pPacket.mExpectAcknowledge)
                    {
                        //locks the line
                        lock (this.mPollingMessageSynchroKey) {
                            #region get the acknowledgement packet
                            //tries n times to read a acknowledge
                            while (!vOk && vTryCount < this.RetryCount)
                            {
                                //wait longer and longer
                                Thread.Sleep(100 * vTryCount);

                                bool vAckOk = false;
                                try {
                                    ARCPO_Packet vAck = new ARCPO_Packet();
                                    vAckOk = vAck.ReadPacket(this.mPort);

                                    if (vAckOk && vAck.mID == pPacket.mID && vAck.ContentString != null && vAck.ContentString.StartsWith("ACK"))
                                    {
                                        vOk = true;
                                    }
                                }
                                catch (TimeoutException) { }

                                vTryCount++;
                            }
                            #endregion
                        }
                    }
                    else
                    {
                        //retry
                    }
                }
                catch (InvalidOperationException) {
                    //disconnected ?
                    this.DisconnectSerial();
                }
                finally {
                    //anycases, release the line
                    this.mWaitingForAck = false;
                }

                //count the tries
                vTryCount++;
            }

            return(vOk);
        }
Пример #18
0
        /// <summary>
        /// Sends a packet, retrying in case it fails and returns if ok
        /// </summary>
        /// <param name="pPacket"></param>
        /// <returns></returns>
        public bool SendPacket(ARCPO_Packet pPacket)
        {
            if (!this.IsSerialConnected() && !this.ConnectSerial()) {
                return false;
            }

            bool vOk = false;
            int vTryCount = 0;

            while (!vOk && vTryCount < this.RetryCount) {
                //force the expect acknowledge except if no retry asked
                pPacket.mExpectAcknowledge = (RetryCount > 0 ? true : pPacket.mExpectAcknowledge);

                //try to make sure we release the wait for ack flag
                try {
                    if (pPacket.mExpectAcknowledge) {
                        lock (mWaitingForAckSynchroKey) {
                            this.mWaitingForAck = true;
                        }
                    }

                    bool vSent = pPacket.SendPacket(this.mPort);

                    if (vSent && pPacket.mExpectAcknowledge) {
                        //locks the line
                        lock (this.mPollingMessageSynchroKey) {
                            #region get the acknowledgement packet
                            //tries n times to read a acknowledge
                            while (!vOk && vTryCount < this.RetryCount) {
                                //wait longer and longer
                                Thread.Sleep(100 * vTryCount);

                                bool vAckOk = false;
                                try {
                                    ARCPO_Packet vAck = new ARCPO_Packet();
                                    vAckOk = vAck.ReadPacket(this.mPort);

                                    if (vAckOk && vAck.mID == pPacket.mID && vAck.ContentString != null && vAck.ContentString.StartsWith("ACK")) {
                                        vOk = true;
                                    }
                                }
                                catch (TimeoutException) { }

                                vTryCount++;
                            }
                            #endregion
                        }

                    }
                    else {
                        //retry
                    }
                }
                catch (InvalidOperationException) {
                    //disconnected ?
                    this.DisconnectSerial();
                }
                finally {
                    //anycases, release the line
                    this.mWaitingForAck = false;
                }

                //count the tries
                vTryCount++;
            }

            return vOk;
        }
Пример #19
0
        /// <summary>
        /// Body of the thread that polls the messages.
        /// It will poll data EVEN IF nobody is registered on the event!
        /// </summary>
        protected void PollMessageThread(object pParam)
        {
            while (this.mPollMessages) {

                if (!this.mWaitingForAck) {
                    //if not waiting for a ACK
                    //small probabily of thread collision, but let's see like that first... (should lock the variable before)
                    lock (mWaitingForAckSynchroKey) {
                        if (!this.mWaitingForAck) {
                            //synchro : lock the line
                            lock (this.mPollingMessageSynchroKey) {
                                try {
                                    bool vReceiveOk = false;
                                    ARCPO_Packet vPacket = new ARCPO_Packet();
                                    vReceiveOk = vPacket.ReadPacket(this.mPort);

                                    if (vReceiveOk && this.PacketReceived != null) {
                                        //received + someone waiting for notification : raise the event
                                        ARCPO_ReceivedEventArgs vE = new ARCPO_ReceivedEventArgs(vPacket);
                                        this.PacketReceived(this, vE);
                                    }
                                }
                                catch (Exception ex) {
                                    //nothing (for now)
                                }
                            }
                        }
                    }
                }

                //get some rest
                Thread.Sleep(RECEIVE_POLLING_IN_MS);
            }
        }
Пример #20
0
        /// <summary>
        /// Prepares a packet based on parameters
        /// </summary>
        /// <param name="pContent"></param>
        /// <param name="pType"></param>
        /// <param name="pSubType"></param>
        /// <returns></returns>
        private ARCPO_Packet MakePacket(string pContent, byte pType, byte pSubType)
        {
            ARCPO_Packet vP = new ARCPO_Packet();
            int vPacketId = mPacketCounter;
            vP.mType = pType;
            //dont allow packet id to be 0!!
            vP.mID = (byte)((mPacketCounter++ % (256 - 1)) + 1);
            vP.mSubType = pSubType;
            vP.mExpectAcknowledge = true;
            vP.ContentString = pContent;

            return vP;
        }