- Home /
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?
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)
{
}
}
}
Your answer
Follow this Question
Related Questions
Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers
Unity crashes when connecting to a serial port 0 Answers
Vive Controller not enabled until mouse click 0 Answers
VR "Hold" Screen for gallery Instal 0 Answers
How to detect button down/up in xr interaction toolkit in unity xr oculus? 0 Answers