Exemplo n.º 1
0
        /// <summary>
        /// Connects to the specified device and returns its CommManager.
        /// </summary>
        /// <param name="device">The device.</param>
        /// <returns>
        /// The CommManager of the device or null
        /// </returns>
        /// <exception cref="System.ArgumentNullException">The specified device cannot be null.</exception>
        /// <exception cref="System.Exception">Could not connect to the specified device.</exception>
        internal async Task <CommManager> Connect(Device device)
        {
            if (device == null)
            {
                throw new ArgumentNullException("The specified device cannot be null.");
            }
            CommManager comm = null;

            await new SynchronizationContextRemover();

            try
            {
                await device.OpenAsync();                          //Open the device to allow a connection

                comm = await CommManager.CommManagerAsync(device); //Connect to the selected device
            }
            catch (Exception ex)
            {
                device.Close();
                throw new Exception($"Could not connect to the specified device. {ex.Message}");
            }

            return(comm);
        }