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 Raikenkun · Jan 22, 2013 at 04:20 PM · serialserial.io.portcom

Serial port Read problem (DataReceivedHandler)

I'm using the "System.IO.Ports" I don't quite get it but is it because Mono library is not the same implementation as .NET so not everything will be working exactly the same sometimes.

My first issue is DataReceivedHandler does not work at all. So the other options are ReadLine or Read but these too can lead to Unity Crashing or being stuck and there's no way to Kill Unity task unless you restart. ReadExisting sometimes work.

The device I'm connecting to requires RTS/CTS flow control, that means I need Handshake set on RequestToSend and that's all the changes needed?

So currently I don't know if there is a stable way to read without getting into some issues. BTW i tried my app on visual studio and it works fine. sample of the code i use in unity:

     public static SerialPort sp = new SerialPort("\\\\.\\COM12", 38400, Parity.None, 8, StopBits.One);   
 
    void Start () 
     {
            print("started!");
            sp.Handshake = Handshake.RequestToSend;
            sp.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
                     sp.Open();  // opens the connection
                     sp.ReadTimeout = 50;  // sets the timeout value before reporting error
                     sp.WriteTimeout = 500;
                     sp.DtrEnable = false;
 }
 
     private static void DataReceivedHandler(
                         object sender,
                         SerialDataReceivedEventArgs e)
     {
         SerialPort sp = (SerialPort)sender;
         string indata = sp.ReadExisting();
         print(indata);
 }
 


Comment
Add comment · Show 2
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 Raikenkun · Jan 22, 2013 at 05:41 PM 0
Share

I did search and found several issues but I couldn't understand why its not working as intended.

But now I know what I have to do.

Thanks Wolfram :)

avatar image kamran-bigdely · Sep 12, 2016 at 08:14 PM 0
Share

I have the same issue. I put it (ReadLine() function) in another thread and still it did not work. I have googled and read other answers here and there but none of their solution works on my machine with Unity 5.4. It is notable that the same code works on Visual Studio.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Wolfram · Jan 22, 2013 at 05:10 PM

Please search UnityAnswers and/or Google, this issue has been discussed several times before already.

Unfortunately, in the Mono versions Unity uses, things like DataReceivedHandler, ReadExisting, or BytesToRead are not implemented and/or buggy, so you can't use them.

Read and ReadLine are "blocking" functions, they will pause execution if no data is available , or until a newline is received, respectively.

Your best chance is to move your Read/ReadLine stuff into a separate function, and then run that function in a separate Thread.

Comment
Add comment · Show 1 · 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 kamran-bigdely · Sep 12, 2016 at 08:15 PM 0
Share

Putting Read()/ReadLine() on a separate thread did not work for me. I can see the port is Open but it does not read the data.

avatar image
0

Answer by RevoltingMuffin · Nov 04, 2016 at 07:09 PM

Please visit the following web site to view updates on this issue and comment letting the Unity staff know how important this functionality is to the COM Port and TTY reading community! VOICE YOU OPINION!

https://issuetracker.unity3d.com/issues/serialport-bytestoread-returns-null-reference

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.

Follow this Question

Answers Answers and Comments

12 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

Related Questions

Write to Serial port 1 Answer

How to List Available Com Ports? 0 Answers

Long lag in serial communication with Arduino 4 Answers

Calling COM Object from Standalone unity project/exe 1 Answer

Are serial (RS-232) events with Mono 2.6 supported? 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