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

dumbledad
  • 16,305
  • 23
  • 120
  • 273
  • Have you tired [this sample](https://github.com/ms-iot/samples/tree/develop/SerialUART/CS)? What's kind of your device? – Rita Han Jun 02 '17 at 07:55
  • I'll take a look at the sample. The device is a replacement hub we are building for the next version of [this](https://blogs.microsoft.com/next/2017/03/15/project-torino-microsoft-creates-physical-programming-language-inclusive-visually-impaired-children/) (the current version uses USB and we're swapping it to Bluetooth). It uses the BlueGiga WT32i-A Bluetooth chip. – dumbledad Jun 06 '17 at 09:47
  • This issue may be a little complex and you can check [this](https://stackoverflow.com/questions/13999439/the-semaphore-timeout-period-has-expired) to troubleshoot. – Rita Han Jun 07 '17 at 03:07

1 Answers1

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.

zax
  • 844
  • 8
  • 14