SerialPort.ReadExisting workaround needed

Hello everyone,

I’m having an issue with the SerialPort.ReadExisting method, which I think is neccessary for my purpose.

I have a set of inertial movement sensors (up to 16) which deliver data every 1 ms via bluetooth serial port profile. So each sensor streams every 16 ms.
In Unity, I want to move the parts of a body (arms, legs etc.) depending on these sensor values.

My first approach with C# was with WinForms, where I could have a DataReceivedHandler and use the ReadExisting() method, then split for lines automatically. In Unity, ReadExisting() seems to be buggy, so I would have to use ReadLine() method instead.
But, with ReadLine(), I would have to call this method asynchronously really fast (around 1 ms) in order to not lose received lines via COM port.

My current codes is a copy of some other code I found on the internet:

 public IEnumerator AsynchronousReadFromSerialPort(Action<string> callback, Action fail = null, float timeout = float.PositiveInfinity)
    {
        DateTime initialTime = DateTime.Now;
        DateTime nowTime;
        TimeSpan diff = default(TimeSpan);
     
        string dataString = null;
    	//string[] serialPortLines;
     
        do
    	{
            try
    		{
                dataString = serialPort.ReadLine();
    			//serialPortLines = serialPort.ReadExisting().Split(new string[] {"

"}, StringSplitOptions.RemoveEmptyEntries);
}
catch (TimeoutException)
{
dataString = null;
//serialPortLines = null;
}

            if (dataString != null)
            {
                callback(dataString);
    			//callback(serialPortLines[0]);
                yield return null;
            }
    		else
    		{
                yield return new WaitForSeconds(0.1f);
    		}
     
            nowTime = DateTime.Now;
            diff = nowTime - initialTime;
     
        } while (diff.Seconds < timeout);
     
        if (fail != null)
            fail();
        yield return null;
    }


    void Update()
        {		
            if (comOpen == true)
    		{
    			
    			StartCoroutine
    			(
    				AsynchronousReadFromSerialPort
    				(   (string s) => Debug.Log(s),     // Callback
    					() => Debug.LogError("Error!"), // Error callback
    					10f                             // Timeout (seconds)
    				)
    			);
}

As you can see, currently I’m using ReadLine() in the Update() function, with is too slow. When I switch the code (commented lines in asynchronous read function) to make use of ReadExisting(), I’m getting this error everytime the ReadExisting() gets called.

NullReferenceException: Object reference not set to an instance of an object
System.IO.Ports.WinSerialStream.get_BytesToRead ()
System.IO.Ports.SerialPort.get_BytesToRead ()
System.IO.Ports.SerialPort.ReadExisting ()
(wrapper remoting-invoke-with-check) System.IO.Ports.SerialPort:ReadExisting ()
SerialComm+<AsynchronousReadFromSerialPort>c__Iterator0.MoveNext () (at Assets/UnityVS/Comports/SerialComm.cs:134)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
SerialComm:Update() (at Assets/UnityVS/Comports/SerialComm.cs:50)

Any help or advice?

Update:

I was thinking wrong by using the ReadExisting() function. I managed to get my code working properly, the same way as previously in Visual Studio, by using SerialPort.Read(). There is simply no need to also read the serial port stream (ReadExisting), the input buffer (Read) is enough.

Now, Every time between the frame updates, I read all data from the serial port input buffer via SerialPort.Read(). I split it into the single data lines and process them. Next time, the same. This way I can transmit data at 1ms constantly, while processing them between the frame updates and not loosing data.

Nevertheless, the ReadExisting() function is still buggy, but that seems to be connected to the bug of checking for existing bytes in the input buffer or stream.