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.

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

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.