3

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.

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                     
            }
        }
    }
Carlo Mendoza
  • 765
  • 6
  • 24

1 Answers1

2

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.

Here is the demo:

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

And please don't forget to add DeviceCapability in manifest:

<DeviceCapability Name="serialcommunication">
  <Device Id="any">
    <Function Type="name:serialPort" />
  </Device>
</DeviceCapability>
Grace Feng
  • 16,564
  • 2
  • 22
  • 45