Quantcast
Channel: Questions in topic: "javascipt"
Viewing all articles
Browse latest Browse all 1875

Read Arduino Stream with Js or C#

$
0
0
Hi there ! I'm working on a project where i need to read a stream from arduino. I tried use C# but i'm having problems with the Read() function freezing the game, the ReadExisting doesn't recieve anything and the event SerialDataReceivedEventHandler is never triggered. I tried to do the same thing in js but i can't get it work, it's say that "stream.on" its not a member of System.IO.Ports. Here is the code in C# and the code in js. **C# Code**: using UnityEngine; using System.Collections; using System.IO.Ports; using System; public class LectorEncoder : MonoBehaviour { public string puerto = "/dev/ttyACM1"; SerialPort stream; //Establezco un stream con el puerto (com4) y el baud (11500) //float[] lastRot = {0,0,0}; //Need the last rotation to tell how far to spin the camera public bool abroPuerto = false; public bool cierro = false; void Start () { stream = new SerialPort (puerto, 115200); } // Update is called once per frame void Update () { if (abroPuerto && !stream.IsOpen) { try { stream.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler); stream.Open (); //Abro el stream del puerto serial. stream.ReadTimeout = 1000; Debug.Log("Esta abierto: "+stream.IsOpen); } catch (Exception e) { Debug.Log("ups: "+e.ToString()); } } if (cierro) { stream.Close (); abroPuerto = false; } //this.algo (); } public void algo() { if (stream.IsOpen) { try { //Debug.Log ("Antes de leer"); string value = stream.ReadExisting (); //Leo la informacion del Stream Debug.Log ("Lectura: " + value); } catch (Exception e) { Debug.Log("ups: "+e.ToString()); } } else { Debug.Log ("El puerto no está abierto"); } } **JS Code:** #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 = ""; Debug.Log("Entro a LeerDatos()"); } catch (ex) { Debug.Log("Error: "+ex.ToString()); } stream.on('data', function(data) { Debug.Log("Entro a Stream.On()"); receivedData += data.ToString(); Debug.Log("Termino de leer: "+receivedData); }); } function OnApplicationQuit() { stream.Close(); }

Viewing all articles
Browse latest Browse all 1875

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>