SerialPort AsyncWrite blocking unity issue

I am trying to implement a simple asynchronous communication to a device through a SerialPort.
The communication is through a protocol: I need to send a command then receive the response.
My problem is SerialPort.BaseStream.WriteAsync is just blocking the whole program and I need to reset Unity.
What I am sure about is that the message is getting there (Signal (5 BEEPS) from the device).
I am new to this type of communication and I can’t figure out what I am doing wrong.
Here is my sample code :

using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using System.IO.Ports;
    using System.IO;
    using System.Text;
    using System.Threading.Tasks;
    using System.Threading;
    public class scr : MonoBehaviour
    {
        CancellationTokenSource tokenSource = new CancellationTokenSource(1000);
        public SerialPort sp;
        string command = "BEEP 5\r";
        byte[] response;
        // Start is called before the first frame update
        void Start()
        {
            response = new byte[200];
            Task write = connectAsync();
            Debug.Log("readAsync on.");
            write.Wait();
            var x = write.Status;
            Debug.Log("write results:"+x);
        }
  
        // Update is called once per frame
        void Update()
        {
          
        }
  
        public async Task connectAsync()
        {
            sp = new SerialPort("COM5");
            sp.Open();
            await sp.BaseStream.WriteAsync(Encoding.ASCII.GetBytes(command), 0, command.Length, tokenSource.Token);
        }
    }

Your call to write.Wait() on line 21 blocks the calling thread until the task completes. That is its purpose. Misunderstanding of what that line does I’m guessing :slight_smile: