Hi - 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.
not sure what library you’re using to read and write bytes. The method names you have there are rather generic, and you don’t identify at all what they are.
But, most libraries block while reading and writing bytes. And there is always latency in doing so.
This means that while you read and/or write, if you do so on the main thread, the application with freeze until complete.
It’s best to do these sorts of thing asynchronously.
Thanks for getting back. Can you suggest how I would go about not reading/writing to the serial port simultaneously ?
Would I have to open the port, read from it, close the port, open the port, write to it ?
This is the full script:
using UnityEngine;
using System.Collections;
using System.IO;
using System.IO.Ports;
using System.Threading;
public class JoeysInputStick : MonoBehaviour {
public static SerialPort sp = new SerialPort(“/dev/cu.usbmodem1411”, 9600);
public static int flex = 0;
private int flexValue;