来自 Windows.Devices.Enumeration.DeviceInformation 的 COM 端口号 [英] COM port number from Windows.Devices.Enumeration.DeviceInformation
问题描述
有没有办法通过DeviceInformation
获取设备的COM端口号?可以通过设备管理器查看 COM 端口号.
Is there a way to get the COM port number for a device through DeviceInformation
? I can see COM port numbers when I look at them through Device Manager.
我刚遇到几个设备,它们没有将 COM 端口号列为 Name
属性的一部分.我插入了其中的两个,无法区分它们.理想情况下,我希望看到一个 COM 端口.有没有其他方法可以获取这些信息?
I just ran into a couple of devices that don't list the COM port number as part of the Name
property. I plugged two of them in and there's no way to distinguish them from each other. Ideally, I'd like to see a COM port. Is there a different way to get that information?
string _serialSelector = SerialDevice.GetDeviceSelector();
DeviceInformationCollection tempInfo = await DeviceInformation.FindAllAsync(_serialSelector);
if (tempInfo.Count > 0)
{
foreach (var efefe in tempInfo)
{
if (efefe.Kind.Equals(DeviceInformationKind.DeviceInterface))
{
//efefe.Name
}
}
}
推荐答案
你已经完成了大部分工作,那么你需要使用你的 efefe
的 id 来获取 SerialDevice
对象使用 SerialDevice.FromIdAsync |fromIdAsync 方法.
You've done most of the job, then you will need to use your efefe
's id to get the SerialDevice
object using SerialDevice.FromIdAsync | fromIdAsync method.
这是演示:
string _serialSelector = SerialDevice.GetDeviceSelector();
var infos = await DeviceInformation.FindAllAsync(_serialSelector);
foreach (var info in infos)
{
var serialDevice = await SerialDevice.FromIdAsync(info.Id);
if (serialDevice != null)
{
var port = serialDevice.PortName;
Debug.WriteLine(port.ToString());
}
}
并且请不要忘记在清单中添加DeviceCapability
:
And please don't forget to add DeviceCapability
in manifest:
<DeviceCapability Name="serialcommunication">
<Device Id="any">
<Function Type="name:serialPort" />
</Device>
</DeviceCapability>
这篇关于来自 Windows.Devices.Enumeration.DeviceInformation 的 COM 端口号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!