• 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 dakshesh1010 · Jun 17, 2019 at 11:44 AM · inputvrsimulationserialport

Reading from Serial port with Vive (SteamVR) resulting in huge lag.

I am developing a Forklift simulation in HTC Vive using the latest SteamVR plugin (V 2.3.0). I want to add a custom fabricated steering wheel to the sim, which gives values based on it's rotation. I already had implemented the simulation controls using Vive controllers and keyboard. But the moment since I added the serial connection reading code, the simulation starts lagging bad. 15-20 fps is what I get just by reading from serial port. Here is the code -

 void Awake ()
     {
         InitializeCOMport();
         StartCoroutine(ReadData());
     }
 
 void InitializeCOMport()
     {
          port = new SerialPort(portName, 9600, Parity.None, 8, StopBits.One);
 
         try
         {
             port.Open();
             System.Threading.Thread.Sleep(1000);
             port.Handshake = Handshake.None;
             Debug.Log("Port open");
         }
         catch (Exception ex)
         {
             Debug.Log("Exception " + ex.Message);
         }
 }
 IEnumerator ReadData()
     {
         yield return null;
         do
         {
             yield return new WaitForSeconds(0.15f);
             try
             {
                 datareceived = port.ReadLine().Trim();
                 char[] charsToTrim = { 'd', 'a', 't', ' ', '=' };
                 datareceived = datareceived.Trim(charsToTrim);
                 //Debug.Log("Steering port: " + datareceived);
                 currSteeringValue = int.Parse(datareceived);
                 
             }
             catch(System.Exception ex)
             {
                 Debug.Log("Exception " + ex.Message);
             }
         } while (true);
     }

I had to read the serial port data this way because when I try doing it using the SerialPort.DataReceived event, I get a time-out exception. With the above Coroutine ReadData() I am able to get the COMM port data, but not using the DataRecieved event. Here is the code that reads the buffer using the event-

 port.DataReceived += new SerialDataReceivedEventHandler(Port_DataReceived);
 
  private void Port_DataReceived(object sender, SerialDataReceivedEventArgs e)
         {
             Debug.Log("received");   //This function is fired with
             try
             {
                 datareceived = port.ReadLine().Trim();
                 char[] charsToTrim = { 'd', 'a', 't', ' ', '=' };
                 datareceived = datareceived.Trim(charsToTrim);
                 Debug.Log("Steering port: " + datareceived);
                 currSteeringValue = int.Parse(datareceived);
     
             }
             catch (System.Exception ex)
             {
                 Debug.Log("Exception " + ex.Message);
             }
             Debug.Log("received");
         }

The above Port_DataReceived() results in Time-out exception and the control never reaches to Debug.Log("Steering port: " + datareceived); this point, as it doesn't get logged.

Is this lag because I am using serial ports with vive? Does anyone have any idea how I can solve this?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by yesica360 · Apr 28, 2020 at 07:09 PM

Hi! I'm a beginner, and im develop a project with HTC Vive and Arduino Im sending a sensor value from Arduino to Unity and i had the same problem like you, the image in the headset is lagging. I know that your project is not the same, but maybe can help you my solution I was a old project with Oculus DK2 and Arduino and i used use serialPort.ReadTimeout = 100; With HTC Vive I was abble to resolve the lagg image only changing a value in Unity code serialPort.ReadTimeout = 1;

This is my code for a test

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System.IO.Ports;
 
 public class test : MonoBehaviour {
 
     SerialPort serialPort = new SerialPort("COM8", 9600);
 
     void Start()
     {
         serialPort.Open();
         serialPort.ReadTimeout = 1;
     }
 
     void Update()
     {
         try
         {
             print(serialPort.ReadLine());
         }
         catch (System.Exception)
         {
         }
     }
 }
Comment
Add comment · 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

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

157 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 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

Vive Controller not enabled until mouse click 0 Answers

VR "Hold" Screen for gallery Instal 0 Answers

Handling Large number of Active Rigidbody Evaluations 2 Answers

Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers

VR Navigation ala TiltBrush or Quill 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