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[] {"\n"}, 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 think I might have found the solution: I reduced the asynchronous routine above to a simple while loop which is true as long as the CO$$anonymous$$ port is open, and it seems to work. Framerate constant, data arriving and showing fast in a debug-textbox. I'll now investigate the actual timelapse between to read lines.
Answer by emthele · Jun 09, 2016 at 11:31 AM
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.
Hi, I've been working on Arduino in Unity and have just experienced the same error using ReadExisting(). The strange thing is, it is working perfectly on my mac, I get really time responses. But now I've ported over to windows and I cant get ReadExisiting() to work at all. Only ReadLine() works but is so unusably slow it is not a solution.
Do you have any idea why ReadExisting doesn't work on windows but is O$$anonymous$$ on mac. I have tried using Read() as suggested but I'm struggling to implement. Can any further explanation be given?
Your answer
Follow this Question
Related Questions
Open Source Tilt Brush black and white screen on oculus quest 0 Answers
Looking for way to get sensor data from Wahoo KICKR directly to Unity for desktop game. 1 Answer
Really high rendering time in build. 1 Answer
HDRP visual errors with VR 0 Answers
Models used for Hand Presence in UnityXR pop out of existence when close to the camera 0 Answers