Using RFID in an ActionScript Application
December 9th, 2008 • AIR, ActionScript, Flash, Physical Computing
http://www.vimeo.com/2473196In order to receive tags, the RFID reader has to be activated through a click on the image. After that, the different RFID tags (sorry for my tiny tags) are used to create different forms.
The previous post was all about wiring the RFID reader ID-12 and an Arduino board. Now I want to show you how to use this setup within an ActionScript application.
As you don’t have the possibility to access the hardware respectively the usb/bluetooth serial port directly, you have to use a Socket server which redirects the network socket connections to or from the serial ports. Fortunately there are already ready-to-use solutions like serproxy or TinkerProxy (win, osx) available. Personally I prefer serproxy, but if you want to have a nice GUI I would recommend TinkerProxy.
These proxy servers are not made specifically for the use with Flash and therefore they don’t implement a cross-domain-policy file. As you probably know this is needed for not getting a security error in your flash application. As a workaround for that problem you can get lessrains policyserver and start it on port 5334 (java -jar policyserver.jar 5334). It will return an appropriate policy file when requested by the flash application.
With these things in mind we can go for some code now.
... public function connect() : void { if(_state != DISCONNECTED) return; _state = CONNECTING; addSocketEvents(); // load policy file from localhost, port 5334 Security.loadPolicyFile("xmlsocket://" + SOCKET_HOST + ":" + String(POLICY_PORT)); // connect to proxy localhost, port 5333 _socket.connect(SOCKET_HOST, PROXY_PORT); } public function disconnect() : void { if(_state != CONNECTED) return; _state = DISCONNECTING; sendDisconnect(); processDisconnect(); } public function finalize() : void { ... } ...
My RFID class implements basically 3 public methods + the EventDispatcher stuff.
... private function connectSocket(event : Event) : void { // send ping to arduino _pingTimer = new Timer(100, 0); _pingTimer.addEventListener(TimerEvent.TIMER, handleTimer); _pingTimer.start(); } private function handleTimer(event : TimerEvent) : void { sendPing(); } ...
The Socket class dispatches Event.CONNECT when the connection to the proxy server is established but that doesn’t mean that the hardware is connected as well. To really know when the arduino is listening I use a timer to send a ping to the Arduino board. When I get the ping back, I know that the Arduino is ready to use.
... private function processRfidTag() : void { _cachePosition = _cache.position - 1; var bytesIn : int = 0; var val : int; var rfidTag : String = ""; while (_cache.bytesAvailable > 0 && bytesIn < 10) { val = _cache.readByte(); // if ETX or STX before the 10 digit reading -> stop reading if((val == STX) || (val == ETX)) break; // Do Ascii/Hex conversion: if ((val >= 0x30) && (val < = 0x39)) val -= 0x30; else if ((val >= 0x41) && (val < = 0x46)) val = 10 + val - 0x41; rfidTag += val.toString(16).toUpperCase(); // ready to read next digit bytesIn++; } // read complete if (bytesIn == 10) { if(rfidTag != "0000000000" ) _tag = rfidTag; else _tag = null; _cachePosition = -1; dispatchEvent(new RFIDEvent(RFIDEvent.TAG_CHANGED)); } } ...
When I receive some data from the socket, I look for the STX byte which is the header or marker of the RFID tag and get the tag via processRfidTag(). Beyond that I don’t read the bytes directly from the socket but write all the incoming data in a cache first to not loose any information coming in in different ProgressEvent.SOCKET_DATA events.
Download the full source of my ID-12 demo and don't forget to replace my tag ids with yours otherwise your RFID tags won't draw any form.
11 Responses (Add Your Comment)
-
-
Thanks for getting back to me Martin. I persevered some more last night and managed to get it so that the square went from grey to black, but then no shapes appeared. I entered my RFID tag ID numbers in the TestRFID.as file overwriting your ones, but no luck. In the nest of files and folders in your download I can’t open the .project file or the .as3_classpath What program were these file created in? – I have managed to get my RFID reader talking to my Arduino and that talking to Flash in a simpler way. What you have created looks really good, but might be a bit more than I need. I would still like to understand it though. My setup is Windows XP and Arduino Diecimila. Thanks again for your help.
-
I added
trace (“this is the value of the byte coming in” + val); to line 290 in the RFID.as file but if anything is tracing I can’t see where. Am I doing something really daft? -
hahawahaha March 15, 2010at 6:57 pm
hie there …
im doing a rfid application and will be used with action script. the thing is, i won’t be using the RFID reader ID-12 and the Arduino board. i am using a different rfid reader. could this work with the guidance u gave above?what does that arduino board do anyway?
thanks
-
hahawahaha March 16, 2010at 11:19 am
ohhh … but i won’t be using the ID-12 reader … im using this locally made rfid reader that outputs its data onto window’s hyperterminal … any idea how i can relate them?
the ID-12 uses hyperterminal to output its data too?
-
hahawahaha March 17, 2010at 11:20 am
hi martin,
i think i got the click of it on how your thing works. but 1 question, how do i check whether the things from my rfid are actually going into the port?
im using the tinkerproxy program. the program sends the serial comm port data to this 5331 port. but how do i check them? are they really going into the port?
thanks
Hi Martin,
I’m struggling a bit to get this working. I feel close but think maybe I have things filed in the wrong places. I’m a bit new to all this. I have set up the Arduino OK and can read the tags in Arduino’s serial window. If I then shut that window, run the serproxy and the policyserver.jar and then double click the RFIDTestMain.swf (after having entered my RFID tag ID numbers in the TestRFID.as file) and click on the grey square to get it to black…..nothing happens! Please can you help me by explaining what I’m doing wrong? or can you please guide me through the process?
Many thanks,
Joe