Automatic Timeout with Serial Ports

As part of a side project, I am trying to get input from a serial port, but am having a bit of trouble getting Unity to even run properly.

While using the code below, I get this error on the first frame.

TimeoutException: The operation has timed-out.
System.IO.Ports.WinSerialStream.Read (System.Byte[] buffer, Int32 offset, Int32 count)
System.IO.Ports.SerialPort.read_byte ()
System.IO.Ports.SerialPort.ReadByte ()
(wrapper remoting-invoke-with-check) System.IO.Ports.SerialPort:ReadByte ()
ArduinoInput.Update () (at Assets/ArduinoInput.cs:25)
using UnityEngine;
using System.IO.Ports;

public class ArduinoInput : MonoBehaviour {

    public float speed;
    private float amountToMove;

    SerialPort sp = new SerialPort("COM4", 9600);

    void Start ()
    {
        sp.Open();
        sp.ReadTimeout = 1;
    }
  
    void Update ()
    {
        amountToMove = speed * Time.deltaTime;

        if (sp.IsOpen)
        {
            try
            {
                MoveObject(sp.ReadByte());
                print(sp.ReadByte());
            }

            catch (System.Exception)
            {
                throw;
            }
        }
    }

    void MoveObject(int direction)
    {
        if (direction == 1)
        {
            transform.Translate(Vector3.left * amountToMove, Space.World);
        }

        if (direction == 2)
        {
            transform.Translate(Vector3.right * amountToMove, Space.World);
        }
    }
}

Update:

I have debugged the exception and have gathered the following information from it.

System.TimeoutException: The operation has timed-out.
  at System.IO.Ports.WinSerialStream.Read (System.Byte[] buffer, Int32 offset, Int32 count) [0x00000] in <filename unknown>:0
  at System.IO.Ports.SerialPort.read_byte () [0x00000] in <filename unknown>:0
  at System.IO.Ports.SerialPort.ReadByte () [0x00000] in <filename unknown>:0
  at (wrapper remoting-invoke-with-check) System.IO.Ports.SerialPort:ReadByte ()
  at ArduinoInput.Update () [0x00022] in D:\Labyrith\Tabletop\Assets\ArduinoInput.cs:26
UnityEngine.MonoBehaviour:print(Object)
ArduinoInput:Update() (at Assets/ArduinoInput.cs:34)

Hi, I’m getting the exact same error as you. Did you ever manage to get round this problem?

Currenty,I met a simalar problem like this,hope to get an answer

Your timeout is set to 1 millisecond which will most likely timeout instantly. Raise it to a more sane number like 500ms or 1000ms.

Did anyone ever find a solution to this? Neither changing the delay() value in Arduino no the serial port timeout in Unity helps.

I had the same problem, I put Serial.flush(); on the arduino code in setup function and boom, the problem solved…

Setting my timeOut to 5000 milliseconds fixed it. Im also threading the signal from the arduino. Used this article here:
https://www.aryanbhasin.com/blog/reduce-delays-in-serial-port-communication-in-unity

I had a similar problem where attempting to read from SerialPort in Unity would cause a timeout regardless of what my readTimeout was set to.

Setting DtrEnable = true on my SerialPort object seems to fix it for me.

1 Like

Thanks for your help Ortega-Dev. None of the above doesnt work. but yours fixed it thanks.

1 Like

For ppl still looking for an answer to this what I di is before I write any data I set my new line

portname.SetNewLine = "\n"
portname.Write("something" + "\n")

Setting DtrEnable = true on my SerialPort object seems to fix it for me as well. Thanks!