Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by patricioaljndro · May 03, 2015 at 08:36 PM · unity 4.6

why don't serial ports work properly in unity?

need help, I'm desperate

During two weeks I have been working in my project, this uses serial port communication (a PIC serial board), I got to set the connection but i can not get data from the com port . I've read some forums and the cause of the problem seems to be a incomplete implementation of "System.IO.Ports" class.

When i try to get data of the COM port, the event "SerialDataReceivedEventHandler" (Represents the method that will handle the DataReceived event of a SerialPort object.) is not called or activated. I tried to resolve it but I don't find a definitive solution. I thought to prove a external DLL but a friend told me that the problem will go on also someone recommended to me use a secondary thread although I don´t understand how to do it at all.

I wrote a program in visual c# and everything works fine. I'm intrigued.

I need to find a solution , some idea or good documentation.

If there's someone knows something about it,please, help me .

I need to understand the cause of this to continue.

Thanks for read.

Comment
Add comment · Show 3
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
avatar image CarpeDiem_17 · Jun 22, 2015 at 03:39 PM 1
Share

I am desperate as well. Need to integrate ports comms in my app with a bluetooth sensor. Everything works like a charm in a cs Windows Form project.

In Unity, same problem as you. I connect to my sensor, I can set the strea$$anonymous$$g of my sensor ON.

BUT No data is inco$$anonymous$$g. The event handler is never triggered. And the "Read" instruction does not see any data anyway.

If someone has a idea, I need it ! Thanks.

avatar image andy78flavia28 · Jun 25, 2015 at 01:05 PM 0
Share

I have the same problem... I need to send (every 1 second) a command on the serial port (the command is port.writeline("D\r")) but the method ReadExisting() doesn't work... have you solved?

avatar image andy78flavia28 · Jun 26, 2015 at 11:07 AM 0
Share

The solution seems good, but If you need to receive data after a syntax/command (for example a command sent with the function port.writeline(....), how you can integrate it?

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Dave-Hampson · May 04, 2015 at 08:00 AM

If SerialDataReceivedEventHandler isn't working, is there a way to do what you need to do by just polling instead of using events?

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

Answer by CarpeDiem_17 · Jun 25, 2015 at 09:21 PM

Well I tried to read what was supposed to be on the com port.. But it is just blocking / not reading anything whereas it works in any non-unity c# app.

My backup solution is to call a custom windows service that reads from my sensor and populates my data on a UDP socket. Then inside my unity app I connect to this socket. It works but it is not a clean solution. Things should be so straightforward and are made complicated due to this implementation of COM ports in unity.

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

Answer by patricioaljndro · Jun 26, 2015 at 05:16 AM

Until now the best solution that i found is to use a external dll files, there is a comercial library called serial port component, here is the link: http://www.activexperts.com/serial-port-component/

This is a commercial library but you can test the trial version for 30 days. You can integrate the dll component with c# and other ones. When you install the component you can see examples in your pc.

For using into Unity you must copy the dll file in any folder of your project an the unity folder "C:\Program Files\Unity\Editor".(I'm using unity 5 free, the last version), with this method I got to read data from the serial port.

I don't read data from "Update method" for performance reasons. Use a asynchronous coroutine , I installed a free package called "thread ninja" for simulate an update method but this is executing each second not each frame .This improves the performance.

link: https://www.assetstore.unity3d.com/en/#!/content/15717

when you install the package, create a class in c# as for example this one (This algorithm requires improvement):

    using UnityEngine;
     using System.Collections;
     using AxSerial;//Import the external class
     using System;
     using System.IO;
     using System.Threading;
     using CielaSpike;//Import the custom thread for unity
     
     
 public SerialController : MonoBehaviour {
     
              ComPort objComport;//Serial port object
             
              //string of data received for the serial port
              string datareceived="";
             
             /*
             *method that initializes the serial port
             */
              void initComponents()
              {
                     try
                     {
                         objComport = new ComPort();
                         objComport.Device="COM7";//this specifies own port
                         objComport.BaudRate = 9600;
                         objComport.Open();//Open the port
                     }
                     catch(Exception ex)
                     {
                         Debug.Log(ex.GetBaseException());
                     }
             }
             
             void Start()
             {
                     initComponents();
                     if (objComport.IsOpened)
                     {
                         StartCoroutine("manage_data");//Start coroutine for manage the data
                         Debug.Log("Port opened");
                     }
                     else
                     {
                         Debug.Log("Could not  to open the port");
             
                     } 
             }
             
             void OnDestroy
             {
                try
                {
                         objComport.Close();    
                }
                catch(Exception ex)
                {
                      Debug.Log(ex.GetBaseException());
                }
             
             }
             /*
             *  asynchronous coroutine,
             *  the idea is that the method of getting data is executing every so often, for example each one second., 
             *  not for each frame like the Update method because this would down the performance
             *
             */
              IEnumerator manage_data()
             {
                     this.StartCoroutineAsync(Blocking(), out task);
                     yield return StartCoroutine(task.Wait());
             }
 
             IEnumerator Blocking()
             {
                     int sleep = int.MaxValue;
                     
                     //test cycle, I tried with the while cicle but it crashes Unity, 
                     // when this "for" cicle is executing , it reads data and prints them almost in sync when the serial device sends data
                     //If you have a best idea, please share !!!
                     for (int i = 0; i <= int.MaxValue; i++)
                     {
                         read_data();
                         //wait 0.5 seconds and read again
                         yield return new WaitForSeconds(0.5F);
                         
                     }
                     Thread.Sleep(sleep);
             }
             
             /*
             *Read the data from the serial port
             */
             void read_data()
             {
                      if (objComport.LastError == 0)
                      {
                             datareceived=objComport.ReadString();
                             //Print data on Console
                             print( "Data Received: " +datareceived)
                      }
                      else
                      {
                              Debug.Log("No data");
                      }
        
             }
     
     
 }


This code is not perfect and I need to understand how this dll component works and build my own dll solution, I've been investigating and most likely is that it uses c++, c# and win32. I'm still seeking how to do it.

If you have some best idea or suggestion, please share. My idea is build a free dll component for manage serial ports inside Unity.

Sorry for my poor English.

more information: http://forum.unity3d.com/threads/alternative-for-reading-serial-ports-into-unity.336326/#post-2176862

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

Answer by Mondkin · Jul 03, 2016 at 04:48 AM

Check this site that has one package that allows communication with COM ports from Unity: http://ardity.dwilches.com/

Comment
Add comment · Show 2 · 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
avatar image Jyito · Dec 10, 2018 at 05:32 PM 0
Share

Hey $$anonymous$$odkin, your link is dead, can you repost it please. I would like to have a look at the software.

avatar image Mondkin Jyito · Dec 10, 2018 at 07:50 PM 0
Share

Thanks for letting me know, now it's updated.

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

7 People are following this question.

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

Related Questions

Cannot do anything except log form ui buttons 0 Answers

Change from Editor utility to Game Object ,Change Editor Utility in to Game Object 0 Answers

UI 4.6: custom anchors breaking sizeDelta 0 Answers

Unity 4.6: Change Default GUI Skin? 1 Answer

Unity 4.6 Buttons not responding in second canvas? 7 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