Hi All,
This is my first post and i have 1 week of Unity +C# under my belt so excuse me if im a little DUMB on this matter.
I have some basic script which will rotate a cube in Unity based on accellerometer values passed to it via an Android phone Bluetooth virtual serial port connection. The data passed to Unity is around 160 bytes per second (x,y,z float values). The serial port is set at 38,400,8,1,N (i have tried different settings). I have tried various receive time out’s from 1ms to 100ms, same problem occurs.The problem i am getting is all works fine for around 5 minutes THEN the data received by unity will start to slow down. What i mean by that is the data i pull from the serial port will be active for 5 seconds, then stop for 2 seconds then start for another 5 seconds. The missing data is in the buffer and read correctly by unity when it restarts. This time ratio is ALWAYS the same.
I have tried several methods of reading the buffer including ReadLine, ReadByte, ReadChar, ReadTo, all these functions exhibit the same behavior. These functions are called from a coroutine, i have also tried a separate a thread but with the same results. I have played with different baud and time out settings but with the same results. I also tried the serial data received event but that did not work at all, apparently there is some problem with this event in mono? I am beginning to think there is a problem with the SerialPort class in unity?
I have tried looking at the sent data from the Android with a terminal program and all is good so no problems there. I have set up a byte counter at each end of the comms and all bytes are received by unity providing it is in focus, the problem being this 2 second gap when unity does not receive (but the serial buffer is filling). I have tried this on 2 different machines with different O/S and the results are the same.
HELP cos it’s driving me nuts! (3 days with NO progress) can i use a 3rd party serial class? any suggestions would be REAL nice, but please remember im a newbie
OK so after a LOT of digging around on the internet and reading lots of problems from other coders i have come to the conclusion that the problem lies in the serialport methods, see this link for a very in depth opinion from a “coms” expert.
He has suggested a reliable method for reading serial comms in an async fashion but because of my lack of experience with C# i cannot understand how i would implement this code in my main script. Could someone out there help me, please, please? I will post my findings here after some extensive testing and hopefully this will be useful to other serial comms users.
OK good point so here is the main script it’s empty as you can see. What i want to know is how i implement this code and read the serial data into a string. I can process and add this to my rotating cube later. I am trying to get to grips with C#, i have 1 week under my belt so the learning curve is BIG. my specialty is electronics design and writing code in assembler for micro controllers :-/
using UnityEngine;
using System.Collections;
using System.IO.Ports
public class ReadSerial : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}