private async System.Threading.Tasks.Task <bool> TryOpenComportAsync() { Boolean openSuccess = false; var aqsFilter = SerialDevice.GetDeviceSelector(ViewModels.SharedData.SharedDataInstance.CurrentTNCDevice.CommPort.Comport); var devices = await DeviceInformation.FindAllAsync(aqsFilter); if (devices.Count > 0) { DeviceInformation deviceInfo = devices[0]; if (deviceInfo != null) { // Create an EventHandlerForDevice to watch for the device we are connecting to EventHandlerForDevice.CreateNewEventHandlerForDevice(); // Get notified when the device was successfully connected to or about to be closed EventHandlerForDevice.Current.OnDeviceConnected = this.OnDeviceConnected; EventHandlerForDevice.Current.OnDeviceClose = this.OnDeviceClosing; openSuccess = await EventHandlerForDevice.Current.OpenDeviceAsync(deviceInfo, aqsFilter); //SerialDevice device = await SerialDevice.FromIdAsync(deviceInfo.Id); //if (openSuccess) //{ // //device.Dispose(); // EventHandlerForDevice.Current.CloseDevice(); // openSuccess = true; //} } } return(openSuccess); }
/// <summary> /// Creates a new instance of EventHandlerForDevice, enables auto reconnect, and uses it as the Current instance. /// </summary> public static void CreateNewEventHandlerForDevice() { eventHandlerForDevice = new EventHandlerForDevice(_log); }
/// <summary> /// The device was closed. If we will autoreconnect to the device, reflect that in the UI /// </summary> /// <param name="sender"></param> /// <param name="deviceInformation"></param> private void OnDeviceClosing(EventHandlerForDevice sender, DeviceInformation deviceInformation) { LogHelper(LogLevel.Info, $"{ViewModels.SharedData.SharedDataInstance.CurrentTNCDevice.CommPort.Comport} Disconnected."); }