Until now the best solution that i found is to use a external dll files, there is a comercial library called serial port component, here is the link:
This is a commercial library but you can test the trial version for 30 days. You can integrate the dll component with c# and other ones. When you install the component you can see examples in your pc.
For using into Unity you must copy the dll file in any folder of your project an the unity folder “C:\Program Files\Unity\Editor”.(I’m using unity 5 free, the last version), with this method I got to read data from the serial port.
I don’t read data from “Update method” for performance reasons. Use a asynchronous coroutine , I installed a free package called “thread ninja” for
simulate an update method but this is executing each second not each frame .This improves the performance.
link: Unity Asset Store - The Best Assets for Game Making
when you install the package, create a class in c# as for example this one (This algorithm requires improvement):
using UnityEngine;
using System.Collections;
using AxSerial;//Import the external class
using System;
using System.IO;
using System.Threading;
using CielaSpike;//Import the custom thread for unity
public SerialController : MonoBehaviour {
ComPort objComport;//Serial port object
//string of data received for the serial port
string datareceived="";
/*
*method that initializes the serial port
*/
void initComponents()
{
try
{
objComport = new ComPort();
objComport.Device="COM7";//this specifies own port
objComport.BaudRate = 9600;
objComport.Open();//Open the port
}
catch(Exception ex)
{
Debug.Log(ex.GetBaseException());
}
}
void Start()
{
initComponents();
if (objComport.IsOpened)
{
StartCoroutine("manage_data");//Start coroutine for manage the data
Debug.Log("Port opened");
}
else
{
Debug.Log("Could not to open the port");
}
}
void OnDestroy
{
try
{
objComport.Close();
}
catch(Exception ex)
{
Debug.Log(ex.GetBaseException());
}
}
/*
* asynchronous coroutine,
* the idea is that the method of getting data is executing every so often, for example each one second.,
* not for each frame like the Update method because this would down the performance
*
*/
IEnumerator manage_data()
{
this.StartCoroutineAsync(Blocking(), out task);
yield return StartCoroutine(task.Wait());
}
IEnumerator Blocking()
{
int sleep = int.MaxValue;
//test cycle, I tried with the while cicle but it crashes Unity,
// when this "for" cicle is executing , it reads data and prints them almost in sync when the serial device sends data
//If you have a best idea, please share !!!
for (int i = 0; i <= int.MaxValue; i++)
{
read_data();
//wait 0.5 seconds and read again
yield return new WaitForSeconds(0.5F);
}
Thread.Sleep(sleep);
}
/*
*Read the data from the serial port
*/
void read_data()
{
if (objComport.LastError == 0)
{
datareceived=objComport.ReadString();
//Print data on Console
print( "Data Received: " +datareceived)
}
else
{
Debug.Log("No data");
}
}
}
This code is not perfect and I need to understand how this dll component works and build my own dll solution, I’ve been investigating and most likely is that it uses c++, c# and win32. I’m still seeking how to do it.
If you have some best idea or suggestion, please share. My idea is build a free dll component for manage serial ports inside Unity.
Sorry for my poor English.
more information: http://forum.unity3d.com/threads/alternative-for-reading-serial-ports-into-unity.336326/#post-2176862