本文介绍了SerialDevice.FromIdAsync(string id)超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我正在编写的UWP应用的一部分

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());
        }
    }
}

我已将以下内容添加到该项目的Package.appxmanifest文件中

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>

当我到达var serialDevice = await SerialDevice.FromIdAsync(deviceInfo.Id);行时,它将引发异常:

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

为什么会失败?UWP是否有某种方式阻止串行连接?

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

(注意,我知道串行连接正常,因为我可以在设备管理器中将其查找为标准的蓝牙串行链接(COM8)",然后将其他代码手动连接到"COM8" )

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

推荐答案

此错误大大降低了串行端口枚举过程的速度,并且在迭代串行设备时可能会发生多次.这是运行代码的计算机所特有的串行端口问题.要解决问题机器上的问题,请检查在处理程序中引发错误的每个deviceInfo.Id.例如,具有"\?\ BTHENUM#{...}"之类的ID的设备会立即告诉您这是导致超时的Bluetooth VSP.您可以在设置/打印机和设置"下删除不再使用的蓝牙打印机.扫描仪将其清除.否则,该ID应该为您提供线索,以在设备管理器"中跟踪问题串行驱动程序,可以将其删除或更新.

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.

这篇关于SerialDevice.FromIdAsync(string id)超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 10:31