Project:RFID

From SpinetiX Support Wiki

Jump to: navigation, search

Description

This sample project demonstrates how an RFID reader (with keyboard emulation) can be used to interact with the HMP in order to display certain information on the screen based on the data sent by the reader. We've used an IdTronic EVO HF Reader (which sends 16 characters followed by an "Enter" for each tag), but any other compatible RFID reader should work as well (though you might need to change the JavaScript code).

The project contains two files:

  • index.svg
    Reads the data sent by the RFID device and updates a Shared variable with the tag code (which can also be displayed on the screen).
    Note that you might need to change the "Tag code length" property, if your device sends tag codes of different length.
  • Action to Switch.svg
    This is the "Action to Switch" widget, simply configured to use some test values for the Shared Variable.

Sample code

The following JavaScript code is included into the index.svg file to interpret the quick succession of keystroke commands emulated by the RFID device.

var buffer = '';

$(function () {
    var txt = $('#jsonLayers').text();
    if ( $.trim(txt) != "" ) {
        $('#layers').add( $.uncan($.parseJSON(txt)) );
    }
    
    var config = $.parseJSON( $('#config').text() );    // load the configured properties
    var sv = createSharedVariable( config.sv_name );    // create the shared variable
    
    if ( config.show_tag ) {
        var textArea1 = $.textArea({
            "displayAlign" : "before",
            "fill" : "#000",
            "fontFamily" : "Arial",
            "fontSize" : "20",
            "height" : 190,
            "id" : "t",
            "left" : 0,
            "textAlign" : "start",
            "top" : 0,
            "width" : 1280
        }).text('Waiting for RFID tags ...').addTo('svg');
    }
    
    $('svg').textInput( function( evt ) {
        buffer += evt.data;
        if( buffer.length >= config.max_len * 5 ) {
            alert('Error: The reader is sending a wrong signal. Discarding existing data.');
            buffer = '';
        }
    }).keydown( function( evt ) {
        if( evt.keyIdentifier == 'Enter' ){
            if ( config.show_tag ) {
                textArea1.text( buffer );
            }
            if ( buffer.length >= config.max_len ){
                sv.set( buffer.slice( -config.max_len ) );
                buffer = '';
            } else {
                alert('buffer size: ' + buffer.length);
            }
        }
    });
    
});
This page was last modified on 3 May 2018, at 14:23.