无法打开串口-UWP

时间:2019-07-23 20:07:19

标签: c# uwp serial-port

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

1 个答案:

答案 0 :(得分:0)

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

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

serialDevice.Dispose();的结尾我没有做foreach

添加serialDevice.Dispose();之后,我就可以使用旧的SerialPort

相关问题