如何在 UWP 中获取可用的串口? [英] How to get available serial ports in UWP?

查看:33
本文介绍了如何在 UWP 中获取可用的串口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找可以获取 UWP 应用程序中串行端口列表的 API.由于 System.IO.Ports 不适用于 UWP,您能否建议以下代码的任何替代方案.

I am looking for API which can get the list of serial ports in UWP app. As System.IO.Ports is not available for UWP, can you suggest any alternative of the below code.

string[] ports = SerialPort.GetPortNames();

推荐答案

首先在您的 ptoject 中的 Package.appxmanifest 中添加此项目,然后右键单击 Package.appxmanifest 并选择 在 Capabilities 标签中查看代码:

First add this items in Package.appxmanifest in your ptoject, to do that right click on Package.appxmanifest and choose View Code in Capabilities tag:

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

之后将所有串行设备连接到您的电脑并检查端口名称.

After that get all serial device connected to your pc and check PortName.

string aqs = SerialDevice.GetDeviceSelector();
var deviceCollection = await DeviceInformation.FindAllAsync(aqs);
List<string> portNamesList = new List<string>();
foreach (var item in deviceCollection)
{
     var serialDevice = await SerialDevice.FromIdAsync(item.Id);
     var portName = serialDevice.PortName;
     portNamesList.Add(portName);
}

这篇关于如何在 UWP 中获取可用的串口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆