首页 > 解决方案 > IAsync 操作FromIdAsync(string deviceId) 返回 null

问题描述

我在 Raspberry Pi 3 上运行 Windows IoT Core 10 17744。
我尝试使用以下代码从 UWP 应用程序初始化 COM 端口:

string aqs = SerialDevice.GetDeviceSelector();
DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(aqs);
List<DeviceInformation> list = devices.ToList();
DeviceInformation di = list.First();
_serial_port = await SerialDevice.FromIdAsync(di.Id);

我有di.Id->变量的\\?\ACPI#BCM2836#0#{86e0d1e0-8089-11d0-9ce4-08003e301f73}
完整表示di如下:

-       di  {Windows.Devices.Enumeration.DeviceInformation} Windows.Devices.Enumeration.DeviceInformation
        EnclosureLocation   null    Windows.Devices.Enumeration.EnclosureLocation
        Id  "\\\\?\\ACPI#BCM2836#0#{86e0d1e0-8089-11d0-9ce4-08003e301f73}"  string
        IsDefault   false   bool
        IsEnabled   true    bool
        Kind    DeviceInterface Windows.Devices.Enumeration.DeviceInformationKind
        Name    "MINWINPC"  string
-       Pairing {Windows.Devices.Enumeration.DeviceInformationPairing}  Windows.Devices.Enumeration.DeviceInformationPairing
        CanPair false   bool
+       Custom  {Windows.Devices.Enumeration.DeviceInformationCustomPairing}    Windows.Devices.Enumeration.DeviceInformationCustomPairing
        IsPaired    false   bool
        ProtectionLevel None    Windows.Devices.Enumeration.DevicePairingProtectionLevel
        Native View To inspect the native object, enable native code debugging. 
+       Properties  {System.__ComObject}    System.Collections.Generic.IReadOnlyDictionary<string, object> {System.__ComObject}
        Native View To inspect the native object, enable native code debugging. 

await手术后我有 null in _serial_port

更新

null即使我将 Raspberry UART 接口连接到工作节点:第
8 个引脚作为 TX 到配对的 RX 线也是如此。
第 10 个引脚作为 RX 到配对的 TX 线。

更新

我有DeviceCapability清单:

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

标签: raspberry-pi3uartwindows-iot-core-10

解决方案


您需要在Package.appxmanifest中添加串行设备功能:

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

即使我将 Raspberry UART 接口连接到工作节点,它也是空的:第 8 个引脚作为 TX 到配对的 RX 线。第 10 个引脚作为 RX 到配对的 TX 线。

这不是 SerialDevice.FromIdAsync() 成功所必需的。即使您不连接这些引脚,如果代码和设置正确,SerialDevice.FromIdAsync() 也可以获取有效设备。您可以参考串行 UART 示例


推荐阅读