Read/Write to serial port from Adobe Air

Posted: August 18, 2010 in Thoughts

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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s