C# Can';t打开串行端口-UWP

C# Can';t打开串行端口-UWP,c#,uwp,serial-port,C#,Uwp,Serial Port,我正在尝试将我的WPF应用程序转换为UWP应用程序。我的应用程序使用串行端口类与设备通信。在我切换到UWP后,使用串口类打开设备连接的代码停止工作 public MotorControl(string portName, Settings settings) { this.settings = settings; this.serialPort = new SerialPort(portName, 9600) { DtrEnable = true,

我正在尝试将我的WPF应用程序转换为UWP应用程序。我的应用程序使用串行端口类与设备通信。在我切换到UWP后,使用串口类打开设备连接的代码停止工作

public MotorControl(string portName, Settings settings)
{
    this.settings = settings;
    this.serialPort = new SerialPort(portName, 9600)
    {
        DtrEnable = true,
        RtsEnable = true
    };
    this.serialPort.ErrorReceived += Port_ErrorReceived;
    this.serialPort.DataReceived += Port_DataReceived;
}
public void Connect()
{
    this.serialPort.Open();
    Thread.Sleep(this.settings.delayBetweenConnectAntWifi);
    this.SetNetworkMode("wifi");
}
当我试图打开连接时,出现以下错误:

System.IO.IOException
  HResult=0x80070000
  Message=The operation completed successfully
  Source=System.IO.Ports
  StackTrace:
   at System.IO.Ports.InternalResources.WinIOError(Int32 errorCode, String str)
   at System.IO.Ports.SerialStream..ctor(String portName, Int32 baudRate, Parity parity, Int32 dataBits, StopBits stopBits, Int32 readTimeout, Int32 writeTimeout, Handshake handshake, Boolean dtrEnable, Boolean rtsEnable, Boolean discardNull, Byte parityReplace)
我试着使用串行设备

public MotorControl(string portName, Settings settings)
{
    this.settings = settings;
    this.portName = portName;
}

private async Task SetupSerialDevice()
{
    string aqs = SerialDevice.GetDeviceSelector(this.portName);
    var myDevices = await DeviceInformation.FindAllAsync(aqs, null);
    if (myDevices.Count == 0)
    {
        return;
    }
    this.serialDevice = await SerialDevice.FromIdAsync(myDevices.First().Id);
    this.serialDevice.BaudRate = 9600;
    this.serialDevice.DataBits = 8;
    this.serialDevice.StopBits = SerialStopBitCount.One;
    this.serialDevice.Parity = SerialParity.None;
    this.serialDevice.Handshake = SerialHandshake.None;
    this.serialDevice.ReadTimeout = TimeSpan.FromMilliseconds(1000);
    this.serialDevice.WriteTimeout = TimeSpan.FromMilliseconds(1000);
    this.dataWriter= new DataWriter(this.serialDevice.OutputStream);
    this.dataReader= new DataReader(this.serialDevice.InputStream);
    this.readPortThread = new Thread(this.ReadPort);
    this.readPortThread.Start();
}
public async void Connect()
{
    await this.SetupSerialDevice();
    Thread.Sleep(this.settings.delayBetweenConnectAntWifi);
     this.SetNetworkMode("wifi");
}
但是,SerialDevice.FromIdAsync始终返回null

<Package
  xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
  xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"
  xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
  xmlns:genTemplate="http://schemas.microsoft.com/appx/developer/windowsTemplateStudio"
  xmlns:iot="http://schemas.microsoft.com/appx/manifest/iot/windows10"
  xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
  IgnorableNamespaces="uap mp genTemplate iot rescap">
.
.
.
<Capabilities>
    <rescap:Capability Name="confirmAppClose"/>
    <Capability Name="internetClient" />
    <iot:Capability Name="lowLevelDevices"/>
    <DeviceCapability Name="serialCommunication">
      <Device Id="any">
        <Function Type="name:serialPort" />
      </Device>
    </DeviceCapability>
  </Capabilities>

.
.
.

问题在于,在尝试连接到设备之前,我使用此功能查找所有端口:

public async static Task<ObservableCollection<string>> GetPortNames()
{
    string aqs = SerialDevice.GetDeviceSelector();
    var deviceCollection = await DeviceInformation.FindAllAsync(aqs);
    ObservableCollection<string> portNamesList = new ObservableCollection<string>();
    foreach (var item in deviceCollection)
    {
       SerialDevice serialDevice = await SerialDevice.FromIdAsync(item.Id);
       string portName = serialDevice.PortName;
       portNamesList.Add(portName);
    }
    return portNamesList;
}
public异步静态任务GetPortNames()
{
字符串aqs=SerialDevice.GetDeviceSelector();
var deviceCollection=等待设备信息。FindAllAsync(aqs);
ObservableCollection端口名称列表=新的ObservableCollection();
foreach(deviceCollection中的var项)
{
SerialDevice SerialDevice=等待SerialDevice.FromIdAsync(item.Id);
字符串portName=serialDevice.portName;
portNamesList.Add(portName);
}
返回端口名称列表;
}
我没有做一个
serialDevice.Dispose()
foreach
的末尾


添加
serialDevice.Dispose()后我能够使用好的旧
串行端口

似乎WPF和UWP有不同的思想和API。看看这些文章,我用我的最新更改更新我的问题您是否在介绍的文章中描述的
Package.appxmanifest
文件中设置了
DeviceCapability
条目?是的,请参见上面的编辑。它可以找到一个设备,但FromIdAsync返回null如果是,请下载并构建引入的示例程序项目,并检查操作。您可能需要将DevicePath与VID/PID一起使用,而不是将“COMx”用作端口名。