Advertisement
savageautomate

Pi4J - SL030 RFID Reader

Aug 12th, 2015
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package iotdevice;
  7.  
  8. import com.pi4j.io.i2c.I2CBus;
  9. import com.pi4j.io.i2c.I2CDevice;
  10. import com.pi4j.io.i2c.I2CFactory;
  11. import java.io.ByteArrayInputStream;
  12. import java.io.DataInputStream;
  13. //import com.pi4j.io.i2c.
  14. import java.io.IOException;
  15.  
  16. /**
  17.  *
  18.  * @author Matteo
  19.  */
  20. public class Sensor {
  21.    
  22.     //Raspberry Pi's I2C bus
  23.     private static final int i2cBus = 1;
  24.     // Device address
  25.     // private static final int address = 0x50;  
  26.  
  27.     // RFID addresses
  28.     private static final int DEVICE_ADDRESS = 0x50;
  29.  
  30.     // Read RFID command
  31.     private static final byte getRfidCmd = (byte) 0x01;
  32.     private static final byte firmwareRfidCmd = (byte) 0xF0;
  33.  
  34.     //I2C bus
  35.     I2CBus bus;
  36.  
  37.     // Device object
  38.     private I2CDevice sl030;
  39.     private DataInputStream sl030CalIn;
  40.     private DataInputStream sl030In;
  41.    
  42.     public Sensor() {
  43.        
  44.         try {
  45.             bus = I2CFactory.getInstance(I2CBus.BUS_1);
  46.             System.out.println("Connected to bus OK!!!");
  47.  
  48.             //get device itself
  49.             sl030 = bus.getDevice(DEVICE_ADDRESS);
  50.  
  51.             System.out.println("Connected to device OK!!!");
  52.             //Small delay before starting
  53.            // Thread.sleep(500);
  54.                  } catch (IOException e) {
  55.             System.out.println("Exception: " + e.getMessage());
  56.                  }
  57.     }
  58.    
  59.     public String readRfid() throws IOException {
  60.        
  61.         byte[] writeBuffer= new byte[2];
  62.         String result="EMPTY";
  63.         try {
  64.             // SEND COMMAND TO DEVICE TO REQUEST FIRMWARE VERSION
  65.             writeBuffer[0]=(byte)0x01;           // LENDTH (CMD+DATA)
  66.             writeBuffer[1]=firmwareRfidCmd;      // COMMAND
  67.             sl030.write(writeBuffer, 0, 2);
  68.  
  69.             Thread.sleep(1000);
  70.  
  71.             // READ DEVICE FIRMWARE RESPONSE
  72.  
  73.             // first just read in a single byte that represents the command+data payload
  74.             int length = sl030.read();
  75.  
  76.             System.out.println("TOTAL BYTES AVAILABLE: " + length);
  77.    
  78.             // if there are no remaining bytes (length == 0), then we can exit the function
  79.             if(length <= 0) {
  80.                 System.out.format("Error: %n bytes read for LENGTH/n", length);
  81.                 return "EMPTY";
  82.             }
  83.  
  84.             // if there is any length of remaining bytes, then lets read them now
  85.             byte[] readBuffer = new byte[length];    
  86.             int readTotal = sl030.read(readBuffer, 0, length);
  87.  
  88.             // validate to ensure we got back at least the command and status bytes
  89.             if (readTotal < 2) {
  90.                 System.out.format("Error: %n bytes read/n", readTotal);
  91.                 return "EMPTY";
  92.             }
  93.  
  94.             byte command = java.lang.Byte.toUnsignedInt(readBuffer[0]);  // COMMAND BYTE
  95.             byte status  = java.lang.Byte.toUnsignedInt(readBuffer[1]);  // STATUS BYTES
  96.  
  97.             // now we need to get the payload data (if there is any?)
  98.             if(readTotal > 2){
  99.               // we skip the first two bytes (command + status)
  100.               result = new String(readBuffer, 2, readTotal - 2);
  101.             }
  102.  
  103.             // what did we get?
  104.             System.out.println("-- LENGTH BYTE READ  : " + length);
  105.             System.out.println("-- COMMAND BYTE READ : " + command);
  106.             System.out.println("-- STATUS BYTE READ  : " + status);
  107.             System.out.println("-- DATA BYTES READ   : " + result);
  108.  
  109.  
  110. //            //sl030In = new DataInputStream(new ByteArrayInputStream(bytesTemp));
  111. //            //UT = sl030In.readUnsignedShort();
  112.  
  113.            
  114.         } catch (IOException e) {
  115.             System.out.println("Error: " + e.getMessage());
  116.         } catch (InterruptedException e) {
  117.             System.out.println("Interrupted Exception: " + e.getMessage());
  118.         }
  119.      //   System.out.println("Id RFID: " + result);
  120.         return result;
  121.     }
  122.    
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement