I’m trying to have unity read in some serial data from an arduino in the serial log. I’m sending potentiometer data from an arduino to unity, right now I just want to read it in the Debug log.
The code kinda works, the data is sending correctly. But there’s a solid 10 second delay from when I move the potentiometer, to when the data shows up in the Unity log. The serial window in arduino works, and I wrote some similar code in visual basic, and it works fine in the VS console window too, so the problem is in unity. It doesn’t seem to be missing readings either, it’s just a very long delay before the data shows up.
My code is below, anyone have any ideas?
Also, to clarify, the FPS seems to be fine, the game is still perfectly responsive to the standard first person controller script. It’s not slowing down the game, it just seems like the data is delayed by a few seconds.
//Unity Code:
using UnityEngine;
using System.Collections;
using System.Threading;
using System.IO.Ports;
public class serialcom : MonoBehaviour
{
public static SerialPort sp;
public static string x;
public static string[] data;
public static string lin;
public static string rot;
public int debugcount = 1;
// Use this for initialization
void Start ()
{
//Debug.Log ("Code started");
OpenConnection();
//Debug.Log ("initialzed properly");
}
void Update()
{
try{
x = sp.ReadLine ();
data = x.Split(' ');
lin = data[0];
rot = data[1];
Debug.Log (lin + " " + rot);
}
catch{Debug.Log ("Please Work");}
}
public void OpenConnection()
{
sp = new SerialPort("COM4", 9600, Parity.None, 8, StopBits.One);
Debug.Log ("OpenConnection started");
if (sp != null)
{
if (sp.IsOpen)
{
sp.Close();
Debug.Log ("Closing port, because it was already open!");
}
else
{
sp.Open(); // opens the connection
sp.ReadTimeout = 100; // sets the timeout value before reporting error
Debug.Log("Port Opened!");
}
}
else
{
if (sp.IsOpen)
{
print("Port is already open");
}
else
{
print("Port == null");
}
}
Debug.Log ("Open Connection finished running");
}
void OnApplicationQuit()
{
if (sp != null)
sp.Close();
}
}
// Ardiuno code:
int pot1 = A0;
int pot2 = A1;
int pot1volt;
int pot2volt;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
pot1volt = analogRead(pot1);
pot2volt = analogRead(pot2);
Serial.print(pot1volt);
Serial.print(" ");
Serial.print(pot2volt);
Serial.println();
delay(1);
}