private async void OnDeviceClosing(EventHandlerForDevice sender, DeviceInformation deviceInformation) { await Dispatcher.RunAsync( CoreDispatcherPriority.Normal, new DispatchedHandler(() => { if (ButtonDisconnectFromDevice.IsEnabled && EventHandlerForDevice.Current.IsEnabledAutoReconnect) { ButtonDisconnectFromDevice.Content = "Disconnect"; } })); }
private async void ConnectBtn_Clicked(Object sender, RoutedEventArgs eventArgs) { var selection = ConnectDevices.SelectedItems; DeviceListEntry entry = null; if (selection.Count > 0) { var obj = selection[0]; entry = (DeviceListEntry)obj; if (entry != null) { EventHandlerForDevice.CreateNewEventHandlerForDevice(); EventHandlerForDevice.Current.OnDeviceConnected = this.OnDeviceConnected; EventHandlerForDevice.Current.OnDeviceClose = this.OnDeviceClosing; Boolean openSuccess = await EventHandlerForDevice.Current.OpenDeviceAsync(entry.DeviceInformation, entry.DeviceSelector); UpdateConnectDisconnectButtonsAndList(!openSuccess); } } }
private void OnDeviceConnected(EventHandlerForDevice sender, DeviceInformation deviceInformation) { // Find and select our connected device if (isAllDevicesEnumerated) { SelectDeviceInList(EventHandlerForDevice.Current.DeviceInformation.Id); ButtonDisconnectFromDevice.Content = "Disconnect"; } if (EventHandlerForDevice.Current.Device.PortName != "") { EventHandlerForDevice.Current.Device.Parity = SerialParity.None; EventHandlerForDevice.Current.Device.StopBits = SerialStopBitCount.One; EventHandlerForDevice.Current.Device.Handshake = SerialHandshake.None; EventHandlerForDevice.Current.Device.DataBits = 8; EventHandlerForDevice.Current.Device.BaudRate = 115200; ResetReadCancellationTokenSource(); ResetWriteCancellationTokenSource(); isActive = true; EventHandlerForDevice.Current.Device.ReadTimeout = new System.TimeSpan(10 * 10000); ReadButton_Click(); } }
/// <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(); }