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
5
Question by Carkey · Sep 10, 2012 at 01:22 PM · controllerhardwareioserial

Has anyone got serial working? BytesToRead has NullReferenceException

Hi all,

I have a custom piece of hardware that communicates over serial (virtualised on USB) that I have working with an old game and I want to update the new game by re-creating it in Unity to make use of the really nice things I see Unity can do.

I've stumbled at the first hurdle though because I can't seem to get my script which polls the serial port for data to work.

I open the serial port and that's fine but if I execute `int bytesToRead = serialport.BytesToRead;` it crashes out with a NullReferenceException:

NullReferenceException: Object reference not set to an instance of an object

System.IO.Ports.WinSerialStream.get_BytesToRead () System.IO.Ports.SerialPort.get_BytesToRead () (wrapper remoting-invoke-with-check) System.IO.Ports.SerialPort:get_BytesToRead () SerialTest.Update () (at Assets/SerialTest.cs:25) I've seen some other questions on here about using serial but I don't have Pro to use a plug-in and I already have the c# code I used for my old version of the game (it was written in XNA) so it'd be great to just port that over but for some reason BytesToRead gives me the NullReferenceException! Has anyone got around this problem or worked out what it is? Thanks a lot for reading, Carkey P.S. Here is a stripped down version of my code just to try and get this working: public static SerialPort sp;
// Use this for initialization void Start () { sp = new SerialPort("COM4", 115200, Parity.None, 8, StopBits.One); sp.Handshake = Handshake.None; sp.ReadBufferSize = 1024; sp.WriteBufferSize = 1024; sp.ReadTimeout = 20; sp.WriteTimeout = 20;
print ("Serial port object created.");
sp.Open(); }
// Update is called once per frame void Update () { byte[] buffer = new byte[1024];
if(sp.IsOpen) { print ("serial is open!");
int bytesToRead = sp.BytesToRead; print ("BYTES TO READ: " + bytesToRead); } } EDIT: Also, is there a way to break on breakpoints in MonoDevelop? If I put a breakpoint in, it doesn't seem to do anything :-/ EDI 2: Okay BytesToRead still doesn't work but you can blindly call the blocking `ReadLine()` or `ReadByte()` etc. This is only reliable if you KNOW the hardware will always be sending data, I am writing the code for the hardware too so I've got it to send ping faster than every frame and then relevant data when it gets it so it seems to work so far. Would be so much more useful if BytesToRead worked though :/
Comment
Add comment · Show 6
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 Bunny83 · Sep 10, 2012 at 01:55 PM 0
Share

Well, i don't see a reason why it shouldn't work. According to the $$anonymous$$ono Compatibility page the class is fully supported on .NET 2.0. Are you sure you build with the correct .NET / $$anonymous$$ono target?

I never used the SerialPort in C# / .NET. I grew up with Pascal / Delphi ;) The other day i might try it out, but unfortunately the serial port is not longer a "common interface" on modern PCs. However, i like hardware which works in a way that can be understood :D

Btw: It is possible to debug a Unity project, but you need to start Unity from $$anonymous$$onoDevelop or attach the debugger to Unity. I rarely used it in the past. I mainly use Visual Studio which can't be used as debugger (afaik).

avatar image Carkey · Sep 11, 2012 at 08:34 AM 0
Share

Hi Bunny83, thanks for the reply.

I managed to get serial working now sort of BytesToRead is still a "NullReferenceException" but you can just blindly call "ReadLine()" or "ReadByte()" etc. I am controlling the software on the serial hardware so I can know there will always be lines to read but it would be nice to have BytesToRead.

Ah I've just googled what you said about the $$anonymous$$onoDevelop debugger, I'll try linking it into Unity like you say.

Thanks.

avatar image DCrosby · Aug 21, 2015 at 03:41 AM 0
Share

I'm finding myself with the same / similar issues. Why are BytesToRead and BytesToWrite always null references ? Is that some kind of compatibility issue with that the Serial IO should really be in it's own thread, and not be blocked by Update() ? alt text

I'm using this with an old camera that had the ability to control the lens, and I have the protocol, but I can't seem to get the bytes the camera sends, of where it is back. I tied implementing an adruino board, that mimics the behavior of the lens, to facilitate faster development, also thinking plenty of people have used arduino with unity, but typically the data sets are small, and update speed or dropping packets isn't an issue, as much as an annoyance. For me It's pretty cruicial to get timely and accurate data back.

I also tried to get this to work in an IEnumerator, as well as a normal procedure, and neither worked.

serialio.png (53.7 kB)
avatar image paul_hayes · Dec 10, 2015 at 04:49 PM 0
Share

Incredibly, 3 years later and this bug still exists. I was able to work around it in my script, but it was extremely difficult and is an inefficient solution.

avatar image wijesijp · Jun 26, 2016 at 08:18 AM 0
Share

I am having the same problem.

Is there a solution for this?

avatar image wijesijp wijesijp · Jun 30, 2016 at 05:34 PM 0
Share

Bump .....

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by RevoltingMuffin · Nov 04, 2016 at 05:02 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
avatar image
0

Answer by Roc · Jan 03, 2016 at 09:23 AM

this blog can good help:http://pxp1230.github.io/Arduino/Unity%E4%B8%8EArduino%E9%80%9A%E4%BF%A1/Unity%E4%B8%8EArduino%E9%80%9A%E4%BF%A1.html

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

16 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

Related Questions

Controller Not Recognized on One Computer 0 Answers

Thrust controller (hardware) as input in Unity 0 Answers

how to get a custom built controller/joystick working with unity game? 1 Answer

MMD How to export model and animations to Unity as 3rd person controller? 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 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