Serial port – Windows UWP Windows.devices.serialCommunication.SerialDevice does not work

Is it just me, or is it a bug?

serialPort = await SerialDevice.FromIdAsync(Id);

serialPort is always null, even if Id is not.

I need this to work. Now I just write very “fast and dirty” code to test serial communication from a Windows 10 Universal application. I debugged the same result in both x86 and x64.

This is where I am now, but I can’t go very far without creating a serialPort…

public class SerialComm
{
private SerialDevice serialPort;
DataWriter dataWriteObject = null;
DataReader dataReaderObject = null;

public async void StartTest()
{

var deviceSelector = SerialDevice.GetDeviceSelector("COM3");
var myDevices = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(deviceSelector);
var myCurrentDevice = myDevices[0];
string Id = myCurrentDevice.Id.ToString();

try
{
serialPort = await SerialDevice.FromIdAsync(Id);
}
catch (Exception)< br /> {

throw;
}

StringBuilder commandBuilder = new StringBuilder();

while (true)
{< br /> var rBuffer = (new byte[1]).AsBuffer();
await serialPort.InputStream.ReadAsync(rBuffer, 1, InputStreamOptions.Partial);

if ((char )rBuffer.ToArray()[0] !=' ')
{
commandBuilder.Append((char)rBuffer.ToArray()[0]);
}
else
{
string temp = "";

try
{
temp += rBuffer.ToString();
}< br /> catch (Exception)
{
temp = "Error";
}

commandBuilder.Append(temp);
}

string stringToDisplay = commandBuilder.ToString();
}

Thank you for your help and suggestions…

For the Maxbotix sensor using FTDI chip for USB to serial communication, I encountered the same problem. I can connect to Device, I can use it from the SerialPort class of the real .NET Framework, but in the UWP SerialSample of GitHub and my code, SerialDevice.FromIdAsync() returns null.

For me Say, the solution is divided into two parts.

The first part is to add device functions to the Package.appxmanifest file:





The second part is to download the updated driver from the FTDI Web site (I am using version 2.12.06). Once I do this, it starts to work.

The following complete example:

var aqsFilter = SerialDevice.GetDeviceSelector("COM3");
var devices = await DeviceInformation.FindAllAsync(aqsFilter);
if (devices.Any())
{
var deviceId = devices.First().Id;
this.device = awai t SerialDevice.FromIdAsync(deviceId);

if (this.device != null)
{
this.device.BaudRate = 57600;
this.device. StopBits = SerialStopBitCount.One;
this.device.DataBits = 8;
this.device.Parity = SerialParity.None;
this.device.Handshake = SerialHandshake.None;

this.reader = new DataReader(this.device.InputStream);
}
}

It’s just me, or this bug ?

serialPort = await SerialDevice.FromIdAsync(Id);

serialPort is always null, even if Id is not.

I need this to work. Now I just write very “fast and dirty” code to test serial communication from a Windows 10 Universal application. I debugged the same result in both x86 and x64.

This is where I am now, but I can’t go very far without creating a serialPort…

public class SerialComm
{
private SerialDevice serialPort;
DataWriter dataWriteObject = null;
DataReader dataReaderObject = null;

public async void StartTest()
{

var deviceSelector = SerialDevice.GetDeviceSelector("COM3");
var myDevices = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(deviceSelector);
var myCurrentDevice = myDevices[0];
string Id = myCurrentDevice.Id.ToString();

try
{
serialPort = await SerialDevice.FromIdAsync(Id);
}
catch (Exception)< br /> {< br />
throw;
}

StringBuilder commandBuilder = new StringBuilder();

while (true)
{
var rBuffer = (new byte[1]).AsBuffer();
await serialPort.InputStream.ReadAsync(rBuffer, 1, InputStreamOptions.Partial);

if ((char)rBuffer .ToArray()[0] !=' ')
{
commandBuilder.Append((char)rBuffer.ToArray()[0]);
}
else
{
string temp = "";

try
{
temp += rBuffer.ToString();
}
catch (Exception)
{
temp = "Error";
}

commandBuilder.Append(temp);
}

string stringToDisplay = commandBuilder.ToString();
}

Thank you for your help and suggestions…

For the Maxbotix sensor using FTDI chip for USB to serial communication, I encountered the same problem. I can connect to the device nicely in the terminal program, and I can use it from the SerialPort class of the real .NET Framework It, but in GitHub’s UWP SerialSample and my code, SerialDevice.FromIdAsync() returns null.

For me, the solution is divided into two parts.

The first part is to add device functions to the Package.appxmanifest file:





The second part is to download the updated driver from the FTDI Web site ( I am using version 2.12.06). Once I did this, it started to work.

The following complete example:

var aqsFilter = SerialDevice .GetDeviceSelector("COM3");
var devices = await DeviceInformation.FindAllAsync(aqsFilter);
if (devices.Any())
{
var deviceId = devices.First ().Id;
this.device = await SerialDevice.FromIdAsync(deviceId);

if (this.device != null)
{
this.device.BaudRate = 57600;
this.device.StopBits = SerialStopBitCount.One;
this.device.DataBits = 8 ;
this.device.Parity = SerialParity.None;
this.device.Handshake = SerialHandshake.None;

this.reader = new DataReader(this.device.InputStream) ;
}
}

Leave a Comment

Your email address will not be published.