Exemplo n.º 1
0
        public static ControllerEventArgs GetInstance(string ControllerName, string Message)
        {
            ControllerEventArgs args = objectPool.GetObject();

            args.ControllerName = ControllerName;
            args.Message        = Message;
            args.TimeStamp      = DateTime.Now;
            return(args);
        }
        /// <summary>
        /// Broadcast a message signalling to listeners that data has been received.
        /// </summary>
        protected void BroadcastDataReceived()
        {
            EventHandler <ControllerEventArgs> handler = OnDataReceived;

            // Note that ControllerEventArgs is an Object Pool object to
            // reduce object creation. These objects should call Dispose()
            // when they are no longer needed.
            if (handler != null)
            {
                handler(this, ControllerEventArgs.GetInstance(this.Name, ""));
            }
        }
        /// <summary>
        /// Broadcast an error message asynchronously to anyone who's listening.
        /// </summary>
        /// <param name="Message">The error message</param>
        protected void BroadcastError(string Message)
        {
            EventHandler <ControllerEventArgs> handler = OnError;

            // Note that ControllerEventArgs is an Object Pool object to
            // reduce object creation. These objects should call Dispose()
            // when they are no longer needed.
            if (handler != null)
            {
                handler(this, ControllerEventArgs.GetInstance(this.Name, Message));
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Handler for controller OnError events.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Controller_OnError(object sender, ControllerEventArgs e)
        {
            // Just re-broadcase the error to our listeners.
            BroadcastError(e.Message);

            e.Dispose();  // Recycle the ControllerEventArgs object
        }
Exemplo n.º 5
0
        //private void WriteBytes(byte[] buffer)
        //{
        //    StringBuilder sb = new StringBuilder();
        //    foreach (byte b in buffer)
        //    {
        //        if (sb.Length != 0)
        //            sb.Append(" ");
        //        sb.Append(b.ToString("X2"));
        //    }
        //    System.Diagnostics.Debug.WriteLine("GRAB: " + sb.ToString());
        //}
        /// <summary>
        /// Handler for controller OnDataReceived events.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Controller_OnDataReceived(object sender, ControllerEventArgs e)
        {
            int count = Controller.BytesToRead;

            //System.Diagnostics.Debug.WriteLine("DATA REC: " + count);

            if (count > 0)
            {
                byte[] buffer = new byte[count];
                Controller.Read(buffer, count);

                if (samplingInProgress)
                {
                    sampleReceived = true;

            #if false
                    //System.Diagnostics.Debug.Write(enc.GetString(buffer));
                    WriteBytes(buffer);
            #endif

                    // NOTE: Not sure if this is any better than looping through the buffer myself...
                    this.Data.AddRange(buffer);

                    // Tell our fans that we're making progress.
                    if (this.ExpectedDataLength > 0)
                        BroadcastProgress((int)((100.0 * this.DataLength) / this.ExpectedDataLength));
                }
                else if (pingInProgress)
                {
                    pingResponseReceived = true;

                    // Check for a valid ping response.
                    if (enc.GetString(buffer).Equals("pOnG\r\n"))
                        BroadcastConsoleMessage("Ping successful\r\n");
                    else
                        BroadcastError("Ping failed\r\n");
                }
                else
                {
                    // If we're not in 'sample' or 'ping' mode, just send the received data to the console.
                    BroadcastConsoleMessage(new System.Text.ASCIIEncoding().GetString(buffer).Replace("\0", ""));
                }
            }

            e.Dispose();  // Recycle the ControllerEventArgs object
        }