RS-232 COMM support

I want to know if Unity can send output command to COMM ports (RS-232)? If so, does it require third party plug in? And in sending the command does it use the internal javascript language? I want to be able to create interactive experience with my project.

regards,

Jeff

no. but you could create a plugin that does that for you. there isn’t anything available that i’m aware of.

cheers.

Unity 2.0 is based on the latest version of Mono… which is is based on .NET 2.0… which has the System.IO.Ports namespace… which can be used for RS232 and USB communications.

I haven’t upgraded to Unity 2.0 (yet), could someone please try to use System.IO.Ports and see if it is in there?

well this doesn’t return anything at all. Empty array.

using UnityEngine;
using System;
using System.IO.Ports;

public class SerialPortExample : MonoBehaviour {
	public void Start () {
	    // Get a list of serial port names.
	    string[] ports = SerialPort.GetPortNames();

	    Debug.Log("The following serial ports were found: ");

	    // Display each port name to the console.
	    foreach(string port in ports) {
	        Debug.Log(port);
	    }
	}
}

Unfortunately. :cry:

Then again, when it comes to C# I have a few kangaroos loose in the top paddock. :wink: So my code could be wrong.

Cheers.

So you re saying that using Unity’s native scripting language, you can connect with RS-232 and USB communication ports?

regards, Jeff

He said “empty array” which I assume means the array of serial ports was empty, and thus there were no serial ports.

I have no idea how mono handles serial ports. (I don’t think any of my current computers even have them anymore!) But you could make a plugin to do serial communication without much trouble at all.

yes stupid me. I guess you would need to test the above code on a PC with serial ports to see if it returns anything. Not on an iMac that definitely doesn’t have any serial ports! :wink:

Cheers.

I posted a follow-up question over in this thread, but regarding this specific test program, I tried it now on two different Windows boxes, with serial ports, and in both cases I got an exception on the GetPortNames() function:

NotImplementedException: Detection of ports is not implemented for this platform yet.
System.IO.Ports.SerialPort.GetPortNames () 
SerialPortExample.Start ()

The code executes without problems on Mac, but since I don’t have any Macs with serial ports (nor does anyone else anymore, I presume?), it is hard to test whethter this would return the correct ports. On the other hand, maybe we’ll just buy one of these USB-serial adapters, and see if they register correctly as a serial device with the various OS’es. This may get the Mac port detection running, but doesn’t help in identifying serial ports on Windows, as long as this exception pops up. It probably wouldn’t get me much further in using the serial port either, as I hit the problems described in the other thread…

If anyone has any input on this, I’d be happy to know about it.[/code]

yeah, I don’t think there’s any way around it. until Unity updates Mono, you’re stuck with writing a Unity native plugin (as we did) to provide this functionality.

thankfully, it’s less monumental of a task than it seems; the OS X and Windows serial port APIs are actually fairly similar, so writing a thin wrapper with a few strategically placed #ifdefs should do the trick.

Thanks for that confirmation. It’s the same conclusion I was getting at myself (eventually). Tried just briefly to make a plugin of the Apple Serial Port Sample code, and it worked ok. I guess a slightly slimmed down approach for our specific needs would be a good place to start, and then port it to Win32, merging it with some exisiting serial com code we have there…

Oh, and first getting a USB-RS232 converter of course. :slight_smile:

At the moment we’re just spec’ing up a new project for a potential client, so it’ll probably be a little while before we’ll do this. But thanks for the input, anyway.

openframeworks has a pretty slim and cross-platform lib (c++) for serial communication that you could probably turn into a plugin fairly easily.

Then there’s http://todbot.com/arduino/host/arduino-serial/arduino-serial.c , if c is your flavor.

/Patrik

Great, we’ll take a look at that. At this point we’re pretty C-dialect-agnostic, so a range of options to choose from is just great. :wink:
Thanks!