/// <summary>
        /// This method closes the device properly using the WinRT Usb API.
        ///
        /// When the UsbDevice is closing, it will cancel all IO operations that are still pending (not complete).
        /// The close will not wait for any IO completion callbacks to be called, so the close call may complete before any of
        /// the IO completion callbacks are called.
        /// The pending IO operations will still call their respective completion callbacks with either a task
        /// canceled error or the operation completed.
        /// </summary>
        private void CloseCurrentlyConnectedDevice()
        {
            if (device != null)
            {
                // Notify callback that we're about to close the device
                if (deviceCloseCallback != null)
                {
                    deviceCloseCallback(this, deviceInformation);
                }

                // This closes the handle to the device
                device.Dispose();

                device = null;

                // Save the deviceInformation.Id in case deviceInformation is set to null when closing the
                // device
                String deviceId = deviceInformation.Id;

                //await rootPage.Dispatcher.RunAsync(
                //    CoreDispatcherPriority.Normal,
                //    new DispatchedHandler(() =>
                //    {
                //        MainPage.Current.NotifyUser(deviceId + " is closed", NotifyType.StatusMessage);
                //    }));
            }
        }
            /// <summary>
            /// Constructor, called from CreateAsync method.
            /// </summary>
            /// <param name="device">The UsbDevice, passed to CreateAsync method.</param>
            private UsbSerialPort(
                Windows.Devices.Usb.UsbDevice device
                )
            {
                this.device = device;

                this.DtrEnable = false;
                this.RtsEnable = false;

                this.ReadTimeout = Constants.InfiniteTimeout;

                this.cdcControl = null;
                this.cdcData    = null;
            }
 /// <summary>
 /// Initializes a new instance of the UsbSerialPort class.
 /// </summary>
 /// <param name="device">
 /// UsbDevice, whose functions should be compatible with CdcControl and CdcData.
 /// </param>
 /// <returns>
 /// The result of Promise contains a new UsbSerialPort instance. It contains null, if failed.
 /// </returns>
 public static Windows.Foundation.IAsyncOperation <UsbSerialPort> CreateAsync(
     Windows.Devices.Usb.UsbDevice device
     )
 {
     return(Task.Run(async() =>
     {
         var that = new UsbSerialPort(device);
         var success = await that.TryInitialize();
         if (success)
         {
             return that;
         }
         else
         {
             return null;
         }
     }
                     ).AsAsyncOperation <UsbSerialPort>());
 }
        /// <summary>
        /// This method opens the device using the WinRT Usb API. After the device is opened, save the device
        /// so that it can be used across scenarios.
        ///
        /// It is important that the FromIdAsync call is made on the UI thread because the consent prompt can only be displayed
        /// on the UI thread.
        ///
        /// This method is used to reopen the device after the device reconnects to the computer and when the app resumes.
        /// </summary>
        /// <param name="deviceInfo">Device information of the device to be opened</param>
        /// <param name="deviceSelector">The AQS used to find this device</param>
        /// <returns>True if the device was successfully opened, false if the device could not be opened for well known reasons.
        /// An exception may be thrown if the device could not be opened for extraordinary reasons.</returns>
        public async Task <Boolean> OpenDeviceAsync(DeviceInformation deviceInfo, String deviceSelector)
        {
            device = await Windows.Devices.Usb.UsbDevice.FromIdAsync(deviceInfo.Id);

            Boolean successfullyOpenedDevice = false;
            //NotifyType notificationStatus;
            String notificationMessage = null;

            // Device could have been blocked by user or the device has already been opened by another app.
            if (device != null)
            {
                successfullyOpenedDevice = true;

                deviceInformation   = deviceInfo;
                this.deviceSelector = deviceSelector;

                //notificationStatus = NotifyType.StatusMessage;
                notificationMessage = "Device " + deviceInformation.Id + " opened";

                // Notify registered callback handle that the device has been opened
                if (deviceConnectedCallback != null)
                {
                    deviceConnectedCallback(this, deviceInformation);
                }

                // Background tasks are not part of the app, so app events will not have an affect on the device
                if (!isBackgroundTask && (appSuspendEventHandler == null || appResumeEventHandler == null))
                {
                    RegisterForAppEvents();
                }

                // User can block the device after it has been opened in the Settings charm. We can detect this by registering for the
                // DeviceAccessInformation.AccessChanged event
                if (deviceAccessEventHandler == null)
                {
                    RegisterForDeviceAccessStatusChange();
                }

                // Create and register device watcher events for the device to be opened unless we're reopening the device
                if (deviceWatcher == null)
                {
                    deviceWatcher = DeviceInformation.CreateWatcher(deviceSelector);

                    RegisterForDeviceWatcherEvents();
                }

                if (!watcherStarted)
                {
                    // Start the device watcher after we made sure that the device is opened.
                    StartDeviceWatcher();
                }
            }
            else
            {
                successfullyOpenedDevice = false;

                //notificationStatus = NotifyType.ErrorMessage;

                var deviceAccessStatus = DeviceAccessInformation.CreateFromId(deviceInfo.Id).CurrentStatus;

                switch (deviceAccessStatus)
                {
                case DeviceAccessStatus.DeniedByUser:
                    notificationMessage = "Access to the device was blocked by the user : "******"Access to the device was blocked by the system : " + deviceInfo.Id;
                    break;

                default:
                    // Most likely the device is opened by another app, but cannot be sure
                    notificationMessage = "Unknown error, possibly opened by another app : " + deviceInfo.Id;
                    break;
                }
            }

            //MainPage.Current.NotifyUser(notificationMessage, notificationStatus);

            return(successfullyOpenedDevice);
        }