Daniel
Daniel

Reputation: 342

UWP doesn't connect to virtual serial port

I will not be able to access the hardware I'm programming with sometimes or it's just not good to debug. So that's why I thought to make my life a bit easier and work with a virtual serial port.

I chose to use com0com since it's pretty straight forward to set up and it's free.

My problem is that my UWP app does find the port but can't connect to it.

The code I'm using is:

public async Task<string> Init()
    {
        try
        {
            if (_serialPort == null)
            {
                var aqs = SerialDevice.GetDeviceSelector("COM7");

                var devices = await DeviceInformation.FindAllAsync(aqs, null);

                if (devices.Any())
                {
                    await OpenPort(devices[0].Id);                       

                    return "found port";
                }                
            }
            else
            {
                return "port already configured";
            }

            return "whatever";
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }


private async Task OpenPort(string deviceId)
    {
        try
        {
            _serialPort = await SerialDevice.FromIdAsync(deviceId);

            if (_serialPort != null)
            {
                _serialPort.WriteTimeout = TimeSpan.FromMilliseconds(1000);
                _serialPort.ReadTimeout = TimeSpan.FromMilliseconds(1000);
                _serialPort.BaudRate = 19200;
                _serialPort.Parity = SerialParity.None;
                _serialPort.StopBits = SerialStopBitCount.One;
                _serialPort.DataBits = 8;
                _serialPort.Handshake = SerialHandshake.None;
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }

I know the code itself is working because I use it with the hardwarelike this. The only thing I changed is that I directly search for the COM7 port.

WHen I debug my code I can see that the port is found and loaded into "device[0]" but it is not loaded into "devices" when i run the FromIdAsync method.

Did I do something wrong or does UWP not work with virtual ports?

Upvotes: 0

Views: 2667

Answers (2)

Nico Zhu
Nico Zhu

Reputation: 32785

My problem is that my UWP app does find the port but can't connect to it. Currently, UWP does not support virtual serial port that with out VID and PID Parameter.

For UWP, it has very limited access to serial port the file. It defines specific DeviceId scheme within AppxManifestType.xsd. When you invoke SerialDevice.FromIdAsync() method, it will match with the following scheme, if your device id does not match. the method will not return SerialDevice. So, UWP does not support visual serial port currently.

<xs:simpleType name="ST_DeviceId">
  <xs:restriction base="ST_NonEmptyString">
    <xs:pattern value="any"/>
    <xs:pattern value="vidpid:[0-9a-fA-F]{4} [0-9a-fA-F]{4}( (usb|bluetooth))?"/>
    <xs:pattern value="model:[^;]{1,512};.{1,512}"/>
  </xs:restriction>
</xs:simpleType>

There are a fewer limits to access serial port in Win32 Application , you could connect to visual serial port directly.

Upvotes: 3

Tamer
Tamer

Reputation: 105

may be this is not directly related to the problem as you asked for the virtual ports, it give a little enlighten on the serial port problem in UWP. and as @Nico Zhu said that the UWP has limited access to Serial Port.

SerialCommunication

System-internal or on-chassis serial ports may be enumerated by DeviceInformation.FindAllAsync(), but cannot be opened by SerialDevice.FromIdAsync() because they currently are not supported. However, serial ports connected over USB, such as on USB-to-Serial cables are supported.

It is possible that the DeviceInformation collection returned by DeviceInformation.FindAllAsync() may have a serial device whose DeviceInformation.Name property is set to the machine name. This is by design and may occur when enumerating an on-board serial port

Upvotes: 1

Related Questions