Exemple #1
0
        string IPortAdapter.ReadChars()
        {
            System.Text.StringBuilder buffer = new System.Text.StringBuilder();
            bool timedOut = false;

            try
            {
                /* There are a few messages that won't end in a NewLine,
                 * so we have to read one character at a time until we run out of them.
                 */
                do
                {     // Read one char at a time until the ReadChar times out.
                    try
                    {
                        buffer.Append(GetCharString());
                    }
                    catch (Exception)
                    {
                        timedOut = true;
                    }
                } while (!timedOut);
            }
            catch (Exception ex)
            {
                ServiceMain.HandleEx(ex);
                throw;
            }
            return(buffer.ToString());
        }
        void CommPortDataReceived(object sender, EventArgs e)
        {
            /* When new data is received,
             * parse the message line-by-line.
             */
            IPortAdapter  port   = (IPortAdapter)sender;
            StringBuilder buffer = new StringBuilder();

            try
            {
                idleTimer.Stop();
#if DEBUG
                System.Diagnostics.Stopwatch stopwatch = System.Diagnostics.Stopwatch.StartNew();     // This stopwatch is an attempt at performance optimization.
#endif

                /* There are a few messages that won't end in a NewLine,
                 * so we have to read one character at a time until we run out of them.
                 */
                // Read one char at a time until the ReadChar times out.
                try
                {
                    buffer.Append(port.ReadChars());
                }
                catch (Exception)
                {
#if DEBUG
                    stopwatch.Stop();
#endif
                }
#if DEBUG
                ServiceMain.AppendToLog($"Elapsed port read time: {stopwatch.ElapsedMilliseconds}");
#endif
                ServiceMain.AppendToLog($"In: \t{buffer}");
                CommState.RcvInput(buffer.ToString());
                idleTimer.Start();
            }
            catch (Exception ex)
            {
                ServiceMain.HandleEx(ex);
                throw;
            }
        }