Hi everyone,
I am creating a game with multiple IMUs sensors. My sensors have BLE module so that transmitting data on bluetooth for my receiver unit. My receiver units have serial port so that I read the data on serial port. However, I am facing a problem for receiving data on Unity platform. When I try to get data on arduino terminal app, ı get proper data. However, when ı try to get data on unity, ı can get data. Can you help me I ?
I got data with this code previous. However, it does not work right now. I do not understand what is going wrong.
My sample code is under this.
using UnityEngine;
using System.Collections;
using System;
using System.IO.Ports;
using System.Threading;
using Microsoft;
public class BLE1 : MonoBehaviour
{
public static BLE1 instance;
SerialPort stream = new SerialPort(“/dev/cu.usbserial-KURight”, 115200);
string hostid=“001EC03092EC”; //Ku-right
//stringhostid=“001EC03D1251”;//KU-Leh-H001EC03D1251
//stringhostid=“001EC03D125D”;//KU-Right001EC03D1251
float[ ] accel, gyro, magnet;
Thread serialThread;
System.Diagnostics.Stopwatch watch;
//rotationmatrixfromgyro data
private float[ ] gyroMatrix = new float[9];
//orientationanglesfromgyro matrix
private float[ ] gyroOrientation = new float[3];
//orientationanglesfromacceland magnet
private float[ ] accMagOrientation = new float[3];
//finalorientationanglesfromsensor fusion
private float[ ] fusedOrientation = new float[3];
//accelerometerandmagnetometerbasedrotation matrix
private float[ ] rotationMatrix = new float[9];
private float[ ] outGyro = new float[3];
private float EPSILON = 0.000000001f;
private float NS2S = 1.0f / 1000000000.0f;
private float timestamp;
private bool initState = true;
[Range(0, 1)]
public float FILTER_COEFFICIENT = 0.98f;
float initTimer = 0;
void Awake()
{
instance = this;
gyro = new float[3];
accel = new float[3];
magnet = new float[3];
gyroOrientation[0] = 0.0f;
gyroOrientation[1] = 0.0f;
gyroOrientation[2] = 0.0f;
//initialisegyroMatrixwithidentity matrix
gyroMatrix[0] = 1.0f; gyroMatrix[1] = 0.0f; gyroMatrix[2] = 0.0f;
gyroMatrix[3] = 0.0f; gyroMatrix[4] = 1.0f; gyroMatrix[5] = 0.0f;
gyroMatrix[6] = 0.0f; gyroMatrix[7] = 0.0f; gyroMatrix[8] = 1.0f;
}
void Start()
{
watch = new System.Diagnostics.Stopwatch();
Connect();
}
void Connect()
{
Debug.Log(“Connectionstarted”);
try
{
stream.Open();
stream.ReadTimeout = 400;
stream.Handshake = Handshake.None;
StartCoroutine(“connectBLE1”);
}
catch (SystemException e)
{
Debug.Log(“Erroropening=” + e.Message);
}
}
IEnumerator connectBLE1()
{
stream.WriteLine(“SR,92000800”);
stream.BaseStream.Flush();
yield return new WaitForSeconds(0.5f);
stream.WriteLine(“R,1”);
stream.BaseStream.Flush();
yield return new WaitForSeconds(2f);
stream.WriteLine(“E,0,” + hostid);
stream.BaseStream.Flush();
yield return null;
serialThread = new Thread(recData);
serialThread.Start();
Debug.Log(“PortOpened!”);
//InvokeRepeating(“calculateFusedOrientationTask”,1,0.03f);
}
void recData()
{
watch.Start();
Debug.Log(“recData”);
if ((stream != null) && (stream.IsOpen))
{
int tmp;
string data = “”;
string avalues = “”;
tmp = 0;
int tabCounter = 0;
while (tmp != 255)
{
tmp = stream.ReadByte();
data += (char)tmp;
if (tmp == ‘\t’)
{
tabCounter++;
}
if (tmp == 13) //ASCIIofcarriagereturn,endofonedata block
{
//print(data);
if (tabCounter == 9) //Ablockcontains9dataseperatedwith tabs
{
avalues = data;
parseValues(avalues);
}
data = “”;
tabCounter = 0;
}
}
}
}