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 smurph9876 · May 31, 2013 at 03:18 PM · lagarduinoserialcom

Long lag in serial communication with Arduino

I'm trying to have unity read in some serial data from an arduino in the serial log. I'm sending potentiometer data from an arduino to unity, right now I just want to read it in the Debug log.

The code kinda works, the data is sending correctly. But there's a solid 10 second delay from when I move the potentiometer, to when the data shows up in the Unity log. The serial window in arduino works, and I wrote some similar code in visual basic, and it works fine in the VS console window too, so the problem is in unity. It doesn't seem to be missing readings either, it's just a very long delay before the data shows up.

My code is below, anyone have any ideas?

Also, to clarify, the FPS seems to be fine, the game is still perfectly responsive to the standard first person controller script. It's not slowing down the game, it just seems like the data is delayed by a few seconds.

 //Unity Code:
 
 using UnityEngine;
 using System.Collections;
 using System.Threading;
 using System.IO.Ports;
 
 public class serialcom : MonoBehaviour
 
 {
 public static SerialPort sp;
 public static string x;
 public static string[] data;
 public static string lin;
 public static string rot;
 public int debugcount = 1;
     
 // Use this for initialization
 void Start () 
 {
     //Debug.Log ("Code started");
     OpenConnection();
     //Debug.Log ("initialzed properly");
 }
 
 void Update() 
     {
         try{
         x = sp.ReadLine ();
         data = x.Split(' ');
         lin = data[0];
         rot = data[1];
         Debug.Log (lin + " " + rot);
         }
         catch{Debug.Log ("Please Work");}
     }
 
     
 public void OpenConnection() 
 {
     sp = new SerialPort("COM4", 9600, Parity.None, 8, StopBits.One);
     Debug.Log ("OpenConnection started");
     if (sp != null) 
     {
        if (sp.IsOpen) 
        {
          sp.Close();
          Debug.Log ("Closing port, because it was already open!");
        }
        else 
        {
          sp.Open();  // opens the connection
          sp.ReadTimeout = 100;  // sets the timeout value before reporting error
         Debug.Log("Port Opened!");
        }
     }
     else 
     {
        if (sp.IsOpen)
        {
          print("Port is already open");
        }
        else 
        {
          print("Port == null");
        }
     }
         Debug.Log ("Open Connection finished running");
     }
     
 void OnApplicationQuit()
     {
         if (sp != null)
             sp.Close();
     }
 }



 // Ardiuno code:
 
 
 int pot1 = A0;
 int pot2 = A1;
 int pot1volt; 
 int pot2volt;
 
 
 void setup() {
   // put your setup code here, to run once:
   Serial.begin(9600);
 }
 
 void loop() {
   // put your main code here, to run repeatedly: 
 
 pot1volt = analogRead(pot1);
 pot2volt = analogRead(pot2);
 
   Serial.print(pot1volt);
   Serial.print(" ");
   Serial.print(pot2volt);
   Serial.println();
   
   delay(1);
   
 }
Comment
Add comment · Show 2
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 Dave-Carlile · May 31, 2013 at 03:38 PM 0
Share

In your Update function, you're logging a message every frame if the ReadLine fails. I'm just guessing, but does that fail whenever there isn't data? If so, you're logging a huge number of messages, which causes Unity to lag quite a bit.

avatar image smurph9876 · May 31, 2013 at 03:48 PM 0
Share

I pulled the message out of the catch function, it's still delayed by a good amount though. The frame itself isn't actually delayed, I'm using the standard first person controller and the game works fine. I've tried the print function ins$$anonymous$$d of debug.log and it's quite delayed there as well.

4 Replies

· Add your reply
  • Sort: 
avatar image
1
Wiki

Answer by smurph9876 · May 31, 2013 at 08:16 PM

Well I figured it out actually, figure I'll leave it here in case any else needs a reference. I set up the times so that it can only get 1 reading per timeout cycle. So I upped the arduino delay to 20 ms, and the in unity i set up readtimeout = 25. That seemed to do the trick very nicely.

Arduino:

 int pot1 = A0;
 int pot2 = A1;
 int pot1volt; 
 int pot2volt;
 
 
 void setup() {
   // put your setup code here, to run once:
   Serial.begin(9600);
 }
 
 void loop() {
   // put your main code here, to run repeatedly: 
 
 pot1volt = analogRead(pot2);
 pot2volt = analogRead(pot1);
 
   Serial.print(pot1volt);
   Serial.print(" ");
   Serial.print(pot2volt);
   Serial.println();
   
   delay(20);
   
   
 }



Unity:

 using UnityEngine;
 using System;
 using System.Collections;
 using System.Threading;
 using System.IO.Ports;
 
 public class serialcom : MonoBehaviour
 
 {
 public static SerialPort sp;
 public Transform player;
 public static string x;
 public static string[] data;
 public static string lin;
 public static string rot;
 public int debugcount = 1;
 public float moveSpeed = 10f;
 public float turnSpeed = 50f;
 public int lincons;
 public int rotcons;
     
 // Use this for initialization
 void Start () 
 {
     //Debug.Log ("Code started");
     OpenConnection();
     //Debug.Log ("initialzed properly");
 }
 
 void Update() 
     {
         
         x = sp.ReadLine ();
         sp.ReadTimeout = 25;
         data = x.Split(' ');
         lin = data[0];
         rot = data[1];
         lincons = Convert.ToInt32(lin);
         lincons = lincons / 25;
         rotcons = Convert.ToInt32(rot);
         rotcons = -((rotcons / 50) - 10);
         player.Translate(Vector3.forward * lincons * moveSpeed * Time.deltaTime);
         player.Rotate(Vector3.up, rotcons * turnSpeed * Time.deltaTime);
     }
 
     
 public void OpenConnection() 
 {
     sp = new SerialPort("COM4", 9600, Parity.None, 8, StopBits.One);
     Debug.Log ("OpenConnection started");
     if (sp != null) 
     {
        if (sp.IsOpen) 
        {
          sp.Close();
          Debug.Log ("Closing port, because it was already open!");
        }
        else 
        {
          sp.Open();  // opens the connection
          // sets the timeout value before reporting error
         Debug.Log("Port Opened!");
        }
     }
     else 
     {
        if (sp.IsOpen)
        {
          print("Port is already open");
        }
        else 
        {
          print("Port == null");
        }
     }
         Debug.Log ("Open Connection finished running");
     }
     
 void OnApplicationQuit()
     {
         if (sp != null)
             sp.Close();
     }
 }
 
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
avatar image
0

Answer by Jashengmato · Nov 29, 2013 at 09:52 AM

The better way to do this is not reduce the delay time on Arduino, but use delegate callback in unity code like this.

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
avatar image
0

Answer by Zeratu · Mar 16, 2018 at 04:16 PM

Thank you! Very useful!

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
avatar image
0

Answer by el-beto-acosta · Sep 25, 2018 at 04:10 AM

Thank you so much. This work perfect!

Arduino:

 unsigned long t = 0;
 int interval = 100;

 void loop(){

    if (millis() - t > interval) {
         t = millis();

    Serial.print(angle[0]); //from MPU6050
    Serial.print("/");
    Serial.println(angle[1]);
    }
 }

Unity:

 void Start () {
     SP.Open ();
     SP.ReadTimeout = 101;

}

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.

Follow this Question

Answers Answers and Comments

17 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Arduino with Unity: Bad Framerate! 3 Answers

Trying to read an integer value from ReadLine() 1 Answer

Time.timescale cause extreme lag? 1 Answer

transform.rotation = ... reacts slow 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