I'm using an arduino board to communicate serial data into Unity. I had this working using the read analog voltage sample that comes with the board and the output from that happily displayed in the debug log.
However, now when I run Unity i get the following error:
IOException: The port `COM11' does not exist.
I've changed my COM port to be a variety of numbers but they all come back with the same error.
My serial port reading code is thus:
SerialPort stream = null;
string data = "Ready";
private float DataTimer = 2.0f;
private float TimeToCheckStream = 0.1f; // check data every second
public string COMPort = "";
public int baudRate = 9600;
void Awake ()
{
stream = new SerialPort(COMPort,baudRate); //originally 9600
Debug.Log ("Initialized stream");
LogWriter writer = LogWriter.Instance;
writer.WriteToLog( COMPort);
}
void Start ()
{
// LogWriter writer = LogWriter.Instance;
// writer.WriteToLog("Testing test");
if ( stream != null )
{
if ( stream.IsOpen ) // close if already open
{
stream.Close();
Debug.Log ("Closed stream");
}
stream.Open();
Debug.Log ("Opened stream");
}
else
{
Debug.Log ("ERROR: Uninitialized stream");
}
}
void Update ()
{
if(DataTimer < TimeToCheckStream)
{
DataTimer += Time.deltaTime;
}
else
{
DataTimer = 0.0f;
if ( stream != null )
{
if ( stream.IsOpen )
{
// if stream is open do things in here
stream.ReadLine();
Debug.Log(stream.ReadLine().ToString());
}
}
else
{
Debug.Log ("NULL stream");
}
}
}
void OnGUI ()
{
GUI.Label ( new Rect(500,10,300,100), data );
}
void OnApplicationQuit ()
{
if ( stream != null )
{
stream.Close();
}
}
Is there any reason as to why my COM port would suddenly decide to close itself?