无法打开串行端口 - UWP



我正在尝试将我的 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;
}

而且我在foreach结束时没有做serialDevice.Dispose();.

添加serialDevice.Dispose();后,我能够使用旧的SerialPort

最新更新