Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Automatic Timeout with Serial Ports

Discussion in 'Scripting' started by ATLAS-INTERACTIVE, Aug 8, 2015.

  1. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    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.

    Code (CSharp):
    1. TimeoutException: The operation has timed-out.
    2. System.IO.Ports.WinSerialStream.Read (System.Byte[] buffer, Int32 offset, Int32 count)
    3. System.IO.Ports.SerialPort.read_byte ()
    4. System.IO.Ports.SerialPort.ReadByte ()
    5. (wrapper remoting-invoke-with-check) System.IO.Ports.SerialPort:ReadByte ()
    6. ArduinoInput.Update () (at Assets/ArduinoInput.cs:25)
    Code (CSharp):
    1. using UnityEngine;
    2. using System.IO.Ports;
    3.  
    4. public class ArduinoInput : MonoBehaviour {
    5.  
    6.     public float speed;
    7.     private float amountToMove;
    8.  
    9.     SerialPort sp = new SerialPort("COM4", 9600);
    10.  
    11.     void Start ()
    12.     {
    13.         sp.Open();
    14.         sp.ReadTimeout = 1;
    15.     }
    16.  
    17.     void Update ()
    18.     {
    19.         amountToMove = speed * Time.deltaTime;
    20.  
    21.         if (sp.IsOpen)
    22.         {
    23.             try
    24.             {
    25.                 MoveObject(sp.ReadByte());
    26.                 print(sp.ReadByte());
    27.             }
    28.  
    29.             catch (System.Exception)
    30.             {
    31.                 throw;
    32.             }
    33.         }
    34.     }
    35.  
    36.     void MoveObject(int direction)
    37.     {
    38.         if (direction == 1)
    39.         {
    40.             transform.Translate(Vector3.left * amountToMove, Space.World);
    41.         }
    42.  
    43.         if (direction == 2)
    44.         {
    45.             transform.Translate(Vector3.right * amountToMove, Space.World);
    46.         }
    47.     }
    48. }
     
  2. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    Update:

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

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

    TheMohima95

    Joined:
    Mar 11, 2016
    Posts:
    1
    Hi, I'm getting the exact same error as you. Did you ever manage to get round this problem?
     
  4. pjtrudiment

    pjtrudiment

    Joined:
    Jul 1, 2016
    Posts:
    2
    Currenty,I met a simalar problem like this,hope to get an answer
     
  5. jimroberts

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560
    Your timeout is set to 1 millisecond which will most likely timeout instantly. Raise it to a more sane number like 500ms or 1000ms.
     
  6. eyeballTickler

    eyeballTickler

    Joined:
    Jun 13, 2015
    Posts:
    7
    Did anyone ever find a solution to this? Neither changing the delay() value in Arduino no the serial port timeout in Unity helps.
     
  7. jcpilatasig

    jcpilatasig

    Joined:
    Jan 31, 2019
    Posts:
    1
    I had the same problem, I put Serial.flush(); on the arduino code in setup function and boom, the problem solved...
     
  8. Maccyfin

    Maccyfin

    Joined:
    Aug 19, 2012
    Posts:
    87
  9. Ortega-Dev

    Ortega-Dev

    Joined:
    Aug 26, 2019
    Posts:
    1
    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.
     
    KoWhale likes this.
  10. KoWhale

    KoWhale

    Joined:
    Jan 17, 2020
    Posts:
    1
    Thanks for your help Ortega-Dev. None of the above doesnt work. but yours fixed it thanks.
     
  11. Bendorius

    Bendorius

    Joined:
    Jun 2, 2017
    Posts:
    1
    For ppl still looking for an answer to this what I di is before I write any data I set my new line


    Code (CSharp):
    1. portname.SetNewLine = "\n"
    2. portname.Write("something" + "\n")