Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by saml_baker · Jan 19, 2019 at 05:19 AM · errorserializationerror-messagearduinoserialport

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!

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image WARdd · Aug 16, 2019 at 02:39 PM 0
Share

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

avatar image saml_baker WARdd · Aug 18, 2019 at 02:05 AM 0
Share

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);

 


avatar image sacredgeometry · Aug 16, 2019 at 07:34 PM 0
Share

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?

avatar image WARdd sacredgeometry · Aug 17, 2019 at 04:49 PM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

133 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges