SerialDevice.FromIdAsync(string id) timing out

百般思念 提交于 2019-12-12 03:43:58

问题


Here's a fragment of a UWP app I am writing

// [...]

using Windows.Devices.Enumeration;
using Windows.Devices.SerialCommunication;
using Windows.Networking.Connectivity;

// [...]

private Dictionary<string, SerialDevice> _SerialDevices = new Dictionary<string, SerialDevice>();

// [...]

var serialSelector = SerialDevice.GetDeviceSelector();
var serialDevices = (await DeviceInformation.FindAllAsync(serialSelector)).ToList();
var hostNames = NetworkInformation.GetHostNames().Select(hostName => hostName.DisplayName.ToUpper()).ToList(); // So we can ignore inbuilt ports
foreach (var deviceInfo in serialDevices)
{
    if (hostNames.FirstOrDefault(hostName => hostName.StartsWith(deviceInfo.Name.ToUpper())) == null)
    {
        try
        {
            var serialDevice = await SerialDevice.FromIdAsync(deviceInfo.Id);
            if (serialDevice != null)
            {
                _SerialDevices.Add(deviceInfo.Id, serialDevice);
            }
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(ex.ToString());
        }
    }
}

I have added the following to the Package.appxmanifest file for the project

<DeviceCapability Name="serialcommunication">
  <Device Id="any">
    <Function Type="name:serialPort" />
  </Device>
</DeviceCapability>

When I get to the line var serialDevice = await SerialDevice.FromIdAsync(deviceInfo.Id); it throws an exception:

{System.Exception: The semaphore timeout period has expired. (Exception from HRESULT: 0x80070079)
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at TorinoBluetooth.UwpHidConnector.d__9.MoveNext()}

Why is this failing?Is there something about UWP that prevents serial connections in this way?

(N.B. I know that the serial connection is OK since I can look it up in the Device Manager, where it is listed as "Standard Serial over Bluetooth link (COM8)", and then connect other code manually to "COM8".)

来源:https://stackoverflow.com/questions/44306228/serialdevice-fromidasyncstring-id-timing-out

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!