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?
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
andPID
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.