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