Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 dil33pm · Aug 05, 2015 at 09:00 AM · unity 5accelerometerbluetoothserialport

Reading Serial data in Unity from Android app

I am using Android's SensoDuino (Official website) app to send sensory data like Accelerometer, gyro etc. using Bluetooth serial communication to Unity in PC. I am using the following code in Unity to receive data -

 using UnityEngine;
 using System.Collections;
 using System.IO.Ports;
 using System;

 public class COM : MonoBehaviour {
 
 public SerialPort sp;
 public float data;

 void Start () {
     sp = new SerialPort("COM3", 9600,Parity.None, 8, StopBits.One);
     Debug.Log ("Connection 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
             sp.DataReceived += new   SerialDataReceivedEventHandler(DataReceivedHandler);
              Debug.Log("Port Opened!");
          }
      }
      else 
     {
         if (sp.IsOpen)
          {
              print("Port is already open");
          }
          else 
         {
              print("Port == null");
          }
     }

      Debug.Log ("Open Connection finished running");
  }

  void Update () { 

  }

 private void DataReceivedHandler(object sender,   SerialDataReceivedEventArgs e)
 {
     SerialPort spl = (SerialPort)sender;
     data = float.Parse( spl.ReadTo ("\r") ) ;
     Debug.Log(data.ToString());
      print ("data = " + data);
 }
 }


But I am not able to receive any data. The DataReceiveHandler is not firing at all. I tried to use ReadLine() also, but it freezes Unity.

The format by which SensoDuino sends Accelerometer data is

>1,104,0.54437256,-0.2632141,9.826126
                                     >1,105,0.56111145,-0.279953,9.8524475
                                                                          >1,106,0.54556274,-0.2632141,9.833298
                               >1,107,0.5515442,-0.26081848,9.841675
                                                                    >1,108,0.5312042,-0.2644043,9.867996

This is a continuous stream and might be the cause of the freeze while using ReadLine().

Please help me to read the data.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by dil33pm · Aug 31, 2015 at 12:25 PM

I have found the solution. ReadLine() is a blocking call, so I had to use ReadByte() to continuously read bytes and convert the data later. To reduce lag, the ReadByte() function was called in a thread.

  void recData() {
     if ((sp != null) && (sp.IsOpen)) {
         byte tmp;
         string data = "";
         string avalues="";
         tmp = (byte) sp.ReadByte();
         while(tmp !=255) {
             data+=((char)tmp);
             tmp = (byte) sp.ReadByte();
             if((tmp=='>') && (data.Length > 30)){
                 avalues = data;
                 parseValues(avalues);
                 data="";
             }
         }
     }
 }

I have made a pretty detailed tutorial here

Comment
Add comment · Show 2 · 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 Mikilo · Aug 31, 2015 at 12:29 PM 1
Share

It a pretty ugly way and non-optimized one.

You should use an async read and read data only when available. Even if you have no memory constraint or CPU constraint.

avatar image dil33pm · Nov 23, 2015 at 02:42 PM 0
Share

Yes, am using a thread to read the inco$$anonymous$$g data.

avatar image
0

Answer by unity_ail4Q-HKR3kdxg · Dec 04, 2017 at 02:58 PM

Hey. I found in your tutorial movement of your pong paddle in y-axis. I've receiving a serial data of my gyroscope through bluetooth. but its not moving. Please help us.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Unity accelerometer and screen/device orientation issue 0 Answers

bluetoot keyboard stopped working when i open unity 0 Answers

Is it possible to get the forward and backward movement of android device 1 Answer

Serial port IOException: access is denied 0 Answers

Move ball object based on gravity 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