• 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 /
  • Help Room /
avatar image
0
Question by emthele · May 31, 2016 at 08:49 AM · vrbug-perhapscommunicationsensorport

SerialPort.ReadExisting workaround needed

Hello everyone,

I'm having an issue with the SerialPort.ReadExisting method, which I think is neccessary for my purpose.

I have a set of inertial movement sensors (up to 16) which deliver data every 1 ms via bluetooth serial port profile. So each sensor streams every 16 ms. In Unity, I want to move the parts of a body (arms, legs etc.) depending on these sensor values.

My first approach with C# was with WinForms, where I could have a DataReceivedHandler and use the ReadExisting() method, then split for lines automatically. In Unity, ReadExisting() seems to be buggy, so I would have to use ReadLine() method instead. But, with ReadLine(), I would have to call this method asynchronously really fast (around 1 ms) in order to not lose received lines via COM port.

My current codes is a copy of some other code I found on the internet:

  public IEnumerator AsynchronousReadFromSerialPort(Action<string> callback, Action fail = null, float timeout = float.PositiveInfinity)
     {
         DateTime initialTime = DateTime.Now;
         DateTime nowTime;
         TimeSpan diff = default(TimeSpan);
      
         string dataString = null;
         //string[] serialPortLines;
      
         do
         {
             try
             {
                 dataString = serialPort.ReadLine();
                 //serialPortLines = serialPort.ReadExisting().Split(new string[] {"\n"}, StringSplitOptions.RemoveEmptyEntries);
             }
             catch (TimeoutException)
             {
                 dataString = null;
                 //serialPortLines = null;
             }
      
             if (dataString != null)
             {
                 callback(dataString);
                 //callback(serialPortLines[0]);
                 yield return null;
             }
             else
             {
                 yield return new WaitForSeconds(0.1f);
             }
      
             nowTime = DateTime.Now;
             diff = nowTime - initialTime;
      
         } while (diff.Seconds < timeout);
      
         if (fail != null)
             fail();
         yield return null;
     }
 
 
     void Update()
         {        
             if (comOpen == true)
             {
                 
                 StartCoroutine
                 (
                     AsynchronousReadFromSerialPort
                     (   (string s) => Debug.Log(s),     // Callback
                         () => Debug.LogError("Error!"), // Error callback
                         10f                             // Timeout (seconds)
                     )
                 );
 }

As you can see, currently I'm using ReadLine() in the Update() function, with is too slow. When I switch the code (commented lines in asynchronous read function) to make use of ReadExisting(), I'm getting this error everytime the ReadExisting() gets called.

 NullReferenceException: Object reference not set to an instance of an object
 System.IO.Ports.WinSerialStream.get_BytesToRead ()
 System.IO.Ports.SerialPort.get_BytesToRead ()
 System.IO.Ports.SerialPort.ReadExisting ()
 (wrapper remoting-invoke-with-check) System.IO.Ports.SerialPort:ReadExisting ()
 SerialComm+<AsynchronousReadFromSerialPort>c__Iterator0.MoveNext () (at Assets/UnityVS/Comports/SerialComm.cs:134)
 UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
 SerialComm:Update() (at Assets/UnityVS/Comports/SerialComm.cs:50)


Any help or advice?

Comment
Add comment · Show 1
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 emthele · May 31, 2016 at 10:22 AM 0
Share

Update:

I think I might have found the solution: I reduced the asynchronous routine above to a simple while loop which is true as long as the CO$$anonymous$$ port is open, and it seems to work. Framerate constant, data arriving and showing fast in a debug-textbox. I'll now investigate the actual timelapse between to read lines.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by emthele · Jun 09, 2016 at 11:31 AM

Update:

I was thinking wrong by using the ReadExisting() function. I managed to get my code working properly, the same way as previously in Visual Studio, by using SerialPort.Read(). There is simply no need to also read the serial port stream (ReadExisting), the input buffer (Read) is enough.

Now, Every time between the frame updates, I read all data from the serial port input buffer via SerialPort.Read(). I split it into the single data lines and process them. Next time, the same. This way I can transmit data at 1ms constantly, while processing them between the frame updates and not loosing data.

Nevertheless, the ReadExisting() function is still buggy, but that seems to be connected to the bug of checking for existing bytes in the input buffer or stream.

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 fooldome · Nov 15, 2017 at 08:16 AM 0
Share

Hi, I've been working on Arduino in Unity and have just experienced the same error using ReadExisting(). The strange thing is, it is working perfectly on my mac, I get really time responses. But now I've ported over to windows and I cant get ReadExisiting() to work at all. Only ReadLine() works but is so unusably slow it is not a solution.

Do you have any idea why ReadExisting doesn't work on windows but is O$$anonymous$$ on mac. I have tried using Read() as suggested but I'm struggling to implement. Can any further explanation be given?

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

70 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 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 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 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

[VR] Properly "Load if available" VR System 1 Answer

HELP: Arduino Genuino 101 to Unity3D. Serial Communication 0 Answers

Really high rendering time in build. 1 Answer

HDRP visual errors with VR 0 Answers

Models used for Hand Presence in UnityXR pop out of existence when close to the camera 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges