【问题标题】:C# UWP - cannot connect to the serial portC# UWP - 无法连接到串口
【发布时间】:2020-05-06 11:49:23
【问题描述】:

我正在开发 UWP 应用程序,需要从串行端口读取数据并将它们显示在图表上。

我收到 3 条随机异常消息:

消息 1: "The operation completed successfully"

消息 2: "An attempt was made to reference a token that does not exist"

消息 3: "The requested lookup key was not found in any active activation context"

我没有实时连接,但我安装了一个模拟器。当我尝试从控制台应用程序建立连接时,它工作正常。

这是我的代码,非常简单:

_serialPort = new SerialPort(portName, baudRate);
_serialPort.DataReceived += _serialPort_DataReceived;
try
{
    _serialPort.Open();
}
catch (Exception ex)
{

}

我想通过_serialPort_DataReceived 方法更新图表。

我知道 UWP 上的串口有一些限制,但我找不到合适的解决方案。

Stack Overflow 上有类似的问题,但没有一个答案能解决我的问题。

【问题讨论】:

  • "The operation completed successfully"怎么是异常信息?这是什么类型的异常?扔到哪里去了?
  • @Fildor 是的,信不信由你:D 这是“IOException”。
  • ...奇怪。它来自你的 sn-p 中的那个 catch 块?
  • @Fildor 是的,对。
  • 我会说我很好,因为相同的代码在控制台应用程序中运行良好。

标签: c# uwp serial-port windows-community-toolkit virtual-serial-port


【解决方案1】:

我正在开发 UWP 应用程序,案例是从串行端口读取数据并将它们显示在图表上。

在 UWP 中,我们使用Windows.Devices.SerialCommunication 命名空间来访问串口。为了使用上述命名空间,我们需要添加以下功能。

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

详细步骤请参考code sample

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-16
    • 1970-01-01
    • 2017-04-27
    • 1970-01-01
    相关资源
    最近更新 更多