Dear Community,
I wanna read 9 different sensor values from an Arduino in Unity. The values are stored in one single line per reading with delay(100) e.g. “# -0.01 # -0.02 # 0.88 # 31.49 # -1.18 # 0.11 # -2.21 # 0 # 0”. However when I want to read out all values in unity not all values are transferred. How can I sync the reading so that all values are updated correctly?
public class SphereManipulator : MonoBehaviour
{
public GameObject panel;
SerialPort sp = new SerialPort("/dev/cu.usbmodem14201", 9600); // opens a new serial port to the Arduino
private string[] rawValues; // represents the values from the Arduino
// Start is called before the first frame update
void Start()
{
sp.Open();
sp.ReadTimeout = 1; // necessary so that unity can find the port
rawValues = new string[9];
parseData();
}
// Update is called once per frame
void Update()
{
if (sp.IsOpen)
{
try
{
bool check = parseData();
if (check)
print(rawValues[0] + " " + rawValues[1] + " " + rawValues[2] + " " + rawValues[3] + " " + rawValues[4]);
}
catch (System.Exception)
{
}
}
}
private bool parseData()
{
string data = sp.ReadLine();
rawValues = data.Split('#');
// Checks if data input is complete
foreach (var sensorValue in rawValues)
{
if (sensorValue == "")
return false;
}
return true;
}
}