Ejemplo n.º 1
0
                /// <summary>
                /// Send an STX ETX formatted command (STX and ETX are added here).
                /// Blocks until ack received or connection fails.
                /// </summary>
                /// <param name="data">the data to send (can be null)</param>
                /// <param name="optionalRetries">Number of retries if no ACK is received</param>
                /// <param name="optionalRetryTime">Amount of time (in ms) between retries</param>
                /// <returns>True if an ACK was recieved, otherwise false</returns>
                public bool SendCommand(byte[] data, int optionalRetries = SendContext.DEF_NUM_RETRIES, int optionalRetryTime = SendContext.DEF_RETRY_TIMEOUT_MS)
                {
                    bool bFoundAck = false;

                    #region Add STX ETX, call new byte array "theData"
                    byte[] theData;
                    SendingContext.AddStxEtx(out theData, data);
                    #endregion

                    lock (SendingContext)
                    {
                        SendingContext.AckFound = new AutoResetEvent(false);
                        StxEtxAck = ACK_SEARCH;

                        try
                        {
                            while (optionalRetries >= 0)
                            {
                                string dat;
                                if (data != null)
                                {
                                    dat = System.Text.Encoding.Default.GetString(data);
                                }
                                else
                                {
                                    dat = "";
                                }

                                CommContext.LogMsg(TraceEventType.Verbose, "STXETX SENT: <STX>" + dat + "<ETX>");
                                CommContext.Write(theData);

                                bool reply = SendingContext.AckFound.WaitOne(optionalRetryTime);
                                if (reply == true)  //got an ack!
                                {
                                    bFoundAck = true;
                                    break;
                                }
                                else
                                {
                                    optionalRetries -= 1;
                                }
                            }
                        }

                        /* want the calling function to catch the exceptions... catch (Exception e)
                         * {
                         *  Console.WriteLine(e);
                         *  throw e;
                         * }*/
                        catch (Exception ex)
                        {
                            if (data != null)
                            {
                                CommContext.LogMsg(TraceEventType.Verbose, "Caught exception when waiting for an ACK to this message: <STX>" + System.Text.Encoding.Default.GetString(data.ToArray()) + "<ETX>. optionalRetries: " + optionalRetries.ToString() + ". Exception: " + ex.ToString());
                            }
                            else
                            {
                                CommContext.LogMsg(TraceEventType.Verbose, "Caught exception when waiting for an ACK to this message: <STX><ETX>. optionalRetries: " + optionalRetries.ToString() + ". Exception: " + ex.ToString());
                            }
                        }
                        finally
                        {
                            try { SendingContext.AckFound.Dispose(); }
                            catch { }
                            SendingContext.AckFound = null;

                            //important to prevent the STX/ETX handler from locking
                            if (StxEtxAck == ACK_SEARCH)
                            {
                                StxEtxAck = STX_SEARCH;
                            }
                        }
                    }
                    return(bFoundAck);
                }