• 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 vrrasik · Sep 13, 2013 at 11:34 PM · lagarduino

Lag issues with unity3d + Arduino (Xbee)

Hardware setup : Trigger -> Arduino -> Xbee ------------- Xbee -> PC (Serial port) -> Unity3d

The Xbee's are running in API mode, which means the data is encapsulated into a packet containing a start byte, checksum, the data, source address etc. This packet is set wirelessly to the other Xbee, which forwards it to the serial port.

Now I'm trying to get these packets into unity, retrieve the data and display it on the screen. Unfortunately, there is a HUGE lag between the time I change the trigger value, and the time it reflects on the screen.

I've observed the packet stream in a serial terminal (X-CTU) and the change reflects there immediately. Why is there such a lag within unity and how do I correct it? Someone help!

Unity Code

 using UnityEngine;
 using System;
 using System.Collections;
 using System.IO.Ports;
 
 public class readSerial : MonoBehaviour {
 
     SerialPort stream = new SerialPort("\\\\.\\COM15"); //Set the port (com15)
     public GUIText txt;
     public GameObject target;
     
     const byte START_BYTE = 0x7E;
     const int offset = 2;
     byte read_bytes = 0;
     byte MSB =0;
     byte LSB =0;
     byte[] comb = {0,0};
     int counter = 0;
     bool startCtr;
     Int16 result;
     
     void Start () {
 
         //settings for the serial port copied from X-CTU
         stream.BaudRate = 9600;
         stream.Parity = Parity.None;
         stream.DataBits = 8;
         stream.StopBits = StopBits.Two;
         stream.ReadTimeout = 3;
         
         stream.Open();
         StartCoroutine(ReadData());
 
     }
       
       // Update is called once per frame
       void Update () {
         
         txt.text = result.ToString();
         
         if(read_bytes==161) //personal data identifier...A1
                 startCtr = true;
             
             if(startCtr)
                 counter++;
             
             if(counter == offset)
             {
                 LSB = read_bytes;
             }
             if(counter == offset+1)
             {
                 MSB = read_bytes;
                 
                 comb[0] = LSB;
                 comb[1] = MSB;
                 
                 result = BitConverter.ToInt16(comb, 0);
                 //txt.text = result.ToString();
                 
                 counter = 0;
                 startCtr =false;
             }
     
     }    
     
     
     IEnumerator ReadData()
     {        
         while(true)
         {                
             read_bytes = (byte) stream.ReadByte();
             yield return new WaitForEndOfFrame();
         }                
     }
 
 }



I tried putting the read bit in a separate thread, but that didn't help.

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 Benproductions1 · Sep 14, 2013 at 12:21 PM 0
Share

[CODE] and [/CODE] do nothing on this website. Watch the tutorial video on the right to see how code is formatted and plz fix up the question :)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

How to read two separate values from the same SerialPort? 1 Answer

Arduino with Unity: Bad Framerate! 3 Answers

Reducing Lag in Game? 6 Answers

Long lag in serial communication with Arduino 4 Answers

My game overheats phone and terrible FPS? 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