public void Open(UsbManager usbManager, int bufferSize = DEFAULT_BUFFERSIZE) { if (disposed) { throw new ObjectDisposedException(GetType().Name); } if (IsOpen) { throw new InvalidOperationException(); } var connection = usbManager.OpenDevice(port.GetDriver().GetDevice()); if (connection == null) { throw new Java.IO.IOException("Failed to open device"); } isOpen = true; buffer = new byte[bufferSize]; port.Open(connection); port.SetParameters(BaudRate, DataBits, StopBits, Parity); cancelationTokenSource = new CancellationTokenSource(); var cancelationToken = cancelationTokenSource.Token; cancelationToken.Register(() => Log.Info(TAG, "Cancellation Requested")); Task.Run(() => { Log.Info(TAG, "Task Started!"); try { while (true) { cancelationToken.ThrowIfCancellationRequested(); Step(); // execute step } } catch (OperationCanceledException) { throw; } catch (Exception e) { Log.Warn(TAG, "Task ending due to exception: " + e.Message, e); ErrorReceived.Raise(this, new UnhandledExceptionEventArgs(e, false)); } finally { port.Close(); port.Driver.Device.Dispose();//richard: avoid GREF leak buffer = null; isOpen = false; Log.Info(TAG, "Task Ended!"); } }, cancelationToken); }
// Use special status handler for the proxied messages: global errors and resets are specific // to the specific controller. Rather than get involved in one controller's error handling, // just bypass it. private bool OnProxyStatus(Status st) { if (st.HasCommandError) { ErrorReceived.Raise(this, new ErrEventArgs(ErrorCode.InvalidProxiedCommand)); return(false); } else { return(true); } }
// 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); }
void Board_ErrorReceived(INamed sender, ErrEventArgs args) { logger.Trace("{0} relayed error {1} from {2}", Name, args.Error, sender.Name); // errors from devices are dispatched to anything that's monitoring our error ErrorReceived.Raise(this, args); }
public void Open(UsbManager usbManager, int bufferSize = DEFAULT_BUFFERSIZE) { if (disposed) { throw new ObjectDisposedException(GetType().Name); } if (IsOpen) { throw new InvalidOperationException(); } var connection = usbManager.OpenDevice(port.Driver.Device); if (connection == null) { throw new Java.IO.IOException("Failed to open device"); } isOpen = true; buffer = new byte[bufferSize]; port.Open(connection); port.SetParameters(BaudRate, DataBits, StopBits, Parity); cancelationTokenSource = new CancellationTokenSource(); var cancelationToken = cancelationTokenSource.Token; cancelationToken.Register(() => Log.Info(TAG, "Cancellation Requested")); Task.Run(() => { Log.Info(TAG, "Task Started!"); try { while (holdWhile) { Thread.Sleep(7000); //new line cancelationToken.ThrowIfCancellationRequested(); sending(); //sending data (new line) Step(); // execute comming data /////////////////////////new line///////////////////////////////////////// ConnectivityManager ConnectivityManager = (ConnectivityManager)Application.Context.GetSystemService(Context.ConnectivityService); NetworkInfo conn = ConnectivityManager.ActiveNetworkInfo; if (conn == null || !conn.IsConnected) { holdWhile = false; } ////////////////////////////////////////////////////////////////////////////// } } catch (OperationCanceledException) { throw; } catch (Exception e) { Log.Warn(TAG, "Task ending due to exception: " + e.Message, e); ErrorReceived.Raise(this, new UnhandledExceptionEventArgs(e, false)); } finally { port.Close(); buffer = null; isOpen = false; Log.Info(TAG, "Task Ended!"); } }, cancelationToken); }