Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 eekdro · Nov 26, 2011 at 05:01 AM · callbackserial

'SerialDataReceivedEventHandler' is not working.

Please help me. 'SerialDataReceivedEventHandler' is not working.

Tell me. If you know other way.


using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO.Ports;

namespace SerialPortCtrl {

 public class ComPort
 {
     SerialPort SP = new SerialPort();
     List<string> RevData = new List<string>();

     public ComPort()
     {
     }

     ~ComPort() 
     {
         SP.Close();            
     }

     private void SP_DataRecieved(Object sender, SerialDataReceivedEventArgs e)
     {
         RevData.Add(SP.ReadLine());
     }

     private void SP_ErrorRecieved(Object sender, SerialErrorReceivedEventArgs e)
     {
         RevData.Add("Error while receiving data. " + e.ToString()); 
     }
     
     public string GetData()
     {
         string RevStr = "";
         if ( RevData.Count == 0) return "";
         RevStr = RevData[0];
         RevData.RemoveAt(0);
         return RevStr;
     }

     public bool SendData(string Data)
     {
         if (SP.IsOpen)
         {
             SP.WriteLine(Data);
             return true;
         }
         else return false;
     }

     public bool Open(string PortName, int BaudRate = 9600, int DataBits = 8)
     {
         
         SP.PortName = PortName;
         SP.BaudRate = BaudRate;
         SP.DataBits = DataBits;
         SP.Parity = Parity.None;
         SP.StopBits = StopBits.One;
         SP.ReadTimeout = (int)500;
         SP.WriteTimeout = (int)500;
         SP.ReadBufferSize = 4;
         SP.WriteBufferSize = 4;

         SP.DtrEnable = true;
         SP.RtsEnable = true;

         SP.DataReceived += new SerialDataReceivedEventHandler(SP_DataRecieved);
         SP.ErrorReceived += new SerialErrorReceivedEventHandler(SP_ErrorRecieved);
         SP.Open();
         
         if (SP.IsOpen) return true; else return false; 
     }

     public void Close()
     {
         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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by boblol · Jun 08, 2012 at 07:35 PM

I've come across the same issue trying to get my [arduino][1] to communicate to unity through serial. I think the reason is due to monos implementation of SerialPort is not complete...

After some digging around I found [SerialPort.ReceivedBytesThreshold][2] property:

The number of bytes in the internal input buffer before a DataReceived event is fired. The default is 1.

Remarks: The DataReceived event is also raised if an Eof character is received, regardless of the number of bytes in the internal input buffer and the value of the ReceivedBytesThreshold property.

If you try and log your serial port's ReceivedBytesThreshold property you get this error message:

NotImplementedException: The requested feature is not implemented.

System.IO.Ports.SerialPort.set_ReceivedBytesThreshold (Int32 value) (wrapper remoting-invoke-with-check) System.IO.Ports.SerialPort:set_ReceivedBytesThreshold (int) GyroSyncBehavior.Start () (at Assets/Scripts/GyroSyncBehavior.cs:23) I'm guessing that the DataReceived event doesn't fire under Unity's .NET 2.0 mono version, but I'm a noob so I might be wrong. [1]: http://www.arduino.cc [2]: http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.receivedbytesthreshold.aspx
Comment
Add comment · Show 3 · 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 Artifactx · Jul 21, 2012 at 05:44 PM 0
Share

Did you manage to figure this out, I'm having the same problem as you guys right now.

avatar image Ben-BearFish · Mar 27, 2015 at 06:02 PM 0
Share

Any workaround for this? I'm having the same issue.

avatar image XYHC · Apr 15 at 05:15 PM 0
Share

10 years later, same issue.

avatar image
0

Answer by CACIeLearn · Jan 22, 2018 at 05:30 PM

It's 2018, has this been fixed yet?

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 Cheeeese · Jan 26, 2018 at 07:02 PM

I still get that the call has not been implemented... I wish someone in the Unity development team shows more love to the serial port calls. We really need those to be complete.

alt text


unitybug.jpg (24.2 kB)
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

7 People are following this question.

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

Related Questions

Can't access to Time.time in callback method AsyncCallback 1 Answer

How to do a "custom" callback ? (like OnMouseEnter()) 3 Answers

Action delegate doesn't show in inspector. 1 Answer

A way to detect when the Editor Application is focused 7 Answers

Admob, Can't call a function from HandleUserEarnedReward() for some reason. 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