Recently I did a kids game in Flash that was deployed as a kiosk and which toured a handful of the nations zoos. One of the requirements for the game was that at the end, it would ’sneeze’ on the player – emitting a brief blast of distilled water. After a fair amount of research I found what I needed in SWFKit, one of the leading ‘wrapper’ tools for Flash. By using SWFKit you have all sorts of access to various Windows components like the registry, dialogs, file system and a lot more. I find it invaluable for kiosk work.
The essential component however was an ActiveX component, created by the same SWFKit team. It is called CPortX ActiveX Component and is what allows SWFKit and Flash to communicate with one of your com ports.
So first, you need SWFKit and you can get that at: http://www.swfkit.com
Once you have it installed, you need to get the CPortX ActiveX component, which you get from the same site at: http://www.swfkit.com/swfkit/download.html#tools
Once you have the component installed you’ll be able to send messages to a com port directly from Flash.
The second phase in my research was then how to get the com port to now turn a pump on and off. After much searching I found what I needed in an RS-232 controlled relay from relaycontrollers.com
For this project I used their R15RS – a fairly low cost, single relay controller with a 5 amp rating. I also picked up a power supply for it, and a cable to go between it and the computer’s com port. All together the three pieces were just under $100.
With this simple setup: the ActiveX component and the relay, you can control all sorts of devices from Flash. I however, needed to control a ’sneezer’. What I did for this was to use a windshield washer pump, available at any auto parts store, with the output going to a nozzle, that could be set to spray anything from a stream to an atomized vapor. Think kitchen cleaner nozzle and you’ll get the picture.
Here’s an image of the relay controller that was used:

The labels, NO, COM and NC mean Normally Open, Common, and Normally Closed. When the relay is off, ie the coil is not energized, the Common arm connects to the Normally Closed connection. When the relay is energized, the Common swings over the the Normally Open connection. With that in mind, the pump and its power supply would be connected like so:
When the relay is turned on the Common swings over to Normally Open and provides power to the pump. Simple as that.
The next step is to write a bit of code to send the proper signals to the com port. For this, the code is written within SWFKit and then called from Flash using ExternalInterface.
Within SWFKit choose to make a new file. This will add an Initialize script to the script list, which is called automatically. Within initialize you need to create an instance of the CPortX ActiveX component:
var commx = new ActiveXObject("CPortX.ComPortX"); if (commx == null) { Dialogs.msgBox("Please install the CPortX ActiveX control!"); return false; } commx.syncMethod = 2; getMainWnd().onClose = function () { commx.close; }
First, we try and create an instance of the component. If it’s null, then it hasn’t been installed and a dialog is presented. The syncMethod is then set to 2, which sets the baud rate and such. Finally, the onClose method is created that closes down the component when the projector is closed.
Next, we need two more scripts – one to turn the relay on, and one to turn it off. You do this by right clicking the Script panel and selecting New Script. First – the relayON script:
if (commx.connected) commx.close; commx.port = "COM4"; commx.open; commx.writeStr("\xFE\x01"); commx.close;
Using the commx instance created within the initialize script, the port is set to com4, opened and then written to before being closed. By sending the hexadecimal FE and then 01 the relay is instructed to turn on. The FE instructs the relay (or modem) to ‘listen’ and then you send the command. In this case a 1 turns the pump on. Makes sense.
Turning it off is just as simple, sort of. Here’s the relayOFF script:
if (commx.connected) commx.close; commx.port = "COM4"; commx.open; var ss = new StringStream; ss.put('\xFE'); ss.put(0); commx.write(ss); commx.close;
The difference here is that I could not use commx.writestr(“\xFE\x00″); – it simply would not work to send a 0 this way. The great folks at SWFKit provided the fix, telling me to use the StringStream object to send it instead. But the same thing happens – we send the FE telling the relay to listen, and then 0 to turn it off.
Lastly, we just need to call these two scripts from within Flash using ExternalInterface. First I have a turnOn() method which calls the SWFKit relayON script:
function turnOn():void { ExternalInterface.call("ffish_run", "relayON"); }
This is called from a sneeze method, which calls turnOn(), and also starts a timer to call turnOff():
function sneeze():void { turnOn(); //relay myTimer = new Timer(400, 1); myTimer.addEventListener(TimerEvent.TIMER, turnOff); myTimer.start(); } function turnOff(e:TimerEvent):void { myTimer.removeEventListener(TimerEvent.TIMER, turnOff); ExternalInterface.call("ffish_run", "relayOFF"); }
When sneeze() is called, it calls turnON() which turns the relay on, and in turn the pump. A timer is then started which calls turnOff() after 400 ms. The turnOff() method removes the timer listener and calls the relayOFF script within SWFKit. As you might have guessed the 400 ms timing was decided on by simple testing… it gave the pump just enough time to send out a nice little burst of water, aimed right at the player’s face. Believe it or not, it was a big hit.
With this kind of setup – SWFKit and a controllable relay there is a lot of possibility to do things from within Flash that might have otherwise been impossible. The relay company also makes multi-channel relays, which you could use to enable switching of various devices all from the same application. Lots of potential here.


