How to access virtual COM port from UWP app
Published Jun 25 2019 09:06 PM 13.8K Views
Microsoft

In this article, I explain connect to virtual COM port from UWP app.

In Windows 10 APIs, there are some classes to access serial device, one of those is Windows.Devices.SerialCommunication.SerialDevice class.

 

After define following capability to Pakcage.appxmanifest:

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

It works fine to access to COM port, but it doesn't works for virtual COM port.

If you tried to get SerialDevice instance of virtual COM port, you would not get, SerialDevice.FromIdAsync method would return null.

var deviceColletion = await DeviceInformation.FindAllAsync("COM3");
if (deviceColletion.Any())
{
    var device = deviceColletion.First();
    var s = await SerialDevice.FromIdAsync(device.Id); // return null
}

If you want to write/read to virtual COM port, you can use System.IO.Ports.SerialPort class.

It can Open virtual COM ports, and write / read data to the virtual COM port.

 

But, there is a limitation. SerialPort class on UWP can't list ports using GetPortNames method. If the method was used, then PlatformNotSupportedException would be occurred.

 

So, I created a sample app using it.

The sample app writes / reads to virtual COM port COM3 for reading and COM4 for writing.

 

I tested the app on an environment that is set up using com0com, like following:

コメント 2019-06-26 125914.jpg

The result of the app is as below:

vcomapp3.gif

The source code is on following GitHub repository.

https://github.com/runceel/VirtualCOMPortApp

 

Next article: Get list of virtual serial COM port from UWP app

 

Thanks.

1 Comment
Version history
Last update:
‎Jul 02 2019 12:42 AM
Updated by: