locked
[UWP]Windows 10 Universal app serial connection RRS feed

  • Question

  • Hi I have been searching the internet for over a week for a solution for this now and hope that someone can offer assistance as it is now severely holding up a number of projects. I have managed to carry out what I need to do with several different languages and platforms but I have hit a road block with Windows and would really appreciate any help on this.

    What I am trying to do is simply read and write data to and from an Arduino via serial. I cannot use Arduino Remote etc as firmata is conflicting with the libraries I need for the fingerprint sensor and NFC reader. In my existing examples it takes only a couple of lines of code to complete this but I have tried multiple C# methods and have hit a road block with all of them. For example in a python application that I completed in a couple of hours I used the following code to simply read the serial:

    baud = 9600 port = '/dev/ttyACM0'
    ser = serial.Serial(port, baud) ser.timeout = 0 var message = ser.read(9);

    I cannot find anywhere a similar way of doing this from a C# universal application and it is driving me nuts that something so simple seems to not be possible.

    After searching for almost a week I came across this on SO:

    using System.IO.Ports;
    
        SerialPort port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
        port.Open();
        byte b = port.ReadByte(); // etc

    I nearly had a heartattack :D I thought I had finally found the answer but alas System.IO.Ports does not seem to be supported in Windows 10 Universal Apps.

    There has to be a simple way to do this, there is no reason I know of why it is not possible and would really appreciate some help with this if anyone can shed any light.

    Thanks in advance

    Adam


    • Edited by Krunal Parekh Friday, December 25, 2015 5:49 AM subject
    Thursday, December 24, 2015 5:46 PM

Answers

  • Hello AdamMiltonBarker,

    Please see: Windows.Devices.SerialCommunication namespace

    The Windows.Devices.SerialCommunication namespace defines Windows Runtime classes that a Windows store app can use to communicate with a device that exposes a serial port or some abstraction of a serial port.  Your Windows Store app must include device capabilities in its App package manifest.

    Please see this UWP sample: Serial Arduino sample

    With Regards,

    Krunal Parekh


    Thanks MSDN Community Support Please remember to Mark as Answer the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Friday, December 25, 2015 6:11 AM

All replies

  • Hello AdamMiltonBarker,

    Please see: Windows.Devices.SerialCommunication namespace

    The Windows.Devices.SerialCommunication namespace defines Windows Runtime classes that a Windows store app can use to communicate with a device that exposes a serial port or some abstraction of a serial port.  Your Windows Store app must include device capabilities in its App package manifest.

    Please see this UWP sample: Serial Arduino sample

    With Regards,

    Krunal Parekh


    Thanks MSDN Community Support Please remember to Mark as Answer the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Friday, December 25, 2015 6:11 AM
  • Thanks for the reply, I have gone through pretty much every topic about this, tried every example and sample and have not got this working. The final attempt I had at getting this working was as follows:

    private async void loginContinueClick(object sender, RoutedEventArgs e)
        {
    
            var filter = SerialDevice.GetDeviceSelector("COM5");
            var devices = await DeviceInformation.FindAllAsync(filter);
            if (devices.Any())
            {
                var deviceId = devices.First().Id;
                this.device = await SerialDevice.FromIdAsync(deviceId);
    
                if (this.device != null)
                {
                    this.device.BaudRate = 9600;
                    this.device.StopBits = SerialStopBitCount.One;
                    this.device.DataBits = 8;
                    this.device.Parity = SerialParity.None;
                    this.device.Handshake = SerialHandshake.None;
    
                    this.dataReader = new DataReader(this.device.InputStream);
    
                    Speak("Device found");
    
                    while (true)
                    {
                        var bytesRecieved = await this.dataReader.LoadAsync(128);
                        if (bytesRecieved > 0)
                        {
    
                            ElementsRead.Text = this.dataReader.ReadString(bytesRecieved).Trim();
                        }
                    }
    
                }
            }
    
    
        }

    It is not a Windows Store App it is a Universal Windows App.

    I have a simple test sketch on an Arduino Uno which is just is just looping and printing HERE into serial. The app manifest is correct The code gets to speak fine and says device found, but then nothing I can't actually read from the serial on com5. Can anyone point out where may be going wrong. I have spent that much time going through docs and examples I totally rewrote everything and just finishing up final stages of integrating MQTT on the app to communicate with my gateway. If I can get to the bottom of the serial issue it is more preferred method for me though.

    Friday, December 25, 2015 7:49 AM
  • I have got this working now.
    Monday, December 28, 2015 1:23 PM