Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 · Mar 13, 2019 at 03:52 AM · serializationarduinoserialportserial.io.port

Problems With Sending Data through Serial Port

I am currently working on a project that communicates between Arduino and Unity through the serial port. I got sending data to unity working great but when I try to send data from unity to Arduino with Serial port.WriteLine it keeps giving me the error, "IOException: The semaphore timeout period has expired." This error seems to freeze all data sent to Arduino because when I stop the game, all the commands get through to the Arduino at the same time. It also is not large amounts of data so I don't know why I would have bus space issues. Only 2 digit numbers.

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 Cornelis-de-Jager · Mar 13, 2019 at 05:20 AM 0
Share

you should post your code you use to communicate between the two - but a simplified version. Remove all the unnecessary code.

avatar image saml_baker Cornelis-de-Jager · Mar 13, 2019 at 10:29 PM 0
Share

O$$anonymous$$ here is the code for Arduino that reads the data inside void loop. (I also already attach edservo1 in the setup function.)

 if(Serial.available() > 0)
   {
     String incom$$anonymous$$gByte = Serial.readString();
     
       int data = incom$$anonymous$$gByte.toInt();
   
       servo1.write(data);
       delay(64); 
   }

and this is the Unity code that sends the data:

 using UnityEngine;
 using System.Collections;
 using System.IO.Ports;
 
 public class Arduino : $$anonymous$$onoBehaviour
 {
 
     public SerialPort sp;
     public GameObject hand;
 
     float timer = 0;
 
     void Start()
     {
         sp = new SerialPort("CO$$anonymous$$6", 9600, Parity.None, 8, StopBits.One); 
 
         sp.DtrEnable = false; 
 
         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");
 
     }
 
     void Update()
     {
         int coord = $$anonymous$$athf.RoundToInt(-hand.gameObject.transform.rotation.x);
 
 
         if(Time.deltaTime >= timer) //add delay to serial flow.
         {
             sp.WriteLine(coord.ToString());
             timer = Time.deltaTime + 0.5f;
         }
 
     }
 
 }
 

I am just trying to set a servo's position to the rotation of the gameobject "hand" in my scene.

avatar image Taunchi · Mar 13, 2019 at 05:30 AM 0
Share

Could it be that you're trying to send information too frequently? At what point are you sending the data? And have you tried re-uploading your sketch to the arduino?

avatar image saml_baker Taunchi · Mar 13, 2019 at 10:30 PM 0
Share

No, because I have a delay set for the data so it only updates every .5 seconds. And yes I have tried reuploading the code.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by itisieric · Jun 15, 2020 at 07:54 AM

Did you find a solution to this?

Comment
Add comment · Show 1 · Share
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 saml_baker · Jun 16, 2020 at 12:08 AM 0
Share

Yes, I raised the read/write timeout to something like 500 I think. Did it for me.

Also added "delay(10);" and "Serial.flush();" in the Arduino code after reading the inco$$anonymous$$g data from Unity but before sending new data if you have to send any to Unity.

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

106 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

Related Questions

Read in 2 Different Arduino Values 1 Answer

The Semaphore Timeout Period Has Expired 0 Answers

Need Help Connecting Arduino to Unity 0 Answers

Unity Arduino Communication Problem 0 Answers

Unity to Arduino (write serial) Framerate fall 2 Answers


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