简体   繁体   English

C# UWP - 无法连接到串口

[英]C# UWP - cannot connect to the serial port

I'm working on UWP app and need to read data from a serial port and display them on the graph.我正在开发 UWP 应用程序,需要从串行端口读取数据并将它们显示在图表上。

I'm getting 3 random exception messages:我收到 3 条随机异常消息:

Message 1: "The operation completed successfully"消息 1: "The operation completed successfully"

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

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

I don't have a live connection, but I installed a simulator.我没有实时连接,但我安装了一个模拟器。 When I'm trying to establish a connection from the console app it works fine.当我尝试从控制台应用程序建立连接时,它工作正常。

Here is my code, which is really simple:这是我的代码,非常简单:

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

}

From the _serialPort_DataReceived method I want to update the graph._serialPort_DataReceived方法我想更新图表。

I know that there is some limitation with the serial port on UWP, but I can't find an appropriate solution.我知道 UWP 上的串口存在一些限制,但我找不到合适的解决方案。

There are similar questions on Stack Overflow, but none of the answers are a solution to my problem. Stack Overflow 上有类似的问题,但没有一个答案能解决我的问题。

I'm working on UWP app and the case is to read data from a serial port and show them on the graph.我正在开发 UWP 应用程序,案例是从串行端口读取数据并在图表上显示它们。

In UWP we use Windows.Devices.SerialCommunication namespace to access serial port.在 UWP 我们使用Windows.Devices.SerialCommunication命名空间来访问串行端口。 For using above namespace we need add the following capability.为了使用上述命名空间,我们需要添加以下功能。

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

For the detail steps, please refer this code sample .有关详细步骤,请参阅此代码示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM