- Home /
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:
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;NullReferenceException: Object reference not set to an instance of an object
// 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 :/
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).
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.
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() ?
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.
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.
I am having the same problem.
Is there a solution for this?
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
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
Your answer
![](https://koobas.hobune.stream/wayback/20220613080849im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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