private async void ConnectToDevice_Click(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) { // 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; // It is important that the FromIdAsync call is made on the UI thread because the consent prompt, when present, // can only be displayed on the UI thread. Since this method is invoked by the UI, we are already in the UI thread. Boolean openSuccess = await EventHandlerForDevice.Current.OpenDeviceAsync(entry.DeviceInformation, entry.DeviceSelector); // Disable connect button if we connected to the device UpdateConnectDisconnectButtonsAndList(!openSuccess); } } }
private async void ConectedButton_Click(object sender, RoutedEventArgs e) { var selections = DeviceList.SelectedItems; DeviceListEntry entry = null; if (selections.Count > 0) { ConectedButton.IsEnabled = false; var obj = selections[0]; entry = (DeviceListEntry)obj; if (entry != 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; // It is important that the FromIdAsync call is made on the UI thread because the consent prompt, when present, // can only be displayed on the UI thread. Since this method is invoked by the UI, we are already in the UI thread. Boolean openSuccess = await EventHandlerForDevice.Current.OpenDeviceAsync(entry.DeviceInformation, entry.DeviceSelector); } } else { NotifyUser("Conecte un dispositivo", NotifyType.NoDeviceConected); } }
/// <summary> /// Creates a DeviceListEntry for a device and adds it to the list of devices in the UI /// </summary> /// <param name="deviceInformation">DeviceInformation on the device to be added to the list</param> /// <param name="deviceSelector">The AQS used to find this device</param> private void AddDeviceToList(DeviceInformation deviceInformation, String deviceSelector) { // search the device list for a device with a matching interface ID var match = FindDevice(deviceInformation.Id); // Add the device if it's new if (match == null) { // Create a new element for this device interface, and queue up the query of its // device information match = new DeviceListEntry(deviceInformation, deviceSelector); // Add the new element to the end of the list of devices listOfDevices.Add(match); } }
public async Task <bool> Setup() { string deviceSelector = Windows.Devices.SerialCommunication.SerialDevice.GetDeviceSelectorFromUsbVidPid( ArduinoDevice.Vid, ArduinoDevice.Pid); var devicesInformation = await DeviceInformation.FindAllAsync(deviceSelector); if (devicesInformation != null && devicesInformation.Count > 0) { var deviceInformation = devicesInformation[0]; var deviceWatcher = DeviceInformation.CreateWatcher(deviceSelector); // Allow the EventHandlerForDevice to handle device watcher events that relates or effects our device (i.e. device removal, addition, app suspension/resume) var entry = new DeviceListEntry(deviceInformation, deviceSelector); 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; // It is important that the FromIdAsync call is made on the UI thread because the consent prompt, when present, // can only be displayed on the UI thread. Since this method is invoked by the UI, we are already in the UI thread. _isConnected = await EventHandlerForDevice.Current.OpenDeviceAsync(entry.DeviceInformation, entry.DeviceSelector); if (_isConnected) { EventHandlerForDevice.Current.Device.BaudRate = 57600; EventHandlerForDevice.Current.Device.StopBits = SerialStopBitCount.One; EventHandlerForDevice.Current.Device.DataBits = 8; EventHandlerForDevice.Current.Device.Parity = SerialParity.None; EventHandlerForDevice.Current.Device.Handshake = SerialHandshake.None; EventHandlerForDevice.Current.Device.WriteTimeout = TimeSpan.FromMilliseconds(500); EventHandlerForDevice.Current.Device.ReadTimeout = TimeSpan.FromMilliseconds(500); ResetReadCancellationTokenSource(); ResetWriteCancellationTokenSource(); } } return(_isConnected); }
private void RemoveDeviceFromList(string deviceId) { // Remove bluetooth devices as well TODO // Removes the device entry from the internal list DeviceListEntry deviceEntry = FindDevice(deviceId); _listOfDevices.Remove(deviceEntry); // Recreate the list of serial ports from list of devices _listOfSerialPorts.Clear(); foreach (DeviceListEntry entry in _listOfDevices) { if (!string.IsNullOrEmpty(entry.ComPort)) { _listOfSerialPorts.Add(entry.ComPort); } } _listOfSerialPorts.Sort(_comportComparer); //ViewModel.CollectionOfSerialDevices = new ObservableCollection<string>(_listOfSerialPorts); }
private void DisconnectFromDevice_Click(Object sender, RoutedEventArgs eventArgs) { var selection = ConnectDevices.SelectedItems; DeviceListEntry entry = null; // Prevent auto reconnect because we are voluntarily closing it // Re-enable the ConnectDevice list and ConnectToDevice button if the connected/opened device was removed. EventHandlerForDevice.Current.IsEnabledAutoReconnect = false; if (selection.Count > 0) { var obj = selection[0]; entry = (DeviceListEntry)obj; if (entry != null) { EventHandlerForDevice.Current.CloseDevice(); } } UpdateConnectDisconnectButtonsAndList(true); }
private async void AddDeviceToListAsync(DeviceInformation deviceInformation, string deviceSelector) { DeviceListEntry match = null; try { // search the device list for a device with a matching interface ID match = FindDevice(deviceInformation.Id); // Add the device if it's new if (match is null) { // Create a new element for this device interface, and queue up the query of its // device information match = new DeviceListEntry(deviceInformation, deviceSelector); // Add the new element to the end of the list of devices _listOfDevices.Add(match); } } catch (Exception e) { _logHelper.Log(LogLevel.Error, $"{e.Message}, Device = {deviceInformation.Id}"); } if (deviceInformation.Pairing.IsPaired) { // Bluetooth device try { if (deviceInformation.Kind == DeviceInformationKind.AssociationEndpoint) { //_listOfBluetoothDevices.Add(deviceInformation); //CollectionOfBluetoothDevices = new ObservableCollection<DeviceInformation>(_listOfBluetoothDevices); //CollectionOfBluetoothDevices.Add(deviceInformation); //ComNameListSource.Source = CollectionOfBluetoothDevices; } } catch (Exception ex) { _logHelper.Log(LogLevel.Error, $"Add Bluetooth device: { ex.Message}"); } } else { try { if (!deviceInformation.Id.Contains("Bluetooth")) { // USB serial port SerialDevice serialDevice = await SerialDevice.FromIdAsync(deviceInformation.Id); if (serialDevice != null) { match.ComPort = serialDevice.PortName; _listOfSerialPorts.Add(serialDevice.PortName); _listOfSerialPorts.Sort(_comportComparer); //ViewModel.CollectionOfSerialDevices = new ObservableCollection<string>(_listOfSerialPorts); serialDevice.Dispose(); // Necessary to avoid crash on removed device } } } catch (Exception e) { _logHelper.Log(LogLevel.Error, $"{e.Message}, Device = {deviceInformation.Id}"); } } }