Home
%3CLINGO-SUB%20id%3D%22lingo-sub-720864%22%20slang%3D%22en-US%22%3EHow%20to%20access%20virtual%20COM%20port%20from%20UWP%20app%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-720864%22%20slang%3D%22en-US%22%3E%3CP%3EIn%20this%20article%2C%20I%20explain%20connect%20to%20virtual%20COM%20port%20from%20UWP%20app.%3C%2FP%3E%0A%3CP%3EIn%20Windows%2010%20APIs%2C%20there%20are%20some%20classes%20to%20access%20serial%20device%2C%20one%20of%20those%20is%20Windows.Devices.SerialCommunication.SerialDevice%20class.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAfter%20define%20following%20capability%20to%20Pakcage.appxmanifest%3A%3C%2FP%3E%0A%3CPRE%3E%26lt%3BDeviceCapability%20Name%3D%22serialCommunication%22%26gt%3B%0A%20%20%26lt%3BDevice%20Id%3D%22any%22%26gt%3B%0A%20%20%20%20%26lt%3BFunction%20Type%3D%22name%3AserialPort%22%20%2F%26gt%3B%0A%20%20%26lt%3B%2FDevice%26gt%3B%0A%26lt%3B%2FDeviceCapability%26gt%3B%3C%2FPRE%3E%0A%3CP%3EIt%20works%20fine%20to%20access%20to%20COM%20port%2C%20but%20it%20doesn't%20works%20for%20virtual%20COM%20port.%3C%2FP%3E%0A%3CP%3EIf%20you%20tried%20to%20get%20SerialDevice%20instance%20of%20virtual%20COM%20port%2C%20you%20would%20not%20get%2C%20SerialDevice.FromIdAsync%20method%20would%20return%20null.%3C%2FP%3E%0A%3CPRE%3Evar%20deviceColletion%20%3D%20await%20DeviceInformation.FindAllAsync(%22COM3%22)%3B%0Aif%20(deviceColletion.Any())%0A%7B%0A%20%20%20%20var%20device%20%3D%20deviceColletion.First()%3B%0A%20%20%20%20var%20s%20%3D%20await%20SerialDevice.FromIdAsync(device.Id)%3B%20%2F%2F%20return%20null%0A%7D%3C%2FPRE%3E%0A%3CP%3EIf%20you%20want%20to%20write%2Fread%20to%20virtual%20COM%20port%2C%20you%20can%20use%20System.IO.Ports.SerialPort%20class.%3C%2FP%3E%0A%3CP%3EIt%20can%20Open%20virtual%20COM%20ports%2C%20and%20write%20%2F%20read%20data%20to%20the%20virtual%20COM%20port.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EBut%2C%20there%20is%20a%20limitation.%20SerialPort%20class%20on%20UWP%20can't%20list%20ports%20using%20GetPortNames%20method.%20If%20the%20method%20was%20used%2C%20then%20PlatformNotSupportedException%20would%20be%20occurred.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESo%2C%20I%20created%20a%20sample%20app%20using%20it.%3C%2FP%3E%0A%3CP%3EThe%20sample%20app%20writes%20%2F%20reads%20to%20virtual%20COM%20port%20COM3%20for%20reading%20and%20COM4%20for%20writing.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20tested%20the%20app%20on%20an%20environment%20that%20is%20set%20up%20using%20com0com%2C%20like%20following%3A%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20448px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F120636iF826DA39BFB8F572%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22%E3%82%B3%E3%83%A1%E3%83%B3%E3%83%88%202019-06-26%20125914.jpg%22%20title%3D%22%E3%82%B3%E3%83%A1%E3%83%B3%E3%83%88%202019-06-26%20125914.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EThe%20result%20of%20the%20app%20is%20as%20below%3A%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20692px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F120637iFFD4964680365DB7%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22vcomapp3.gif%22%20title%3D%22vcomapp3.gif%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EThe%20source%20code%20is%20on%20following%20GitHub%20repository.%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Frunceel%2FVirtualCOMPortApp%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2Frunceel%2FVirtualCOMPortApp%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENext%20article%3A%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2FWindows-Dev-AppConsult%2FGet-list-of-virtual-serial-COM-port-from-UWP-app%2Fba-p%2F723207%22%20target%3D%22_self%22%3EGet%20list%20of%20virtual%20serial%20COM%20port%20from%20UWP%20app%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThanks.%3C%2FP%3E%3C%2FLINGO-BODY%3E
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.