UWP 未连接到虚拟串口

UWP doesn't connect to virtual serial port

有时我将无法访问我正在编程的硬件,或者调试不好。 所以这就是为什么我想让我的生活更轻松一些并使用虚拟串行端口。

我选择使用 com0com,因为它的设置非常简单,而且是免费的。

我的问题是我的 UWP 应用确实找到了端口但无法连接到它。

我使用的代码是:

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;
        }

    }

我知道代码本身可以正常工作,因为我将它与这样的硬件一起使用。我唯一改变的是我直接搜索COM7端口。

当我调试我的代码时,我可以看到端口已找到并加载到 "device[0]" 但是当我 运行 FromIdAsync 方法时,它没有加载到 "devices" 中。

是我做错了什么还是 UWP 不能使用虚拟端口?

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.

对于 UWP,它对串口文件的访问非常有限。它在 AppxManifestType.xsd 中定义了特定的 DeviceId 方案。当您调用 SerialDevice.FromIdAsync() 方法时,如果您的设备 ID 不匹配,它将匹配以下方案。该方法不会 return SerialDevice。所以,UWP暂时不支持可视串口。

<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>

Win32应用程序访问串口限制较少,可直接连接可视串口。

可能这与您要求的虚拟端口问题没有直接关系,它对 UWP 中的串口问题有一点启发。正如@Nico Zhu 所说,UWP 对串行端口的访问权限有限。

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