Monday, November 9, 2009

Reading RFID Tags with TWedge


TEC-IT's data acquisition software TWedge allows an easy implementation of RFID controller protocols. TWedge can be adjusted to communicate with RFID controllers in a bi-directional way. Reading and processing tag identifiers is as easy as reading/writing tag data!

The following TWedge script can be used to read RFID Tag IDs from EMS Datalogic RFID Controllers (utilizing the ABx Fast Protocol).

The following script performs the following functions:
1) Query the RFID controller by pressing the hotkey

2) When data is received test it for a valid response

3) Display data in MessageBox

Try it yourself - Download the software wedge for free! Keep in mind: V2.3 or higher is required!

Global Helper Functions

HEX Conversion

function encodeHex(input) {
hexOutput = "";
for (i=0; i < input.length; i++) {
hexOutput = hexOutput + ' ' + Dec2Hex(input.charCodeAt(i));
}
return hexOutput;
}

function encodeDec(input) {
decOutput = "";
for (i=0; i < input.length; i++) {
decOutput = decOutput + ' ' + input.charCodeAt(i);
}
return decOutput;
}

function Dec2Hex(Decimal) {
var hexChars = "0123456789ABCDEF";
var a = Decimal % 16;
var b = (Decimal - a)/16;
hex = "" + hexChars.charAt(b) + hexChars.charAt(a);
return hex;
}

Test for TAG ID

function IsRFIDTagID (data)
{
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13
// 02 02 00 09 0F E0 04 01 00 35 7F 5E 19 03

return data.length >= 14
&& data.charCodeAt(0) == 2
&& data.charCodeAt(1) == 2
&& data.charCodeAt(4) == 15;
}

Test for TAG Response

function IsRFIDReadResponse(data)
{
// 0 1 2 3 4 5 6 7 8 9 10
// 02 02 00 06 05 31 32 33 34 35 03

return data.length >= 6
&& data.charCodeAt(0) == 2
&& data.charCodeAt(1) == 2
&& data.charCodeAt(4) == 5;
}

Send RFID READ Request

var escZero = "\\0";
var escBack = "\\\\\\\\";

function SendRFIDReadRequest ()
{
var request;
// read 5 Bytes from address 1 with timeout 2000 msec
// 02 02 00 07 05 00 01 00 05 07 D0 03

//request = String.fromCharCode ( 2, 2, 0, 7, 5, 0, 1, 0, 5, 7, 208, 3 );
request = "\x02\x02" + escZero + "\x07"
+ "\x05" + escZero + "\x01" + escZero + "\x05"
+ "\x07\xD0\x03";

// MessageBox ('WriteToDevice: ' + request);
WriteToDevice (request, 2000);

}

Get RFID Response Data

function GetRFIDDataFromResponse ( data )
{
// 0 1 2 3 4 5 6 7 8 9
// 02 02 00 06 05 31 32 33 34 35 03
var len;
var out = "";
if (data.length >= 5)
{
len = data.charCodeAt(2) * 256 + data.charCodeAt(3) - 1;
for (i=0; i< len; i++)
{
out = out + String.fromCharCode(data.charCodeAt(5 + i));
}

//MessageBox ('Len: ' + len + ', ' + out);
return out;
}
return null;
}

OnData Script

if ( IsRFIDTagID (DATA) )
{
SendRFIDReadRequest ();
}
if ( IsRFIDReadResponse (DATA) )
{
MessageBox (encodeHex(DATA) + ' (' + DATA.length + ' Bytes)');
MessageBox ('Nutzdaten: ' + GetRFIDDataFromResponse (DATA));
}

HotKey Script

SendRFIDReadRequest ();