Search code examples
unity-game-engineserial-port

Unity does not read values from the Serial port correctly, help please


I am connecting an Arduino to the Unity game engine and I need them to communicate through the serial ports but for some reason, Unity does not read the data correctly and instead gives me random values between 0 and 300. this is only the problem with Unity since the Arduino IDE's serial monitor reads everything correctly. I am not sure if this is the right forum to ask but nobody ever replied to my question on the Unity forum

here is code from Unity :)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO.Ports;

public class UIcontroler : MonoBehaviour
{
    SerialPort data_stream = new SerialPort("COM3", 9600);
    public string receivedstring;
    public string[] datas;
    public Text text;

    void Start()
    {
        data_stream.Open();
        data_stream.ReadTimeout = 5000;
    }


    void Update()
    {
        receivedstring = data_stream.ReadLine();
        text.text = receivedstring;
    }
}

Solution

  • actually what I had to do was put a delay of around 10ms in my void Update() on the Arduino code