QueTwo's Blog

thouoghts on telecommunications, programming, education and technology

Tag Archives: DLL

Creating a Windows AIR Native Extension with Eclipse – Part 4

In this final of my 4-part video series, I show you how to import and use the ANE that we created in the last three videos.  We will be using Adobe Flash Builder 4.6 to import the ANE, and we will build a very quick sample application that will use the getTestString and getHelloWorld functions that we wrote in our native DLL written in C.

If you want a copy of all the final projects, you can download them here.  The ZIP file includes the CDT project, the compiled DLL, the ActionScript project, the compiled ANE and the project created in this fourth video.  Enjoy!

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!

Creating a Windows AIR Native Extension with Eclipse – Part 3

In part 3 of this video series, I talk about how to write the AS3 portion of the AIR Native Extension, and how to package all the components into your final .ANE file. 

You can watch part one here, and part two here.  Part four will talk about how to use the .ANE within Flash Builder.  I will post all the files used in this tutorial in the final blog post.

Creating a Windows AIR Native Extension with Eclipse – Part 2

In part two of this video series, I go through how to actually program your ANE Windows DLL.  This involves doing some C programming.  Please see part 1 here.

The snippets mentioned in this video are available here :  ANE Snippets Download     You can use these to jump-start your development.

Creating a Windows AIR Native Extension with Eclipse – Part 1

The second I heard about Adobe giving us the ability to create our own extensions to the Flash Platform in AIR 3.0, I was smitten.  It was finally a way that we could add our own features and do the things that were high priorities on our lists, but not on Adobe’s.  I knew I was looking for features that were one-offs (how many people today really need access to the COM ports), but they were forcing me to do all sorts of weird workarounds like launching proxy applications to do seemingly simple tasks.

AIR 3.0 got released a few weeks ago and I’ve jumped in head first into creating some ANEs (AIR Native Extensions).  For those of you who don’t know, ANEs are packaged extensions that contain operating-system specific code (DLLs for Windows, Libraries for MacOS, Java classes for Android and Objective-C for iOS), that allow you to do things that the Flash Player wasn’t able to do. 

Unfortunately, Adobe assumed that if you were developing DLLs for Windows, you were going to be using Visual Studio and nothing more.  This didn’t make a whole lot of sense in my mind as they’ve been leveraging Eclipse for all of their tooling, and Eclipse does offer some great C/C++ addins.  Now, that being said, Visual Studio is by far the more feature-full and hands-down the best editor for enabling these kinds of workflows on Windows.  It is, however, very costly and even though Microsoft offers a free versions, it takes over your computer by installing debug versions of most of Microsoft’s shared libraries making your computer slower and more crash prone.

I wanted to use Eclipse’s CDT addin with the standard GCC tooling that is available on pretty much every operating system.  By using GCC, I was able to make very portable code that with minimal effort was able to compile on all three of the major OSs (Windows, Mac, Linux). Adobe’s documentation was little help in getting this going (even if you were coding in Visual Studio, there is very little guidance on how to get things setup).  I do have to note that with my setup there is one distinct disadvantage — the lack of ability to debug the DLL when it is launched from AIR.  You will have to write your own C/C++ harness to do testing on your code in order to test it.  If you use the Visual Studio tooling, you CAN debug any DLL while it is running (this is why Microsoft replaces the shared libraries on your system to allow that debugging).

I’ve created a four part video series documenting how to get going creating ANEs.  Part 1 covers setting up your environment, including installing CDT, the compiler, and getting Eclipse setup to do your programming.  Part 2 covers actually coding the C/C++ code for your Windows DLL.  Part 3 covers creating your ANE, and packing up all the stuff needed to make it work.  And Part 4 covers how to use your new ANE in an AIR project.

Follow

Get every new post delivered to your Inbox.

Join 27 other followers