Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by alexispolak · Sep 14, 2015 at 01:04 PM · javascriptarduinosystem.iostreamserialport

Read Arduino Stream with Js or C#

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();
  }


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

30 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity / Arduino wifi communication for multiwii serial protocol 0 Answers

HELP: Arduino Genuino 101 to Unity3D. Serial Communication 0 Answers

I need to connect arduino and android device via USB and send data via serial port with Unity. 4 Answers

Add A Start Off State for Switch Case Scenario 2 Answers

Convert an Int to a String 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges