/// <summary> /// Invoked when this page is about to be displayed in a Frame. /// /// We will enable/disable parts of the UI if the device doesn't support it. /// </summary> /// <param name="eventArgs">Event data that describes how this page was reached. The Parameter /// property is typically used to configure the page.</param> private void Initialize() { if (EventHandlerForDevice.Current.Device == null) { SerialConfigureDeviceVisibility = Visibility.Collapsed; SerialMain.NotifyUser("Device is not connected", NotifyType.ErrorMessage); } else { if (EventHandlerForDevice.Current.Device.PortName != "") { SerialMain.NotifyUser("Connected to " + EventHandlerForDevice.Current.Device.PortName, NotifyType.StatusMessage); } UpdateBaudRateView(); UpdateParityView(); UpdateHandShakeView(); UpdateBreakStateSignalView(); UpdateCarrierDetectStateView(); UpdateDataBitsView(); UpdateDataSetReadyStateView(); UpdateDataTerminalReadyEnabledView(); UpdateRequestToSendEnabledView(); UpdateStopBitCountView(); } }
/// <summary> /// Invoked when this page is about to be displayed in a Frame. /// /// We will enable/disable parts of the UI if the device doesn't support it. /// </summary> /// <param name="eventArgs">Event data that describes how this page was reached. The Parameter /// property is typically used to configure the page.</param> //protected override void OnNavigatedTo(NavigationEventArgs eventArgs) public void Initialize() { IsNavigatedAway = false; if (EventHandlerForDevice.Current.Device == null) { ReadWriteScollViewerVisibility = Visibility.Collapsed; SerialMain.NotifyUser("Device is not connected", NotifyType.ErrorMessage); } else { if (EventHandlerForDevice.Current.Device.PortName != "") { SerialMain.NotifyUser("Connected to " + EventHandlerForDevice.Current.Device.PortName, NotifyType.StatusMessage); } // So we can reset future tasks ResetReadCancellationTokenSource(); ResetWriteCancellationTokenSource(); UpdateReadButtonStates(); UpdateWriteButtonStates(); UpdateReadBytesCounterView(); UpdateReadTimeoutView(); UpdateWriteBytesCounterView(); UpdateWriteTimeoutView(); } }
/// <summary> /// We will remove the device from the UI /// </summary> /// <param name="sender"></param> /// <param name="deviceInformationUpdate"></param> private async void OnDeviceRemoved(DeviceWatcher sender, DeviceInformationUpdate deviceInformationUpdate) { await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync( CoreDispatcherPriority.Normal, new DispatchedHandler(() => { SerialMain.NotifyUser("Device removed - " + deviceInformationUpdate.Id, NotifyType.StatusMessage); RemoveDeviceFromList(deviceInformationUpdate.Id); })); }
/// <summary> /// This method opens the device using the WinRT Serial 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) { try { device = await SerialDevice.FromIdAsync(deviceInfo.Id); } catch (Exception) { throw; } 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); } if (appSuspendEventHandler == null || appResumeEventHandler == null) { RegisterForAppEvents(); } // Register for DeviceAccessInformation.AccessChanged event and react to any changes to the // user access after the device handle was opened. 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; if (deviceAccessStatus == DeviceAccessStatus.DeniedByUser) { notificationMessage = "Access to the device was blocked by the user : "******"Access to the device was blocked by the system : " + deviceInfo.Id; } else { // Most likely the device is opened by another app, but cannot be sure notificationMessage = "Unknown error, possibly opened by another app : " + deviceInfo.Id; } } SerialMain.NotifyUser(notificationMessage, notificationStatus); return(successfullyOpenedDevice); }