• 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 Udjasen · Dec 26, 2013 at 05:50 PM · javascriptserialport

Is there a way to Handle SerialPort Connections through JavaScript?

I've seen Many Solutions But all of them Use C#, and I want to use JavaScript. I tried:

 import System.IO.Ports;
 public static var sp : SerialPort ;
 sp.BaudRate = 9600;
 sp.PortName ="COM4";
 sp.Parity = Parity.None;
 sp.DataBits = 8;
 sp.StopBits = StopBits.One;

but I keep getting this error: -NullReferenceException: Object reference not set to an instance of an object Unityinojava.OpenConection () (at Assets/Unityinojava.js:41) Unityinojava.Start () (at Assets/Unityinojava.js:10)

Full code Bellow:

 #pragma strict
 import System.IO.Ports;
 public static var sp : SerialPort ;
 sp.BaudRate = 9600;
 sp.PortName ="COM4";
 sp.Parity = Parity.None;
 sp.DataBits = 8;
 sp.StopBits = StopBits.One;
 var count:int = 0;
 var message: String;
 var message1: String;
 var message2 : int;
 
 function Start () {
 OpenConection();
 }
 function OnGUI()
 {
     GUI.Label(new Rect(230,60,500,20),""+ message1+"Recebido"+ message2);
     GUI.Label(new Rect(120,10,500,30),"Status:"+message);
     if (GUI.Button(new Rect(10,60,200,30), "Liga LED"))
     {
         if (sp.IsOpen)
         {
             message1 = "Sent1";
             sp.Write("1");
             message2 = sp.ReadByte();
         }
     }
     if (GUI.Button(Rect(10,150,200,30),"Desliga LED"))
     {
         if (sp.IsOpen)
         {
             message1= "Sent2";
             sp.Write("2");
             message2= sp.ReadByte();
         }
     }
 }
 
 function Update () 
 {
 }
 function OpenConection()
 {
     
     if(sp!=null)
     {
         if (sp.IsOpen)
         {
             sp.Close();
             message="Fechando porta por que ja estava aberta";
         }else
         {
             sp.Open();//Abrindo conexao
             sp.ReadTimeout = 100;//Timeout Antes de reportar erro
             message = "Porta Aberta";
         }
         
     }else
     {
         if(sp.IsOpen)
         {
             print("Porta ja esta aberta");
         }else
         {
          print("Porta esta Nula");
         }
     }
 }
 function OnApplicationQuit()
 {
     sp.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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by ArkaneX · Dec 26, 2013 at 08:59 PM

There is no place in your code where you initialize your sp variable. And that's why you get NullReferenceException on second sp.IsOpen line. You have to initialize sp variable:

 sp = new SerialPort();

and copy this block:

 sp.BaudRate = 9600;
 sp.PortName ="COM4";
 sp.Parity = Parity.None;
 sp.DataBits = 8;
 sp.StopBits = StopBits.One;

after initialization.

Comment
Add comment · Share
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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

Arduino serial Read 0 Answers

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

What is Commercial Game? 1 Answer

Quick Bullet Instantiation Problem 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