示例#1
0
 protected override void OnPause()
 {
     base.OnPause();
     if (mUsbSerialPort != null)
     {
         try
         {
             mUsbSerialPort.DataReceived -= UsbSerialPort_DataReceived;
             mUsbSerialPort.Close();
         }
         catch (Exception)
         {
             // Ignore.
         }
     }
     Finish();
 }
        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);
        }
 protected override void OnPause()
 {
     base.OnPause();
     if (CheckInstance is SendDataCheck)
     {
         ((SendDataCheck)CheckInstance).StopSendData();
     }
     if (UseUsbSerialPort != null)
     {
         try
         {
             UseUsbSerialPort.Close();
             UseUsbSerialPort.DataReceivedEventLinser -= DataReceivedHandler;
         }
         catch (Exception)
         {
             // Ignore.
         }
     }
     Finish();
 }
        public void Close()
        {
            if (disposed)
            {
                throw new ObjectDisposedException(GetType().Name);
            }
            if (!IsOpen)
            {
                throw new InvalidOperationException();
            }

            // -------------------------------------
            // Close port
            // -------------------------------------
            port.Close();
            Log.Debug("PPX", "Port is closed");

            //// cancel task
            //cancelationTokenSource.Cancel();
        }
 public void Close()
 {
     port.Close();
 }