Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package iotdevice;
- import com.pi4j.io.i2c.I2CBus;
- import com.pi4j.io.i2c.I2CDevice;
- import com.pi4j.io.i2c.I2CFactory;
- import java.io.ByteArrayInputStream;
- import java.io.DataInputStream;
- //import com.pi4j.io.i2c.
- import java.io.IOException;
- /**
- *
- * @author Matteo
- */
- public class Sensor {
- //Raspberry Pi's I2C bus
- private static final int i2cBus = 1;
- // Device address
- // private static final int address = 0x50;
- // RFID read address
- private static final int rfidAddrRead = 0xA1;
- private static final int rfidAddrWrite = 0xA0;
- // Read RFID command
- private static final byte getRfidCmd = (byte) 0x01;
- private static final byte firmwareRfidCmd = (byte) 0xF0;
- //I2C bus
- I2CBus bus;
- // Device object
- private I2CDevice sl030Read;
- private I2CDevice sl030Write;
- private DataInputStream sl030CalIn;
- private DataInputStream sl030In;
- public Sensor() {
- try {
- bus = I2CFactory.getInstance(I2CBus.BUS_1);
- System.out.println("Connected to bus OK!!!");
- //get device itself
- sl030Read = bus.getDevice(rfidAddrRead);
- sl030Write = bus.getDevice(rfidAddrWrite);
- System.out.println("Connected to device OK!!!");
- //Small delay before starting
- // Thread.sleep(500);
- } catch (IOException e) {
- System.out.println("Exception: " + e.getMessage());
- }
- }
- public String readRfid() throws IOException {
- byte[] bytesTemp = new byte[4];
- byte[] towrite= new byte[2];
- String result="EMPTY";
- try {
- towrite[0]=(byte)0x01;
- towrite[1]=firmwareRfidCmd;
- sl030Write.write(towrite,0,2);
- Thread.sleep(1000);
- // sl030.r
- int readTotal = sl030Read.read(bytesTemp, 0, 4);
- System.out.println("Letti: " +readTotal);
- // if (readTotal < 2) {
- // System.out.format("Error: %n bytes read/n", readTotal);
- // return "EMPTY";
- // }
- // //sl030In = new DataInputStream(new ByteArrayInputStream(bytesTemp));
- // //UT = sl030In.readUnsignedShort();
- result = new String(bytesTemp);
- } catch (IOException e) {
- System.out.println("Error: " + e.getMessage());
- } catch (InterruptedException e) {
- System.out.println("Interrupted Exception: " + e.getMessage());
- }
- // System.out.println("Id RFID: " + result);
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment