0

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?

Daniel
  • 242
  • 2
  • 12
  • Have you added serial device [capability](https://learn.microsoft.com/en-us/uwp/api/windows.devices.serialcommunication) into the `Package.appxmanifest`? – Nico Zhu Feb 09 '18 at 03:30
  • Sure I did. I use the exect same project for testing with the hardware. I just don't always have the hardware at the moment. It's just a try to make it more convenient for me. – Daniel Feb 09 '18 at 17:38
  • I have a very similar problem here: https://stackoverflow.com/questions/51219062/uwp-usb-hid-device-wont-connect – Christian Findlay Jul 08 '18 at 09:37
  • @NicoZhu-MSFT , it's impossible for some devices. For me. This doesn't work: 'vidpid:‭2C97‬ 0001' but this works fine: 'vidpid:534C 0001' – Christian Findlay Jul 08 '18 at 09:38
  • @NicoZhu-MSFT why does the parser accept some devices and not others? The string 'vidpid:‭2C97‬ 0001' is perfectly valid according to the regex any|vidpid:[0-9a-fA-F]{4} [0-9a-fA-F]{4}( (usb|bluetooth))?|model:[^;]{1,512};.{1,512} – Christian Findlay Jul 08 '18 at 09:39

2 Answers2

3

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.

Nico Zhu
  • 32,367
  • 2
  • 15
  • 36
  • Thank you for the answer. That's sad to hear... Since I write a UWP app that is not going to be used on a PC but on an IoT device I can't really use the Win32 equivalent since it may provide different results. Would it be possible to use 2 USB RS232 adapters and connect them with each other? – Daniel Feb 09 '18 at 17:35
  • This is driving me insane! I'm pretty sure that that parser here has a bug in RegEx . I have two device IDs. One parses fine and the other gives an error, and yet when I use the regex specified with another regex parser, it matches fine. This works fine 'vidpid:534C 0001' , but this doesn't 'vidpid:‭2C97‬ 0001' even though this is a perfectly legit device vid. – Christian Findlay Jul 08 '18 at 09:29
  • I am not able to enumerate Win32 device using the normal Windows API calls. They just don't show up in a UWP app – Christian Findlay Jul 08 '18 at 09:35
1

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

Community
  • 1
  • 1
Tamer
  • 105
  • 8