0% found this document useful (0 votes)
47 views3 pages

Java Code for RFID Tag Reading

This document provides sample Java code to read RFID tags connected via a serial port. The code opens the COM1 serial port, writes data to initiate a tag read, and implements an event listener to read the tag response. When a tag is detected, the code analyzes the response bytes to determine the tag type and prints the hexadecimal tag ID. This allows the program to read RFID tags connected via a serial port and extract the relevant identification information.

Uploaded by

Zubair Malik
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views3 pages

Java Code for RFID Tag Reading

This document provides sample Java code to read RFID tags connected via a serial port. The code opens the COM1 serial port, writes data to initiate a tag read, and implements an event listener to read the tag response. When a tag is detected, the code analyzes the response bytes to determine the tag type and prints the hexadecimal tag ID. This allows the program to read RFID tags connected via a serial port and extract the relevant identification information.

Uploaded by

Zubair Malik
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Java & RFID Tags by Shamshad Ansari Example 1: serialPort = (SerialPort) portId.

open("SimpleReadApp", 2000) opens COM1 serial p ort identified in the following code segment while ([Link]()) { portId = (CommPortIdentifier) [Link](); if ([Link]() == CommPortIdentifier.PORT_SERIAL) { if ([Link]().equals("COM1")) { [Link]("The port is: " + [Link]()); RFIDTagRead reader = new RFIDTagRead(); } } }

Listing One /* Sample java code to read an RFID tag */ import [Link].*; import [Link].*; import [Link].*; public class RFIDTagRead implements Runnable, SerialPortEventListener { static CommPortIdentifier portId; static Enumeration portList; InputStream inputStream; SerialPort serialPort; Thread readThread; //Array consisting of SOH, length, command, data, BCC static byte[] bytearray = {0x01, 0x02, 0x09, 0x32, 0x39}; static OutputStream outputStream; static int n =0; public static void main(String[] args) { //Enumerate a list of available ports portList = [Link](); // Identify the ports. I connected the reader with COM1 while ([Link]()) { portId = (CommPortIdentifier) [Link](); if ([Link]() == CommPortIdentifier.PORT_SERIAL) { if ([Link]().equals("COM1")) { [Link]("The port is: " + [Link]()); RFIDTagRead reader = new RFIDTagRead(); } } } } public RFIDTagRead() { try { //Open the COM1 port and name it MicroReader with timeout 2000ms

serialPort = (SerialPort) [Link]("SimpleReadApp", 2000); } catch (Exception e) {[Link]("Port Error");} try { outputStream = [Link](); // Write the stream of data conforming to PC to reader protocol [Link](bytearray); [Link](); [Link]("The following bytes are being written"); for(int i=0; i<[Link]; i++) [Link](bytearray[i]); [Link]("Tag will be read when its in the field of th e reader"); } catch (IOException e) {} // Set Serial Port parameter try { [Link](9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); } catch (UnsupportedCommOperationException e) {} try { //Register an event listener object to the port [Link](this); } catch (TooManyListenersException e) {[Link]("Too Many L isteners"); } //Specify an event type. On data availability, triggers serialEvent meth od [Link](true); try { //Associate an InputStream object with this port. inputStream = [Link](); } catch (IOException e) {} //Start a thread to handle the time-to-read the tag readThread = new Thread(this); [Link](); } public void run() { try { [Link](56); } catch (InterruptedException e) {} } //This method is called by notifyOnDataAvailabe() public void serialEvent(SerialPortEvent event) { switch([Link]()) { case [Link]: case [Link]: case [Link]: case [Link]: case [Link]: case [Link]: case [Link]: case [Link]: case SerialPortEvent.OUTPUT_BUFFER_EMPTY: break; case SerialPortEvent.DATA_AVAILABLE: n++; //to count the number of readings [Link]("The reading description of RFID Tag" + " " + n); //array size must not be less than the number of bytes to be read

byte[] readBuffer = new byte[20]; // to store the read data int numbyte = 0; try { while([Link]() >0) { // Read the RFID data and store in the byte array numbyte = [Link](readBuffer); [Link]("Number of Bytes read: " + numbyte); } } catch (IOException e) {} if( readBuffer[0] == 1) /*check if start bit is detected */ { int length = readBuffer[1]; // Identify the Transponder type switch(readBuffer[2]) { case 12 : { [Link]("RFID is RO:" + "\t"); break; } case 13 : { [Link]("RFID is R/W:" + "\t"); break; } case 14: { [Link]("RFID is MPT/SAMPT:" + "\t"); break; } case 15: { [Link]("RFID is Other:" + "\t"); break; } } // Write the actual tag reading in Hexadecimal for( int m = length+1; m > 2; m--) [Link]([Link](readBuffer[m] & 255)); } [Link](" "); [Link]("\t" + "Read Sucessful"); [Link]("----------------------------------"); break; } } }

You might also like