QueTwo's Blog

thouoghts on telecommunications, programming, education and technology

Connecting your Arduino to AIR using an AIR Native Extension

One of the things that has been bugging the the most when working with Arduinos and AIR is the fact that you needed some sort of proxy application running in order to allow the AIR (or Flash) application to talk to the Arduino.  You see, the Arduino only exposes a Serial port (via USB), and AIR is limited in this regard.  The way people have gotten around this so far is to have AIR create a TCP socket connection to some sort of Proxy application, which in turn opens the Serial port to the Arduino.  It works, and it works very well, however this is a separate application you have to ship, have the user configure, and run.  Heck, even me as the developer found this solution clumsy when demoing these solutions.

I finally decided to fix the problem and write an AIR Native Extension.  The AIR Native Extension (ANE) is a C based .DLL / .framework for the Windows/Mac platforms that allows AIR to essentially open a COM port.  I wrote it in a way that is supposed to emulate the functions of the flash.net.Socket library that is included in the AIR runtime.  I’ve posted the entire project, including the source code and final binaries on Google Code at http://code.google.com/p/as3-arduino-connector/ (well, everything except for my compilation scripts, which are specific to my computers). 

The biggest learning experience in creating this ANE was developing on the MacOS platform.  I’ve never done any programming targeted for that platform before, and working with XCode is just a pain.  I’m used to environments like Visual Studio and Eclipse, but XCode always seemed to fight me every step of the way.  From simple things like hiding all the project properties, to trying to force you into an MDI workflow.  Also, working with the lack of documentation on the AIR Runtime side was kind of depressing…  Don’t worry, a future blog post will try to fill everybody in on how to make an ANE using XCode.

Using the ANE :

  • Include the ANE into your project.  Make sure you are using Flash Builder 4.6 or later. Right-Click on your project in the Package Explorer (Project Explorer), and go to Properties.  Go to the Flex Build Path tab, and then the Native Extensions tab.  Click “Add ANE…” and bring it in.  It does not need to reside within your project source folder.
  • Next, import the com.quetwo.Arduino.ArduinoConnector and the com.quetwo.Arduino.ArduinoConnectorEvent packages.
  • Instantiate a new variable of type ArduinoConnector.
  • Check the isSupported() function to make sure that the ANE is supported on your specific platform, and if it loaded properly.
  • Call the getComPorts() function which will return an array of valid list of COM ports on your OS.  On Windows, this returns ALL COM ports that are valid in Windows, where on the MacOS platform, it will return any USB Serial Devices, which usually would only be your Arduino.
  • Make the connection to the Arduino by calling the connect(comPort, baud) function.  You will need to pass in one of the COM ports from the getComPorts() array, along with the baud rate that your Arduino is operating on.  If you are using Firmata, it is 57600.  Most other Arduino projects use 9600.
  • Next, add an event listener to listen for the “socketData” event.  This will fire when new data is available in the data buffer.  Don’t wait too long to pull data out of the buffer, because it is only 4k 🙂

Sending Data:

Sending data is just like the Socket class, except you don’t have to flush() the buffer — the data goes out in realtime.  The two most common ways to send data :

  • writeString(“hello world”);
  • writeByte(255);

Getting Data from the Buffer:

As data arrives and is placed into the buffer, the bytesAvailable variable is incremented to reflect how many bytes are available.  Most people will read data from the buffer using one of the two functions :

  • readBytesAsString();
  • readByte();

All of the read functions are FIFO, meaning they will return the oldest data in the buffer before they return the newest (First In, First Out).

On the Google Code site, I am posting more detailed documentation, including a simple patch to as3Glue which will allow as3Glue to work seamlessly with this ANE. 

Enjoy, and please provide feedback on this ANE.  It’s the first one I’ve released to the public and I’d like to know how it works for everybody!


57 responses to “Connecting your Arduino to AIR using an AIR Native Extension

  1. J December 2, 2011 at 10:42 am

    Seems to be a nice piece of work! Will be checking it out as soon as i can 🙂

  2. gepatto December 2, 2011 at 11:22 am

    I have some problems on osx 10.7…
    I’m getting Errors doing calls on an instantiated ArduinoConnecter object.
    getPorts works , but connect fails with the following error
    ArgumentError: Error #3500: The extension context does not have a method with the name setupPort.
    at flash.external::ExtensionContext/_call()
    at flash.external::ExtensionContext/call()
    at com.quetwo.Arduino::ArduinoConnector/connect()[/Users/quetwo/Documents/Adobe Flash Builder 4.6/SerialANE-Lib-Mac/src/com/quetwo/Arduino/ArduinoConnector.as:71]

    Seems like it can’t find the methods in the osx bundle… Maybe some name_mangling ?
    or did I forget something?

    thnx for the good work so far …

    • quetwo December 2, 2011 at 11:31 am

      This is a known, reported bug with Flash Builder 4.6 for Mac. It doesn’t call adl (the debugger version of AIR) with the proper parameters to include the .ANE. You can either export a release version (in which case, it will work), or you can launch ADL from the command line (or ANT, which is what I ended up doing). It is a real pain, and threw me for at least two weeks while I was trying to build the MAC version. I have a blog post coming up (I still need to put together all of my notes) on exactly how to get around it until Adobe fixes the issue.

  3. Sanguinoide December 3, 2011 at 8:37 am


    Great Proyect!!

    I have the same problem as gepatto, but if i try to export a release, i get this error:

    Native extensions are being used in the project but are not supported by the AIR package type. Runtime issues can occur.

    Any idea?

    Thanks in advance.

    (sorry my english, im from spain)

    • quetwo December 3, 2011 at 9:17 am

      At this time, ANEs are ONLY supported with the “Signed Native Installer” release option. They will not work with AIRI, Captive Runtime or as a standard AIR package. I’ve been told they are working on allowing the Captive Runtime option for a future release of FB.

  4. Sanguinoide December 3, 2011 at 3:57 pm

    Thanks, it works!!

    I will try to launch ADL or ANT, releasing all time is not an option…

    Thanks again for your work, it makes very simple to develop arduino with as3 🙂

  5. Pingback: Arduino Blog » Blog Archive » Connecting your Arduino to AIR using an AIR Native Extension

  6. Pingback: Connecting your Arduino to AIR using an AIR Native Extension » Geko Geek

  7. jomfg2r0e December 5, 2011 at 11:29 am

    (Stupid question to come) Does it work in flash ?

    • quetwo December 5, 2011 at 2:51 pm

      ANEs do not work within the browser-based flash player at this time. That is due to the security sandbox restrictions that the Flash Player lives in. You can still use a proxy like serProxy for those applications.

  8. Pingback: Connecting your Arduino to AIR using an AIR Native Extension | dev.SquareCows.com

  9. Mark Hester December 6, 2011 at 9:56 am

    Great project.
    Any ideas if this will work on an Air app that’s output for an Android device?
    If so we could connect the arduino megaADK directly to the Android device and control inputs and outputs from a nice AS3 interface.

    • quetwo December 6, 2011 at 11:47 pm

      Unfortunately, at this time there really isn’t a standard interface out from most cell phones. Androids offer a “B” USB connection (device, not host), so unless we build a custom harness, there is not much we can do at this point. I hope that changes in the future…

      • Gautham April 26, 2013 at 11:33 am

        I can make the harness for this but when i used this ANE and tried to publish in Air for Android it said there is not implementation of this extension for the target platform

      • quetwo April 27, 2013 at 1:46 pm

        The ANE is only for Windows and Mac at the moment. I’ve got a working prototype for Android using the Electric Sheep, but Androids in general don’t have serial ports…

  10. Max December 8, 2011 at 6:13 am

    Just to let you know that this project is awesome. It works perfectly and is verry easy to set up.
    I still have to place the “pthreadgc2.dll” manually but everything else is just perfect!
    Keep on the good work,


  11. Alex January 16, 2012 at 8:06 am

    HI quetwo,

    really cool, i also wanted to do this.
    I have the following problem using your ANE. I am on Mac OSX 10.6.8 running the 64 bit kernel.
    Compilation is done with FlashBuilder 4.6.

    I can compile but after the installed app starts it crashes. I am not connecting to the Arduino but just tracing the available COM Ports to the MonsterDebugger. Inside MonsterDebugger I can see, that the ANE seems to work. I am getting a list of COM Ports. But then the app crashes.

    Do you have any idea what the cause might be?


    • quetwo January 21, 2012 at 6:12 pm

      The list of COM ports actually doesn’t come from the .framework, but rather it is created internally from within ActionScript. I did this on purpose, so you wouldn’t have to load the framework/dll before choosing the COM port. It sounds like the .framework may not be loading properly.

      If you load the app with adl from within terminal, do you see any error messages on the console?

      • Alex January 23, 2012 at 5:06 am


        thanks for your reply. I normally start adl via ant but I will try to start it via the terminal. Will keep you updated respectively ask again…


  12. Saar January 25, 2012 at 12:21 pm

    hey quetwo, great stuff, including your video from adobe MAX

    i am building a kiosk on air that needs to do 2 way talks with printers, barcode readers, and other peripherals (no Arduinos). could i use your library or is it hard-written for Arduino use only?


    • quetwo January 25, 2012 at 8:58 pm

      The library will work with any serial device. On the Mac, you may need to modify the function that gives you back the serial ports on the computer (that is specific to Arduinos), but the rest works fine. I know others are using the library to talk directly to GPSs and modems. The only thing to note is that I don’t pay attention to the CTS/RTS pins, so if your device depends on hardware flow-control, you may be out of luck without modifying it.

  13. mateuszmackowiak February 22, 2012 at 11:27 am

    Great work. I use it with my specyfic device that has nothing to to with Arduino and works great.
    But is there a way You could add support in the connection method to add the parameters that You hardcoded in the extension : “data=8 parity=N stop=1”.
    Whould be very grateful .

    • quetwo February 23, 2012 at 2:55 pm

      This particular extension is meant to target Arduino devices, which are hard-coded to 8/N/1 settings. You are more than welcome to download the source code (available on the google code site) and make the changes. The video series on this blog show how to compile them and make them work for your particular needs 🙂 The other thing I am ignoring with this project is flow control, which right now is hard-set to hardware, because I am depending on the UART in the Arduino.

  14. Reno Sioux February 26, 2012 at 10:45 pm

    Hey, thanks so much for putting this together.

    Would it be possible to package an example ant build setup? I’m a little lost right now. The export release works, but it is such a slow process, and I would like to be able to use the debugger.


  15. lluuccaa March 9, 2012 at 3:44 pm

    Hello, my name is Luke, I am writing from Italy for a problem I found with your library for Arduino.

    I’m trying to use the library for the Arduino and the library for Kinect, but there is a problem if you use two libraries ANE.
    http://forums.adobe.com/message/4026961 # 4026961

    I played the course on how to create a library dll, I managed to do the tutorial and rename the initialization methods and are able to operate the library for Arduino ANE with the library of your tutorial.

    I’m trying to import the library project, ANE, but I can not recompile it.

    you may rebuild the library, changing the name to the initialization methods?

    thank you

  16. lluuccaa March 11, 2012 at 10:55 am

    I managed to compile the DLL by renaming the initialization methods, I can now use the library for the arduino and kinect simultaneously

  17. Alfonso Florio March 30, 2012 at 10:10 am

    Hi! I am sorry to say that “The extension context does not have a method with the name setupPort” is happening also on my windows 7 x64 machine. I’d really love to use this extension. Any clues?

    ArgumentError: Error #3500: The extension context does not have a method with the name setupPort.
    at flash.external::ExtensionContext/_call()
    at flash.external::ExtensionContext/call()
    at com.quetwo.Arduino::ArduinoConnector/connect()[C:\Users\Nicholas Kwiatkowski\Adobe Flash Builder 4.6\SerialANELib\src\com\quetwo\Arduino\ArduinoConnector.as:83]

  18. chris April 3, 2012 at 1:45 am

    I, for some reason, can’t seem to get any of these to work with just a simple sketch on my arduino that has a Serial.println(“blah”); I had issues on a mac but switched to a PC. didn’t get the flex errors but now the socketData event isn’t being fired. checked all the ports. put the dll’s into my System32 folder.

    its been a while since i’ve done arduino sketches. maybe there is something missing. is there a bare bones arduino sketch and flex project using this ane that I can test out just to make sure i’m getting some kind of signal?

    • quetwo April 4, 2012 at 10:49 pm

      You may want to try loading the standard ‘firmata’ image onto the arduino and using the as3glue swc I posted on the google code site. You should be able then to 1. getFirmwareVersion, 2. set the pins, 3. set analog reporting (if needed) , then do what you want to do with the project.

      • chris April 5, 2012 at 1:11 am

        Okay, think I’ve got it with the Firmata! I have an older Duemilanove. Seems you have to hit the reset button on it after launching the app to get anything to trigger. But this could be ignorance on my part.

      • chris April 11, 2012 at 10:34 pm

        Seems like I’ve run into a road block. Seems the only version of firmata that will actually let me get data is SimpleAnalogFirmata. But it crashes after a few seconds. Can’t get any of the other ones in the examples that come with Arduino 1.0 to work. Is there a version you know of that will let me get Analog/Digital/IC2 data? If there is no real stable version, then is there an example project of ArduinoConnector code minus the as3glue? Thanks again for making an awesome library and all your help.

      • quetwo April 11, 2012 at 11:17 pm

        I’ve had mixed results with the new Arduino 1.0 IDE and associated firmata. I do most of my testing with Arduino Studio 0022 when I’m using my older Duemilanove. http://arduino.cc/en/Main/Software

      • chris April 12, 2012 at 12:32 am

        Thanks! But no good.with 0022 either. It is able to read the analog input of a Force Sensitive Resistor for about a second on pin 0 but then it dies. If you think this is a valid issue, I can post to your google code instead of blowing up your blog post 🙂

      • chris April 12, 2012 at 12:33 am

        Sorry, I meant the socketData stops firing the event listener. Just to clarify.

  19. lluuccaa May 4, 2012 at 1:38 am

    Hello, how can I use the library to connect two serial ports?
    I need to connect two Arduino.

  20. mcrobbsn May 21, 2012 at 5:40 am

    It seems the ane gets stuck on a response from my device longer than say 40 characters.

    I am on a win7 x64 machine, pthreadGC2.dll installed, isSupported == true, connection to device via “COM51” and 115200 works, sendByte() works from start, readAsString() works several times with responses of less than 40 characters. As soon as my device/virtualPort-Terminal sends a string > 40 characters (<< 4k buffer) everything freezes and the ane neither sends nor receives anything. Any clues?

  21. mcrobbsn May 29, 2012 at 8:01 am

    Don’t know if you got the last message, so I’m posting it again.

    I got your ane running on a win x64 system, everything works fine so far, sending, receiving, all perfect!

    The only issue I got is that the whole thing freezes at incoming responses of about 40 ascii characters length (far less than the limit of 4k byte of the buffer size). After I got one of these responses from the device I can neither receive nor send anything from my as3 application.

    Keep up the great work!
    Cheers Rob

  22. egorka August 7, 2012 at 5:36 am

    Great Project!
    But I got the same problem as chris
    >>Sorry, I meant the socketData stops firing the event listener….
    windows XP, dll in System32

  23. Vlad October 15, 2012 at 7:46 am

    quetwo, thanks for your extension. But, is the usb-bridge from Android device to another usb-device still unavailable? Is there opportunity to recieve and send data from android device to another devices, connected to mobile via usb?

  24. Pablo Cabana May 21, 2013 at 6:35 pm

    Ok, I lost my afternoon here but finally got it.
    I was getting the “The extension context does not have a method with the name setupPort” bug and was unable to use the solutions provided.

    What solved to me:

    1 – Arrange the project as indicated normally.

    2 – DUPLICATE your ArduinoConnector.ane, create a folder “uncompressedANE” and put your duplicated ANE inside it.

    3 – Decompress your duplicated ANE (inside the uncompressedANE folder) – Just rename it to ArduinoConnector.zip (or .rar) and decompress.

    4 – The decompressed itens should be put in a folder called “ArduinoConnector.ane” (exactly, the same name as the compressed extension).

    5 – Open the terminal and navigate to the folder where you can find the adl compiler.
    On my mac was just a matter of typing: “cd /Applications/Adobe\ Flash\ Builder\ 4.6/sdks/4.6/bin”

    6 – Run the adl command:
    “./adl -profile extendedDesktop -extdir /path/to/your/folder/uncompressedANE /pat/to/your/air/xml/config/file/FlashArduino1-app.xml”

    7 – Have fun!

  25. Pablo Cabana May 22, 2013 at 1:02 pm

    I am using the “AS3-Arduino-Connector” library to talk to a Arduino UNO board using Flash. It works perfectly.

    However, I need to use this Arduino to dispatch serial signals through an Xbee radio. It is well known that the Xbee shield uses the UART, so it is impossible to send any data through USB to a board using it. It is not possible to upload sketches either.

    So, how can I send data to the Arduino UNO using the connector while it is using the Xbee shield?

    Or better, is it possible to talk directly to an Xbee radio?

    • quetwo May 23, 2013 at 7:46 am

      You might be better off talking directly to the Xbee. While the ANE works great for serial mode, you really need a program like AVRdude or the Arduino Studio in order to upload the a new firmware, bootloader or sketch. It involved a lot of DTE/DCE switching that, while possible is pretty complicated to do with this ANE.

      • Pablo Cabana May 23, 2013 at 12:24 pm

        I got it! 🙂
        In fact it was way simpler. I used the Xbee explorer cable to connect the Xbee (without any arduinos) to my computer and connected my Flash app to it wtih your ANE. The Xbee explorer makes the Xbee behaves like an Arduino, so the ANE finds it automagically.
        Now my computer works like a wireless Radio broadcaster! Awesome! \o/
        Thanks a lot.

  26. olli June 11, 2013 at 6:58 am

    is there a way to use this extension with an arduino connected via wifi? .. or, if not, do you have plans to extend it so that it would be possible to have acceess to an arduino connected via wifi?

    • quetwo June 22, 2013 at 4:03 pm

      The best option of controlling an Arduino via WiFi would most likely be to skip the TCP/IP -> Serial conversion and just open up TCP sockets directly to the unit. Shouldn’t need this addin — Flex/AIR/Flash have TCP/IP networking controls built in.

  27. gepatto September 11, 2013 at 10:57 am

    I had trouble with version 1.5 of the ane on Win7-32bit with Flashbuilder 4.7 giving me the setupPort Error.
    ( version 1.2 was working.) After some deep digging I found that it couldn’t find
    libgcc_s_dw2-1.dll . I found a version of that dll via stackoverflow and copied it to windows/system32 now It seems to work just fine..

  28. M Ramon López Torres June 1, 2014 at 6:12 pm

    Has anyone used this ANE compiling with apache flex from the command line?

    I get compile my project, but when I run it with ADL I get this error:

    VerifyError: Error # 1014: Class com.quetwo.Arduino ArduinoConnector :: Could not be found.

    at flash.events :: EventDispatcher / dispatchEventFunction ()
    at flash.events :: EventDispatcher / dispatchEvent ()
    at mx.core :: UIComponent / dispatchEvent ()
    at spark.components.supportClasses :: ButtonBase / dispatchButtonEvents ()
    at spark.components.supportClasses :: ButtonBase / commitProperties ()
    at mx.core :: UIComponent / validateProperties ()
    at mx.managers :: LayoutManager / validateProperties ()
    at mx.managers :: LayoutManager / doPhasedInstantiation ()
    at mx.managers :: LayoutManager / doPhasedInstantiationCallback ()

  29. Dave August 1, 2014 at 8:01 am

    Hi, thanks for this! Been using serproxy and this is great to not need another piece of software. I have it working fine, but my problem is serial port consistency. Is the com port stored on the Arduino? I plug in one Arduino and in Device Manager it shows com 9. plug in another and it shows com 15. Is there any way to be consistent? I have a kiosk that will be running on eight machines and am hoping to be able to use the same com port… Or at least know which port arduino is on when enumerating with getComPorts(). Is there any way? Or will I need an xml file or something to specify the port? Any help is really appreciated.

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s

%d bloggers like this: