- Home /
Receiving information from a serial port
I'm new to Unity3D, in fact just today. I'm doing a project that requires input from a device that sends data to a computer via a serial port. Is there a way to program Unity in C/C++ to be able to open a serial port and receive data streamed from it?
Answer by Magnus Wolffelt · Aug 07, 2010 at 09:39 AM
Shouldn't need a plugin, at least not with Unity 3.0:
"Then along comes .Net 2.0, and this time Microsoft added the System.IO.Ports Namespace, and within that was the SerialPort Class. DotNet developers finally had an intrinsic way of serial port communication,"
http://msdn.microsoft.com/en-us/library/system.io.ports.aspx
Also, be sure to set API compatibility level to .NET 2.0. The default is something like .NET 2.0 reduced.
Have you actually tried this? In my tests, I get nothing but exceptions, if I'm lucky it doesn't hang. The Read functions all fail, and simply having ReadLine compiled in causes it to hang. I've read elsewhere the fault is with $$anonymous$$ono.
Ah, finally got it working, thanks! ReadLine blocks the thread, so using a short timeout and alternate thread helps
Answer by Rasmus Schlnsen · Sep 26, 2010 at 06:33 PM
I've just did a blogpost on communicating with an Arduino device using System.IO.Ports.SerialPort class. Basically you create an instance of the SerialPort class in Start() specifying the COM port and baud rate, and then in Update your extract data.
If you're interested you can find the post here
I was not able to get this working, does it still work in 3.3?
Answer by Eric5h5 · Aug 07, 2010 at 07:32 AM
You'd need to write a plugin for that (requires Unity Pro).
Great! So it is possible, good to know. Thanks! Any quick pointers on where that might be in the documentation: writing a plugin.
Answer by David Thomas · Dec 13, 2010 at 02:05 PM
I have a PIC based controller that uses a USB virtual serial port. I've been using it with XNA and all my interface software is in C#. I'll let you know how it goes with getting it to talk with Unity. Any advice out there?
Answer by subhash · Jun 10, 2011 at 01:11 PM
I am trying the same thing whenever I use readline . the framerate goes down to 1fps and if I try coroutine then then the data is not correct it is merged one inside the another.
Can somebody help on this Regards Subhash
Your answer
Follow this Question
Related Questions
How to use native C++ classes in Unity 1 Answer
Zooming out by moving camera away from target(scrollwheel) 1 Answer
DirectInput in Unity 0 Answers
Can I use c plus plus to program a game in unity? 1 Answer
IUnityEventQueue - how do I use it? 2 Answers