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!
Like this:
Like Loading...
Seems to be a nice piece of work! Will be checking it out as soon as i can
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 …
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.
Thnx ! works now .. excellent!
I got the same problem with Flash Builder 4.6 on windows…
Thanks for your video tutorials on ANE, they are really clear and I understood everything!
It starts working with a release AIR package setup… Then I had a message: pthreadGC2.dll was missing, so I copied it to %windir%\syswow64\ , now everything is ok, even in debug mode!
Hi,
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)
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.
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
Pingback: Arduino Blog » Blog Archive » Connecting your Arduino to AIR using an AIR Native Extension
Pingback: Connecting your Arduino to AIR using an AIR Native Extension » Geko Geek
(Stupid question to come) Does it work in flash ?
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.
Pingback: Connecting your Arduino to AIR using an AIR Native Extension | dev.SquareCows.com
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.
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…
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
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…
Hello,
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,
Cheers
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?
Thanks
Alex
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?
Hi,
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…
Regards
Alex
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?
thanx
Saar
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.
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 .
Thanks
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.
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.
Thanks!
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
I managed to compile the DLL by renaming the initialization methods, I can now use the library for the arduino and kinect simultaneously
I have it on my todo list for the 1.2 release. Hoping to have that up in the next few weeks.
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]
Alfonso,
This is a known issue in Flash Builder 4.6. It is most prevalent on the Mac, but I sometimes hear of it on the PC. It usually means that the DLL didn’t actually get loaded. Check out the FAQ on the google code site : http://code.google.com/p/as3-arduino-connector/wiki/FAQ
Hey Quetwo,
Any chance you can show us your example ANT build file ? I’ve had a look around and haven’t been able to find a ANT script that does ANE for OSX
Cheers
I just had one ANT file contributed to this project I will be posting on the Google Code site as soon as I get a chance.
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?
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.
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.
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.
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
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
Sorry, I meant the socketData stops firing the event listener. Just to clarify.
Hello, how can I use the library to connect two serial ports?
I need to connect two Arduino.
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?
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
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
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?