Press "Enter" to skip to content

Serial Com Ports in Unity (+Arduino stuffs)

Hey everyone! Sorry it’s been such a long time. I’ve been super busy with work and all most of the stuff I’m working on cannot be shared publicly. However, one project I’m working on requires Serial Port communications and the scripts I had been using started to lock up and no longer work. It’s a common pain with Serial Ports and threading in Unity’s .NET. Threads don’t close, ports don’t close, programs lock up, etc, etc. So I spent some of my personal time enhancing DWilches’ SerialCommUnity project on Github.

DWilches’ SerialCommUnity project is a great basis for doing serial port communication in Unity. Where it seemed to break down for us was in the areas of ports closing and the thread joining. Also, I really didn’t like that it made use of Unity’s SendMessage method instead of callbacks. Also, it wasn’t very self-contained. Meaning it didn’t start itself, didn’t handle reporting messages back on Unity’s main thread very well, etc.

So what I’ve done is:

  • Made AbstractSerialThread responsible for complete init and teardown of the thread and port.
  • It also contains all the delegates a programmer might need to hook in to.
  • I removed all calls to UnityEngine.Debug as they can get a bit wonky when called off of the main Unity thread.
  • AbstractSerialThread now instantiates a Monobehaviour that will dequeue incoming messages, errors, and the callbacks that are made in the separate thread. This makes it all “Unity thread safe”.

So now all the programmer needs to do is create an implementation of AbstractSerialThread, subscribe to delegates, and construct. And there’s already a basic implementation for reading/writing serial lines ready to go. I also decided that I really need a terminal to test this out well. So I made a scene with a basic terminal.

The direct download to the package is here.

Check out the full GitHub repository here.

If you clone that repository and run the Terminal scene then you’ll be able to use this to communicate with serial devices like Arduinos. With this you can do incredible things like use the Arduino as an input device by adding buttons, accelerometers, analog joysticks, and more! Or maybe you want to turn a Raspberry Pi into a gaming console and would love to show a high score display. The possibilities are endless.

Here’s a little Arduino sketch that will simply reply with the input you sent. From there just program to handle specific inputs.

Cheers!

Further thoughts

Even with these changes to DLWiches’ SerialPort library a Unity has issues with serial ports. This may likely be due to the fact that Unity is using Mono C# and not really .NET. Even if they were, a lot of developers complain about the .NET implementation for serial ports. One big issue comes around disconnecting and reconnecting. Sometimes the serial port won’t free up until the application closes (sometimes not until you physically unplug it). Is it Mono’s fault, Unity’s, this Serial port library, or device driver, or Windows? Who knows? Too many variables in there to exclude. There is a C++ library for dealing with serial ports, but I’ve not had a chance to wrap it for use in Unity and then I’m not sure if it will work on Android (which theoretically, DLWiches’ library should). Someday I plan on using that.

Please share your experiences and projects using serial ports and Arduino. There’s a lot of fun out there to be had!

Loading

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *