A Microsoft platform for building and publishing apps for Windows devices.
UWP Serial Port Returns null whenever serialPort = await SerialDevice.FromIdAsync(deviceId) is run
I am trying to get the serial port information for the following code but it keeps returning null the device id is coming back so I'm not sure why is isn't working?
string qFilter = SerialDevice.GetDeviceSelector("COM3");
DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(qFilter);
if (devices.Any())
{
string deviceId = devices.First().Id;
Debug.WriteLine(" This is the device ID: " + deviceId);
await OpenPort(deviceId);
}
ReadCancellationTokenSource = new CancellationTokenSource();
while (true)
{
Debug.WriteLine("Listen function is about to run.");
await Listen();
// added to serial code to break from infinite loop add
// console log here to see if data is being read correctly
// in listen function
break;
}
The following open port is getting an Id but the serial port is null please advise.
private async Task OpenPort(string deviceId)
{
Debug.WriteLine("OpenPort Function has started");
serialPort = await SerialDevice.FromIdAsync(deviceId);
Debug.WriteLine("SerialPort:", serialPort);
if (serialPort != null)
{
Debug.WriteLine("SerialPort not null in Openport Function");
serialPort.WriteTimeout = TimeSpan.FromMilliseconds(1000);
serialPort.ReadTimeout = TimeSpan.FromMilliseconds(1000);
serialPort.BaudRate = 9600;
serialPort.Parity = SerialParity.None;
serialPort.StopBits = SerialStopBitCount.One;
serialPort.DataBits = 8;
serialPort.Handshake = SerialHandshake.None;
}
}
The device I am trying to connect to is: