Windows UWP Windows.Devices.SerialCommunication.SerialDevice Not working

13,049

Solution 1

I had the same problem with a Maxbotix sensor that was using an FTDI chip for the USB-to-serial communication. I could connect to the device fine in a terminal program, and I could use it from the real .NET Framework's SerialPort class, but in both the UWP SerialSample from GitHub and my code, SerialDevice.FromIdAsync() returned null.

For me, the solution was in two parts.

The first part was to add the device capability in the Package.appxmanifest file:

<DeviceCapability Name="serialcommunication">
  <Device Id="any">
    <Function Type="name:serialPort" />
  </Device>
</DeviceCapability>

The second part was to download an updated driver (I used version 2.12.06) from the FTDI Web site. As soon as I did this, it started working.

Full sample below:

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

Solution 2

If the "COM3" Serial Port you are trying to open is an onboard Serial Port, then the current design of the Serial Communication class does not allow accessing on-board serial ports. The Serial Communication class only supports USB-To-Serial communication, not direct serial communication. In your code above, what is the value of myDevices[0].Port.Name?

Share:
13,049

Related videos on Youtube

BernardG
Author by

BernardG

I started programming many moons ago, left to live in the USA for a while, and came back to my roots in 2008. Programming is (generally) fun for me, and I love discovering new tools and methods to work with. I am fully immersed in the .Net Framework now, after having used Clarion for a long time. I do contract work in different areas of France. I love to see the current evolution of programming from an art form to a more structured, industrial process. Which is why, beside purely programming, I am interested in Continuous Integration, Source Control and ways to automate this. Beside computers and programming, I love airplanes, and model airplanes, so don't be surprised if there are some entries about that, too, in my blog.

Updated on June 04, 2022

Comments

  • BernardG
    BernardG almost 2 years

    Is it just me, or is this a bug?

    serialPort = await SerialDevice.FromIdAsync(Id);
    

    serialPort is always null, even while Id is not.

    I need to have this working. For now I am just writing very "quick and dirty" code to test serial communication from a Windows 10 Universal app. I debugged in both x86 and x64 with same result.

    Here is where I am at for now, but I can't go very far without a serialPort being created....

    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)
                {
    
                    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] != '\n')
                    {
                        commandBuilder.Append((char)rBuffer.ToArray()[0]);
                    }
                    else
                    {
                        string temp = "";
    
                        try
                        {
                            temp += rBuffer.ToString();
                        }
                        catch (Exception)
                        {
                            temp = "Error";
                        }
    
                        commandBuilder.Append(temp);
                    }
    
                    string stringToDisplay = commandBuilder.ToString();
                }
    

    Thanks for your help and advices....

    • user1703401
      user1703401
      The docs are entirely too shoddy to have an informed answer. I'll randomly guess that, since there is no explicit Open() method, you can't get one because the device is already in use. Or you forgot to ask for the "serialcommunication" capability in the manifest.