- Home /
Question by
alexispolak · Sep 16, 2015 at 05:35 AM ·
javascriptcommunicationarduinoserialportreading
Arduino serial Read
Hi there ! I'm having a problem with some js code to read a stream from arduino. Here is the error:
Error: System.NullReferenceException: Object reference not set to an instance of an object
at (wrapper managed-to-native) System.IO.Ports.WinSerialStream:ClearCommError (int,uint&,System.IO.Ports.CommStat&)
at System.IO.Ports.WinSerialStream.get_BytesToRead () [0x00000] in <filename unknown>:0
at System.IO.Ports.SerialPort.get_BytesToRead () [0x00000] in <filename unknown>:0
at System.IO.Ports.SerialPort.ReadExisting () [0x00000] in <filename unknown>:0
at (wrapper remoting-invoke-with-check) System.IO.Ports.SerialPort:ReadExisting ()
at LectorSerial.LeerDatos () [0x00006] in Z:\home\apolak\Proyectos\Unity3D\Apuntador\Assets\Scenes\Prueba Encoder\LectorSerial.js:62
UnityEngine.Debug:Log(Object)
LectorSerial:LeerDatos() (at Assets/Scenes/Prueba Encoder/LectorSerial.js:66)
UnityEngine.EventSystems.EventSystem:Update()
I'm using this script:
#pragma strict
import System.IO.Ports;
public static var stream : SerialPort;
public var nombrePuerto = "/dev/ttyACM1";
public var abroPuerto = false;
public var cierro = false;
function Start ()
{
CrearSerialPort();
}
function Update ()
{
AbrirConexion();
}
function CrearSerialPort()
{
stream = new SerialPort ();
stream.BaudRate = 115200;
stream.PortName = nombrePuerto;
stream.Parity = Parity.None;
stream.DataBits = 8;
stream.StopBits = StopBits.One;
}
function AbrirConexion()
{
if (abroPuerto && !stream.IsOpen)
{
Debug.Log("Antes del Try");
try
{
stream.Open (); //Abro el stream del puerto serial.
stream.ReadTimeout = 1000;
Debug.Log("Esta abierto: "+stream.IsOpen);
}
catch (ex)
{
Debug.Log("Error: "+ex.ToString());
}
}
if (cierro)
{
Debug.Log("Entro a Cierro");
stream.Close ();
abroPuerto = false;
}
}
function LeerDatos()
{
try
{
var receivedData = "";
receivedData = stream.ReadExisting().ToString();
}
catch (ex)
{
Debug.Log("Error: "+ex.ToString());
}
}
function OnApplicationQuit()
{
stream.Close();
}
Comment
Your answer
Follow this Question
Related Questions
Unity to Arduino (write serial) Framerate fall 2 Answers
HELP: Arduino Genuino 101 to Unity3D. Serial Communication 0 Answers
Read objects like in Penumbra 1 Answer
parse serial data from arduino 1 Answer
Transform Unity Object with Arduino 2 Answers