Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 benken44 · Apr 05, 2014 at 02:40 PM · frameratecommunicationarduinoserialport

Unity to Arduino (write serial) Framerate fall

Hi !

I send bytes to my arduino (with an interval ) via the serial with unity. All is fine except after some seconds the framerate fall under 5 fps

I don't know why here I use the invokerepeating method, I have also use an time interval in the update but the result is the same the framerate fall...

I am interested in a solution

Thanks in advance !

 using UnityEngine;
 using System.Collections;
 using System.IO.Ports;
 using System.IO;
 
  public class Com : MonoBehaviour {
 
  
     public bool outputToSerial=true;
 
     public byte m1_1=0;
     public byte m1_2=0;
     public byte m2_1=0;
     public byte m2_2=0;
 
     SerialPort sp = new SerialPort("COM6", 9600);
 
     void Start () {
 
          sp.Open();
         InvokeRepeating("RepeatingSend", 1, 0.2f);
 
     }
 
  
     void RepeatingSend () {
 
         SendToSerial((byte) m1_1,(byte) m1_2,(byte) m2_1,(byte) m2_2);
 
     }
 
  
    void SendToSerial(params byte[] cmd)
 
     {
 
         if (outputToSerial) 
 
         {
 
             if (sp.IsOpen){
 
                 try { 
 
                     sp.Write( cmd, 0, cmd.Length );
 
                 }
 
                 catch (System.Exception ex) 
 
                 {
 
                     Debug.LogWarning(ex);
 
                 }
 
             }
 
         }
 
     }
 
  
 }
Comment
Add comment · Show 1
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 Zeretyh · May 03, 2016 at 10:47 AM 0
Share

Hi,

Did you find an answer?

I am reading data via USB serial from my arduino, and it makes the FPS drop as well. In fact if the arduino sends data packages too fast, like under every 100ms, unity will be extremely slow.

I guess there might be a way to synchronise both based on the targeted framerate?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by canert · May 12, 2016 at 01:01 PM

I solve this problem with multi-threading structure. while unity works on the graphic and other things, you can create your own thread in the operating system. this helps you for using other cores of the processor and your fps probably will increase.

The unity works in Main thread

 //in awake or start
 MyThreadClass thread;
 thread.StartThread(); // your thread started here and the unity continue to process other things.
 ...
 


and serial communication processes are in the other thread class.

 private Thread thread;
 
 public bool StartThread()
 {
 // initializing things // port open. 
             if (thread == null)
             {
                 thread = new Thread(ThreadJob);
                 thread.IsBackground = true;
                 thread.Start();
             }
 }
 
 private void ThreadJob()
 {
      while(true){
 
         // write things to port
 
         // listen port
        
       }
 }

you can search concurrent programming for more things , I have also new in this field :D

I hope I could help you

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 canert · May 12, 2016 at 01:04 PM 0
Share

You can search semaphore,mutex,lock and monitor keywords for communication between unity and your thread

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

23 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

Related Questions

HELP: Arduino Genuino 101 to Unity3D. Serial Communication 0 Answers

Arduino Unity3d FPS drop after twisting Potentiometer 0 Answers

Arduino serial Read 0 Answers

Help with Unity communicating with Arduino (serial port communication) 0 Answers

Arduino Button Input not working 0 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