0

I have created a game with the unity game engine and have two way serial communication with an arduino. I can send data to Unity using Serial.println without any problem. However when I try and send a letter to Arduino from Unity I am getting incredible latency. The latency is being caused by the following code. It slows down the entire game and also slows down the data being sent to unity from Arduino.

  {
   int lf = 10;
   Serial.readBytesUntil(lf, myCol, 1);
   if(strcmp(myCol,"A")==0){
   digitalWrite(StimPin, HIGH);       
   }

I have read that serial ports can't read/write without causing latency. If this is so can anyone recommend a workaround to have two way communication between arduino & unity without lag ? Thanks Joey

3
  • If you send only a single letter from Unity then you must use Serial.read(). Using Serial.readBytesUntil() like that is only suitable for reading a string terminated by a linefeed, it will take a full second to timeout. Mar 4, 2015 at 9:26
  • Thanks Hans - that eliminates the latency the only issue now is that I am getting an error: "IOException: Resource busy"
    – joeyc
    Mar 4, 2015 at 11:04
  • That's obviously not an error that's ever raised by the code you posted. You need a clairvoyant, not a programmer. Mar 4, 2015 at 11:17

2 Answers 2

0

Try to change approach, I had a project with unity and creating a custom string was much faster than sending. In the Arduino side, try to loop for each char and re-create the string and in the Unity side have a secondary thread looping for any incoming char.

-1

I solved this issue by simply putting timeout to 100ms:

Serial.setTimeout(100);

Reference: https://www.arduino.cc/en/Serial/SetTimeout

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.