- Home /
The Semaphore Timeout Period Has Expired
Hello I am currently working on a project that communicates between Arduino and Unity. My goal is for unity to send data to the arduino while two gameobjects are touching. I have this script that links the arduino to unity and recieves/parses data from the arduino.
 using UnityEngine;
 using System.Collections;
 using System.IO.Ports;
 
 public class arduinoMovement : MonoBehaviour {
 
     public SerialPort sp;
     public GameObject indexFinger;
     public GameObject thumbFinger;
     public GameObject hand;
 
     Rigidbody RB;
 
     public float indexPos;
     public float thumbPos;
     public float handX;
     public float handY;
     public float handZ;
 
     public float delay;
 
     public string[] pos;
 
     // Use this for initialization
     void Start()
     {
         RB = indexFinger.GetComponent<Rigidbody>();
 
         sp = new SerialPort("COM6", 9600, Parity.None, 8, StopBits.One); //Replace "COM5" with whatever port your Arduino is on.
         sp.DtrEnable = false; //Prevent the Arduino from rebooting once we connect to it. 
                               //A 10 uF cap across RST and GND will prevent this. Remove cap when programming.
         sp.ReadTimeout = 1; //Shortest possible read time out.
         sp.WriteTimeout = 1; //Shortest possible write time out.
         sp.Open();
         if (sp.IsOpen)
         {
             sp.Write("Hello World");
             Debug.Log("Connection Successful!");
         }
         else
             Debug.LogError("Serial port: " + sp.PortName + " is unavailable");
     }
 
     // Update is called once per frame
     void Update()
     {
         StartCoroutine(ReadDataFromSerialPort());
 
         indexPos = float.Parse(pos[1]);
         thumbPos = float.Parse(pos[0]);
 
         indexFinger.transform.rotation = Quaternion.Slerp(indexFinger.transform.rotation, Quaternion.Euler(handX, handY + 180, -indexPos - 5), Time.deltaTime * delay);
 
         thumbFinger.transform.rotation = Quaternion.Slerp(thumbFinger.transform.rotation, Quaternion.Euler((1.5f * thumbPos), handY + 180, -handZ), Time.deltaTime * delay);
 
     }
 
 
     IEnumerator ReadDataFromSerialPort()
     {
         while (true)
         {
             string values = sp.ReadLine();
             pos = values.Split(',');
 
             yield return new WaitForSeconds(0.005f);
         }
     }
 
 }
I also have a script that sends data to the arduino when two objects collide.
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class arduinoCollision : MonoBehaviour
 {
     public arduinoMovement arduino;
 
     void OnTriggerStay(Collider other)
     {
         if (other.tag == "Index")
         {
             arduino.sp.WriteLine((arduino.indexPos + 10).ToString());
         }
 
     }
 
 }
Whenever the two objects collide I get the message, "The Semaphore Timeout Period Has Expired" and the game and arduino freeze. It works fine when it is onTriggerEnter but I need it to be onTriggerStay so that it is a constant stream of data when the objects are touching. I would really appreciate it if you know what is going on. Thanks!
Have you found a solution for this? I'm struggling with the same error, even worse the arduino seems to work fine when strea$$anonymous$$g the same data from the console
I fixed $$anonymous$$e by clearing the serial buffer every time a new String is sent. I don't know if this will fix your problem but if you are sending data to Arduino via "WriteLine" then try this.
 arduino.sp.ReadExisting(); //reads all previous data and clears it.
 arduino.sp.WriteLine(DATA YOU ARE SENDING);
 
This doesn't sound like a unity error this sounds more like a problem with CO$$anonymous$$s with the arduino.
Have you tried not running it in a coroutine?
I'm finding some things that alleviate the issue but nothing that fixes it:
- Putting it in the main thread ins$$anonymous$$d of a secondary one makes the error slightly more frequent. 
- Reducing the amount of data seems to help a lot, though it only delays the issue. 
- There were also a mistake in the output data being sent to the arduino which would trigger the error after being sent exactly 3 times... no joke. 
Your answer
 
 
             Follow this Question
Related Questions
Problems With Sending Data through Serial Port 1 Answer
Send many ints from Unity To Arduino. 2 Answers
Unity Serializer scripting error 1 Answer
Unable to convert classes to dex format GoogleMobileAds SDK conflicting Facebook SDK 0 Answers
moved project to new version of Unity, Weird stylehseet error 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                