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 /
avatar image
0
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
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

2 People are following this question.

avatar image avatar image

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


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