Unity3d app for UWP freeze when using SerialDevice

187 views Asked by At

I have a piece of code that I want to use with Unity 2018.2.14.f1.

#if UNITY_WSA && !UNITY_EDITOR

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Windows.Devices.Enumeration;
using Windows.Devices.SerialCommunication;

namespace SerialForUWP
{
    public class SerialComHelper
    {
        public async static Task<string[]> GetPorts()
        {
            var serials = SerialDevice.GetDeviceSelector();
            var coms = await DeviceInformation.FindAllAsync(serials);

            List<string> results = new List<string>();
            foreach (var device in coms)
            {
                using (var serialDevice = await SerialDevice.FromIdAsync(device.Id))
                {


                    if (serialDevice != null)
                    {
                        var port = serialDevice.PortName;
                        results.Add(port.ToString());
                    }
                }
            }

            return results.ToArray();
        }
    }
}

#endif

I compile for UWP using IL2CPP :

enter image description here

I also already updated the manifest of the cpp projet :

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

As soon as I try to use the GetPorts() function my app freeze.

    void Update()
    {
        text.text = Time.time.ToString();
        if (Input.GetKeyUp(KeyCode.Space))
        {
            try
            {
#if UNITY_WSA && !UNITY_EDITOR
                foreach (var com in SerialForUWP.SerialComHelper.GetPorts().Result)
                {
                    text.text = com;
                }
#endif
            }
            catch (Exception ex)
            {
                text.text = ex.ToString();
            }

        }
    }

I got no error, no exception.

Can anyone help please ?

2

There are 2 answers

0
Filimindji On BEST ANSWER

According to this answer I can't use .Result

1
Joa On

To get the exceptions use:

Debug.Log(ex.ToString()) instead of Text.text = ex.ToString()