• 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 ChrisBurt · Jul 18, 2013 at 09:11 PM · c#.netserialport

Hard crash when accessing certain members of SerialPort

Hi! I am creating a SerialPort object and opening it. Then I'm accessing the BytesToRead property, at which point Unity implodes and I arrive at the Crash Report dialog. I'd like to avoid that.

Many other properties such as IsOpen seem to work fine. Is there something I'm not catching, or certain things I can't use from .NET or from System.IO.Ports?

More information about my situation:

I'm trying to use a Zephyr HxM heart monitor as an input device in my game. The HxM is a bluetooth device. I have it paired with a Rocketfish bluetooth USB stick. The stick opens three COM ports and the input from the HxM is constantly streaming in through one of them.

My project is configured as .NET 2.0. I'm on Unity Pro 4 on Windows 7.

My script:

 using UnityEngine;
 using System;
 using System.Text;
 using System.Collections;
 using System.Collections.Generic;
 using System.IO.Ports;
  
 public class PortReader: MonoBehaviour 
 {
     List<string> portNames = new List<string>();
     string portName = "COM8"; // THE HEART MONITOR IS CONSTANTLY INPUTTING THROUGH THIS PORT
     SerialPort serialPort;
     
     void Start () 
     {
         serialPort = new SerialPort();
     }
     void FixedUpdate() 
     {
         // OPEN THE PORT (IF NOT ALREADY OPEN)
         try
         {
             OpenConnection();
             print("Port open!");
         }
         catch(Exception e)
         {
             print("Problem opening port! -- " + e.ToString());
         }
         // GET DATA
         try
         {
             int bytes = serialPort.BytesToRead; // THIS LINE CRASHES UNITY
             // int bytes = 133; // THIS LINE DOES NOT
             byte[] comBuffer = new byte[bytes];
             serialPort.Read(comBuffer, 0, bytes);
             print(ByteToHex(comBuffer));
         }
         catch(Exception e)
         {
             print("Problem reading! -- " + e.ToString());
         }
     }
     private void OpenConnection()
     {
         if( serialPort != null && !serialPort.IsOpen )
         {
             serialPort.PortName = portName;
             // THESE SETTINGS ARE ACCORDING TO THE HXM MANUAL
             serialPort.BaudRate = 115200;
             serialPort.DataBits = 8;
             serialPort.StopBits = StopBits.One;
             serialPort.Parity = Parity.None;
             serialPort.Handshake = Handshake.None;
             // THE MANUAL SAYS THE HXM TRANSMITS ABOUT ONCE PER SECOND, SO I GIVE IT A LITTLE MORE THAN THAT BEFORE TIMEOUT.
             serialPort.ReadTimeout = 1200;
             print("Opening port " + portName + "! Time: " + DateTime.Now.ToLongTimeString());
             serialPort.Open();
         }
     }
     private string ByteToHex(byte[] comByte)
     {
         //create a new StringBuilder object
         StringBuilder builder = new StringBuilder(comByte.Length * 3);
         //loop through each byte in the array
         foreach (byte data in comByte)
             //convert the byte to a string and add to the stringbuilder
             builder.Append(Convert.ToString(data, 16).PadLeft(2, '0').PadRight(3, ' '));
         //return the converted value
         return builder.ToString().ToUpper();
     }
     void OnApplicationQuit()
     {
         if(serialPort != null)
         {
             print("Closing port!");
             serialPort.Close();
         }
     }
 }
 

PS This is my first question so let me know if it needs improvement!

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 maciejkrol · Oct 09, 2013 at 08:57 PM 0
Share

I have the same. Unity crashes when trying to access "BytesToRead" however reading works fine.

avatar image Kriogenic · Dec 24, 2015 at 10:05 AM 0
Share

I am having this same issue and I believe it is actually an issue with the SerialPort import in .NET 2.0

I found this link which shows how to use it if you must but I will admit I am having difficulties getting it to work correctly.

http://www.sparxeng.com/blog/software/must-use-net-system-io-ports-serialport

It happens specifically with BytesToRead and it would be great to have this all working. If you solve the issue please let us know here.

1 Reply

· Add your reply
  • Sort: 
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.

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

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Open With... a Build Unity program 2 Answers

Serial Communication Running Slow 0 Answers

System.IO.Ports missing for Unity with .NET 4.x 3 Answers

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