Archive for August 18, 2010

Accessing the USB port

Posted: August 18, 2010 in API
Tags: ,

The Arduino is an inexpensive, easy to find, open-source hardware prototyping platform.  You can connect a wide variety of sensors and other electronics to it, and then using the open-source as3glue “Physical Interaction Library for Flash” you can communicate via the USB port with Flash or AIR.
Arduino: http://www.arduino.cc/

as3glue: http://code.google.com/p/as3glue/

  1. Get an Arduino, a basic one goes for about US$30.  Download the driver and software for your OS from the “Download” section of the Arduino web site and install them.
  2. Download the as3glue files from the Google Code website.  It will include the installs for Standard Firmata and Serproxy that you need for later steps.  (Note: The Serproxy included is for Windows and Mac only.)
  3. Plug your Arduino into the USB port and upload the Standard Firmata.  This sets up the Arduino to use a standardized protocol for communicating with a host computer.
  4. Unplug your Arduino and connect up your sensor, probably to one of the analog input pins.
  5. Install Serproxy on your computer.  (Or another serial port proxy if you are not using Windows or Mac.  Sorry but I can’t help you with that.)  Serproxy will take the USB input from the Arduino and act as a serial server which your Flash or AIR app will open a Socket connection to.
  6. Plug the Arduino into your USB port, start Serproxy and you’re ready to use as3glue to receive the sensor’s output in Flash or AIR.  There are some example .fla files included in the as3glue download that show how to code this.
  7. Celebrate! 🙂
PS:  If you have a problem with loading the Standard Firmata that comes with as3glue onto your Arduino you may want to try a newer version, which you can download from the Firmata Wiki here http://firmata.org/wiki/Main_Page.

For communicate with device (in my case a biometric scanner) via RS232. This involves interpreting the serial output from the device and sending the device commands as required.

Use the freeware application ‘Serproxy’ to redirect serial communication with the device via a network socket connection to my Air application.

Serproxy can be found here:  http://www.lspace.nildram.co.uk/freeware.html

Serproxy is a multi-threaded proxy program for redirecting network socket connections to/from serial links, in cases where the remote end of the serial link doesn’t have a TCP/IP stack (eg an embedded or microcontroller system). The proxy allows other hosts on the network to communicate with the system on the remote end of the serial link. The Linux version requires a recent version of libpthread to compile/run.’

Follow the readme to configure the program, its very simple and in my month or so of use has been rock solid.

Note: I found that I had to configure ‘newlines_to_nils=false’ (not true as suggested in the config comments) to get consistant text from the serial port.

Now for the socket connection in you Air application:

I created a singleton class called BiometricScanner. I will leave you to look up singetons in as3 elsewhere should you require them as there are more complete solutions than the one I implemented.

The basics of the socket connection to Serproxy are simple – initialise the connection (if serproxy is running on your local machine: localhost, 5331), then parse and handle incoming data and send outgoing commands as required.

private var _socket:Socket;

        /**
         * Constructor
         */
        public function BiometricScanner(caller:Function = null)
        {
            // init socket connection for serial to tcp/ip bridge
            _socket = new Socket();
            _socket.addEventListener(Event.CONNECT, onConnect);
            _socket.addEventListener(ProgressEvent.SOCKET_DATA, onSocketData);
            _socket.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
        }

        /**
         * connect to the biometric scanner socket.
         */
        public function connect(scannerSocketIPAddr:String, scannerSocketPort:Number):void
        {
            _socket.connect(scannerSocketIPAddr, scannerSocketPort);
        }

        /**
         * disconnect from the scanner.
         */
        public function disconnect():void
        {
            _socket.close();
        }

        /**
         * handles scanner connect successfult event.
         */
        private function onConnect(event:Event):void
        {
            // dispatchEvent(…)
        }

        /**
         * on IO Error
         */
        private function onIOError(event:IOErrorEvent):void
        {
            // dispatchEvent(…)      
        }

        /**
         * called when data received from the socket connection
         * AKA the serial connection
         */
        private function onSocketData(event:ProgressEvent):void
        {
            try
            {
                var temp:String = _socket.readMultiByte(_socket.bytesAvailable, "ISO-8859-1");

                // process buffer, throw an event, whatever you want
                // processBuffer(temp);
            }
            catch(err:Error)
            {
                // TODO: throw exception or event
            }
        }

        /**
         * send text command to scanner.
         */
        public function sendSerialCommand(cmd:String):void
        {
            _socket.writeUTFBytes(cmd);
            _socket.flush();
        }

Finally we need to included Serproxy and its config as an asset to be deployed with my air application. Now we need to addedd Serproxy to windows startup although you could launch it from you app as needed if using Air 2.

Alternatively you could use merapi and write a serial reader/writer in java.


Adobe’s Flash technology has been taking a beating lately. Apple (AAPL) still won’t support it on its upcoming iPad or its iPhone. Steve Jobs calls it buggy and crash-prone and dismisses Adobe (ADBE) as being lazy. Adobe is trying to fight the negative vibes emanating from Cupertino and elsewhere. It has already pointed out that it will be easy to convert Flash apps into iPad apps, and now CTO Kevin Lynch is weighing in to defend Flash.

In a blog post Tuesday, Lynch addresses the two major threats to Flash: Apple’s refusal to support it on mobile touchscreen devices and the rise of HTML5 as a new, open standard which may one day replace Flash. On Apple, Lynch says Adobe is ready and able to put Flash on the iPhone, the iPad or anything else Apple can throw its way. But, as has been the case for more than a year, the ball is in Apple’s court:

We are ready to enable Flash in the browser on these devices if and when Apple chooses to allow that for its users, but to date we have not had the required cooperation from Apple to make this happen.

Lynch points out that the next version of Flash for smartphones, 10.1, is about to become available and that practically all other smartphones will support it, including Android, Blackberry, Nokia, and Palm Pre. If they can handle it, why can’t an iPhone?

But the bigger long-term threat to Flash is HTML5, especially for rendering video. Lynch says that 75 percent of video on the Web currently is shown in a Flash player. That number could decline if HTML5 video starts to take off. Google (GOOG) (via YouTube, Chrome, and other products) and others are pushing HTML5 hard. Lynch tries to pretend that HTML5 is not a threat, saying in the same breath that Adobe supports HTML5, but its incompatibilities across browsers spells doom for the Web. He writes:

Adobe supports HTML and its evolution and we look forward to adding more capabilities to our software around HTML as it evolves. If HTML could reliably do everything Flash does that would certainly save us a lot of effort, but that does not appear to be coming to pass. Even in the case of video, where Flash is enabling over 75% of video on the Web today, the coming HTML video implementations cannot agree on a common format across browsers, so users and content creators would be thrown back to the dark ages of video on the Web with incompatibility issues.

HTML5 is still a young technology, and those incompatibility issues can be solved over time. Flash is still a more capable technology when it comes to rendering video, but HTML5 is advancing faster and as a native Web standard it has many other advantages which may help it win over time.

Adobe is in a battle for developers, who buy its Creative Suite software to make Flash apps. As long as Flash is the de facto standard for video and animation on the Web, those sales will not be threatened. But if Flash developers migrate to other technologies to build better apps for the Web and mobile devices such as the iPhone and iPad, Adobe’s competitive position will be weakened. It will defend Flash to the death.