SerialDevice.FromIdAsync(string id) timing out

0

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".)

bluetooth
uwp
serial-port
asked on Stack Overflow Jun 1, 2017 by dumbledad • edited Sep 6, 2017 by dumbledad

1 Answer

0

This error slows down the serial port enumeration process quite a bit, and it can happen several times when iterating the serial devices. It is a serial port specific issue to the machine the code is running on. To fix the issue on the machine in question, inspect each deviceInfo.Id that throws the error in your handler. For example, a device with an ID like "\?\BTHENUM#{...}" tells you off the bat that it is a Bluetooth VSP causing the timeout. You might delete a no-longer used Bluetooth printer under Settings/Printers & Scanners to clear it up. Otherwise, the ID should give you clues to track down the problem serial driver in Device Manager, which you could them remove or update.

answered on Stack Overflow Feb 6, 2020 by zax

User contributions licensed under CC BY-SA 3.0