I have successfully managed to connect my bluetooth device to my pc via bluetooth serial port connection.
Reading data and writing also works fine, problem is that device is sending data with 900k bps speed.
So far this device i am using is just a test device and it is sending data like this:
abcdefghijklmnopqrstuvwxyz0
abcdefghijklmnopqrstuvwxyz1
abcdefghijklmnopqrstuvwxyz2
abcdefghijklmnopqrstuvwxyz3
abcdefghijklmnopqrstuvwxyz4
I made function that checkes if last digit is increased by one in regards to previous one.
When i put reading function in Update and checking function in new thread there is about 15-20% packet loss… Then what i did was put everything into fixedUpdate and increased speed of it in “edit-time”, and i managed to lower it but it just seemed like a bad way to do this.
After hours of searching i found a script where guy makes loop run really fast.
I used that script and managed to get my packet loss to about 5%… Problem is that i have to get it to 0%.
public void Connect()
{
sp = new SerialPort(@"\\.\" + port, 9600);
Debug.Log ("Connection started");
try
{
sp.Open(); // opens the connection
connected = true;
}
catch(System.Exception e)
{
Debug.Log(e.ToString());
connected = false;
}
}
private void SuperFastLoop()
{
// We can't use Time.time which is a Unity API, instead we'll use this
var time = System.DateTime.UtcNow.Ticks;
const int oneSecond = 1;
var count = 0;
// This begins our Update loop
while (true)
{
if (System.DateTime.UtcNow.Ticks - time >= oneSecond)
{
ThreadedUpdatedsPerSecond = count;
count = 0;
time = System.DateTime.UtcNow.Ticks;
}
data = sp.ReadLine();
digit = Regex.Replace(data, "[^0-9]", "");
if(digit.Length==1)
lastDigits.Add(digit);
if(lastDigits.Count>500)
{
numBuffer++;
lostPackets = 0;
[B]//this is for testing how many packets we lost[/B]
for(int k=0;k<lastDigits.Count;k++)
checkArray.Add(lastDigits[k]);
lastDigits.Clear();
int temp = int.Parse(checkArray[0]);
for(int i=0;i<checkArray.Count;i++)
{
if(temp != int.Parse(checkArray[i]))
{
try
{
temp=int.Parse(checkArray[i+1]);
lostPackets++;
}
catch(System.Exception e)
{}
}
else
temp++;
if(temp == 10)
temp = 0;
}
//Debug.Log("----- lost packets: "+ ((lostPackets*100.0f)/checkArray.Count)+"%");
pLost += (lostPackets*100.0f)/checkArray.Count;
//Debug.Log("Avarage lost percentage "+pLost/numBuffer+"%");
checkArray.Clear();
lostPackets = 0;
//thread = new Thread(checkPackets);
//thread.Start();
}
count++;
}
}
public void startStream()
{
RestartValues();
sendCommand("start");
_updateThread = new Thread(SuperFastLoop);
_updateThread.Start();
}
So now i am wondering how can i make this run even faster…
Any help would be much appreciated.