Exemple #1
0
        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);
            }
        }
Exemple #3
0
        /// <summary>
        /// If all the devices have been enumerated, select the device in the list we connected to. Otherwise let the EnumerationComplete event
        /// from the device watcher handle the device selection
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="deviceInformation"></param>
        private void OnDeviceConnected(EventHandlerForDevice sender, DeviceInformation deviceInformation)
        {
            // Find and select our connected device
            if (isAllDevicesEnumerated)
            {
                SelectDeviceInList(EventHandlerForDevice.Current.DeviceInformation.Id);

                ButtonDisconnectFromDevice.Content = ButtonNameDisconnectFromDevice;
            }
        }
Exemple #4
0
 /// <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 async void OnDeviceClosing(EventHandlerForDevice sender, DeviceInformation deviceInformation)
 {
     await Dispatcher.RunAsync(
         CoreDispatcherPriority.Normal,
         new DispatchedHandler(() =>
     {
         // We were connected to the device that was unplugged, so change the "Disconnect from device" button
         // to "Do not reconnect to device"
         if (ButtonDisconnectFromDevice.IsEnabled && EventHandlerForDevice.Current.IsEnabledAutoReconnect)
         {
             ButtonDisconnectFromDevice.Content = ButtonNameDisableReconnectToDevice;
         }
     }));
 }
Exemple #5
0
        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);
        }
        /// <summary>
        /// If all the devices have been enumerated, select the device in the list we connected to. Otherwise let the EnumerationComplete event
        /// from the device watcher handle the device selection
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="deviceInformation"></param>
        private async void OnDeviceConnected(EventHandlerForDevice sender, DeviceInformation deviceInformation)
        {
            // Find and select our connected device
            if (isAllDevicesEnumerated)
            {
                SelectDeviceInList(EventHandlerForDevice.Current.DeviceInformation.Id);

                DisconectButton.Content = ButtonNameDisconnectFromDevice;
            }
            NotifyUser("Conectado a - " +
                       EventHandlerForDevice.Current.DeviceInformation.Name, NotifyType.StatusMessage);

            if (EventHandlerForDevice.Current.Device != null)
            {
                EventHandlerForDevice.Current.ConfigureCurrentlyConnectedDevice();
                ResetReadCancellationTokenSource();
                ResetWriteCancellationTokenSource();
                await WriteReadTaskAsync("square");
            }
        }