x


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 :/
more ▼

asked Sep 10, 2012 at 01:22 PM

Carkey gravatar image

Carkey
31 7 7 9

Well, i don't see a reason why it shouldn't work. According to the Mono Compatibility page the class is fully supported on .NET 2.0. Are you sure you build with the correct .NET / Mono 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 MonoDevelop 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).

Sep 10, 2012 at 01:55 PM Bunny83

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 MonoDevelop debugger, I'll try linking it into Unity like you say.

Thanks.

Sep 11, 2012 at 08:34 AM Carkey
(comments are locked)
10|3000 characters needed characters left

0 answers: sort newest
Be the first one to answer this question
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x848
x63
x41
x30

asked: Sep 10, 2012 at 01:22 PM

Seen: 2458 times

Last Updated: Sep 11, 2012 at 08:36 AM