Example #1
0
        // Each response from the board includes a status field, which is handled here for most messages.
        // If an error is indicated in the status field, issues a query for the error code.
        // If a reset is indicated, issues a query for the reset code.
        // Only one query issues; command error takes precedence over reset, reset takes
        // precedence over global error.
        // Returns False if the rest of the response should be ignored, True otherwise.
        virtual internal bool OnStatus(Status stat)
        {
            ICommand query = null;

            if (stat.HasCommandError || (stat.HasGlobalError && !stat.HasBeenReset))
            {
                query = new iCommand(_addr + GetError + DefaultChannel(),
                                     // Process the ER response's status: if HasCommandError, comm is not working:
                                     //   so, return false to prevent the callback action from executing.
                                     (st) => { return(!st.HasCommandError); },
                                     // Got an error response, raise any actual error in an event.
                                     (err) =>
                {
                    // TODO: global errors tend to be persistent; suppress raising them for every message
                    if (err != 0)
                    {
                        ErrorReceived.Raise(this, new ErrEventArgs(TranslateError(err)));
                    }
                });
            }
            else if (stat.HasBeenReset)
            {
                // query the reset parameter to find out why, and to clear it
                query = new iCommand(_addr + ResetController + DefaultChannel(),
                                     (st) => { return(!st.HasCommandError); },
                                     // Got a reset response, save the reported cause for later querying
                                     (cause) =>
                {
                    // Report reset as an error only if reset state is already known.
                    // A reset flag present in first communications with device does not result in error.
                    if (_reset_cause != ResetCause.UnknownResetState)
                    {
                        ErrorReceived.Raise(this, new ErrEventArgs(ErrorCode.ControllerReset));
                    }
                    _reset_cause = ResetCause.IsDefined(typeof(ResetCause), cause) ?
                                   (ResetCause)cause :
                                   ResetCause.UnrecognizedReset;
                });
            }
            // no error or reset, it's a good response; do a little housekeeping
            else if (_reset_cause == ResetCause.UnknownResetState)
            {
                _reset_cause = ResetCause.NoResetReported; // State is no longer unknown
            }
            if (query != null)
            {
                _board.Issue(query, true);  // Issue query right away, in front of other messages in queue
            }
            return(!stat.HasCommandError);
        }