Exemple #1
0
 /// <summary>
 /// trigger by a timeout signal for the serial commonication
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 void _serial_TimeOut(object sender, EventArgs e)
 {
     if (OnTimeOut != null)
     {
         //OnTimeOut(this, new EventArgs());
         ThreadSafe.Invoke(this.OnTimeOut, new object[] { this, EventArgs.Empty });
     }
 }
Exemple #2
0
        /// <summary>
        /// trigger by incoming data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void _serial_DataReceived(object sender, ReceivedDataEventArgs e)
        {
            if (e.ReceivedData.Length > 1)
            {
                byte[] answer   = e.ReceivedData;
                string sAnswer  = string.Empty;
                byte   LRC      = 0x00;
                byte   checkLRC = 0x00;

                bool beginMessage = false;
                bool endMessage   = false;
                for (int i = 0; i < answer.Length; i++)
                {
                    if (answer[i] == _cc_STX)
                    {
                        LRC          = 0x00;
                        beginMessage = true;
                        endMessage   = false;
                        LRC         += answer[i];
                    }
                    else if (beginMessage == true)
                    {
                        if (answer[i] == _cc_ETX)
                        {
                            beginMessage = false;
                            endMessage   = true;
                        }
                        else
                        {
                            sAnswer += CommunicationTools.byteToString(answer[i]);
                        }
                        LRC += answer[i];
                    }
                    else if (endMessage)
                    {
                        checkLRC = answer[i];
                        break;
                    }
                }

                LRC = (byte)((0xff - LRC) + 0x01);

                if (LRC != checkLRC)
                {
                    if (OnCRCError != null)
                    {
                        //OnCRCError(this, new EventArgs());
                        ThreadSafe.Invoke(this.OnCRCError, new object[] { this, new QCommunicationCRCEventArgs(sAnswer, answer, checkLRC, LRC) });
                    }
                }

                if (OnIncomingData != null) //
                {
                    //OnIncomingData(this, new QCommunicationEventArgs(answer, decompressData));
                    ThreadSafe.Invoke(this.OnIncomingData, new object[] { this, new QCommunicationEventArgs(sAnswer, answer) });
                }
            }
            else
            {
                if (e.ReceivedData[0] == _cc_ACK)
                {
                    if (OnACKMessage != null)
                    {
                        ThreadSafe.Invoke(this.OnACKMessage, new object[] { this, EventArgs.Empty });
                    }
                }
                else if (e.ReceivedData[0] == _cc_NAK)
                {
                    if (OnNAKMessage != null)
                    {
                        ThreadSafe.Invoke(this.OnNAKMessage, new object[] { this, EventArgs.Empty });
                    }
                }
                else if (e.ReceivedData[0] == _cc_SYN)
                {
                    if (OnSYNMessage != null)
                    {
                        ThreadSafe.Invoke(this.OnSYNMessage, new object[] { this, EventArgs.Empty });
                    }
                }
            }
        }