RFID Processing Examples

From On-signal: Projects & Research

Jump to: navigation, search

This page lists the examples for the RFID Processing library.

These examples are meant to run from within the Processing environment. You can copy-paste them into a new sketch, after having installed the RFID library as explained in the installation instructions.

Example 1: Testing the reader

This sample shows how to setup the RFID reader and handling the different errors that can occur while opening the reader ports. Once this sketch is running, the reader prints messages when tags are added/removed. (because we used setVerbose(true)).

import org.onsignal.rfid.*; 
import gnu.io.*; // for the exceptions

RFIDReader reader;
String portName = "COM3";

void setup() {
    try {
	reader = new RFIDReader(portName);
        reader.setVerbose(true);
    } catch (NoSuchPortException e) {
	println("Port "+portName+" was not found!");
    } catch (PortInUseException e) {
	println("Port "+portName+" is in use by another program.");
    }
}

Example 2: Doing stuff with the tags

This example shows how to do things at the moment a tag is read and at the moment one is removed. In the RFIDReader constructor, we pass in the port name and also an instance of our 'taglistener' class. This class contains functions which will be called when a tag is added/removed. This is where you would implement your program logic.

import org.onsignal.rfid.*; 
import gnu.io.*; // for the exceptions

RFIDReader r;
String portName = "COM3";

class TagListener implements RFIDListener
{
    void tagAdded(RFIDTagEvent e) {
	println("Tag " + e.getTag() + " was added to reader " + e.getReader());
    }

    void tagRemoved(RFIDTagEvent e) {
	println("Tag " + e.getTag() + " was removed from reader " + e.getReader());
    }
}


void setup() {
    try {

	r = new RFIDReader(portName, new TagListener());

    } catch (NoSuchPortException e) {
	println("Port "+portName+" was not found!");
    } catch (PortInUseException e) {
	println("Port "+portName+" is in use by another program.");
    }
}

Example 3: Getting all available tags

This enhances example 2 in that for every tag event, it will also print a list of current tags that are on the reader.

import org.onsignal.rfid.*; 
import gnu.io.*; // for the exceptions

RFIDReader r;
String portName = "/dev/ttyUSB0";

class TagListener implements RFIDListener
{
    void tagAdded(RFIDTagEvent e) {
	println("Tag " + e.getTag() + " was added to reader " + e.getReader());
        printCurrent();
    }

    void tagRemoved(RFIDTagEvent e) {
	println("Tag " + e.getTag() + " was removed from reader " + e.getReader());
        printCurrent();
    }
    
    void printCurrent() {
      println("Current tags:");
      Iterator it = r.getCurrentTags().iterator();
      while (it.hasNext()) {
        println((RFIDTag)it.next());
      }
    }
    
}


void setup() {
    try {

	r = new RFIDReader(portName, new TagListener());

    } catch (NoSuchPortException e) {
	println("Port "+portName+" was not found!");
    } catch (PortInUseException e) {
	println("Port "+portName+" is in use by another program.");
    }
}
Personal tools