- Home /
How to stop waiting on Serial Output
I have this code that reads the serial port from a serial connection to an arduino board
void Update()
{
receivedString = data_stream.ReadLine();
//do stuff
}
My problem is, that Unity waits until a new line is sent on the Serial connection, resulting in very low fps.
How can I make Unity run normally until a new line is printed on the Serial? I hope this does not require a multithreading solution...
Answer by hsmoon5458 · Jan 21, 2021 at 08:08 PM
One easy wat to boost up your fps is, read the data stream fast manually.
you can use InvokeRepeating function to do that.
This video might help
Answer by andrew-lukasik · Jan 26, 2021 at 10:54 PM
Threading doesn't has to be complicated you know
using System.Threading.Tasks;
async void Start ()
{
while( this.enabled )
{
receivedString = await Task.Run( () => {
try
{
return data_stream.ReadLine();
}
catch( System.Exception ex )
{
Debug.LogException( ex );
return "- exception -";
}
} );
Debug.Log($"received string: '{receivedString}'");
await Task.Delay( 1000 );
}
}
Your answer
Follow this Question
Related Questions
Arduino with Unity: Bad Framerate! 3 Answers
send string to serial port on android 0 Answers
How to List Available Com Ports? 0 Answers
Trying to read an integer value from ReadLine() 1 Answer
Handle input from serial device 0 Answers