Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

COM port number from Windows.Devices.Enumeration.DeviceInformation

Tags:

c#

uwp

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                     
            }
        }
    }
like image 925
Carlo Mendoza Avatar asked Dec 13 '25 07:12

Carlo Mendoza


1 Answers

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>
like image 136
Grace Feng Avatar answered Dec 15 '25 21:12

Grace Feng



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!