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?