SHOW:
|
|
- or go back to the newest paste.
| 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 | - | // RFID read address |
| 26 | + | |
| 27 | - | private static final int rfidAddrRead = 0xA1; |
| 27 | + | // RFID addresses |
| 28 | - | private static final int rfidAddrWrite = 0xA0; |
| 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 | - | private I2CDevice sl030Read; |
| 36 | + | |
| 37 | - | private I2CDevice sl030Write; |
| 37 | + | |
| 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 | - | sl030Read = bus.getDevice(rfidAddrRead); |
| 48 | + | |
| 49 | - | sl030Write = bus.getDevice(rfidAddrWrite); |
| 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 | - | byte[] bytesTemp = new byte[4]; |
| 60 | + | |
| 61 | - | byte[] towrite= new byte[2]; |
| 61 | + | byte[] writeBuffer= new byte[2]; |
| 62 | String result="EMPTY"; | |
| 63 | try {
| |
| 64 | - | towrite[0]=(byte)0x01; |
| 64 | + | // SEND COMMAND TO DEVICE TO REQUEST FIRMWARE VERSION |
| 65 | - | towrite[1]=firmwareRfidCmd; |
| 65 | + | writeBuffer[0]=(byte)0x01; // LENDTH (CMD+DATA) |
| 66 | - | sl030Write.write(towrite,0,2); |
| 66 | + | writeBuffer[1]=firmwareRfidCmd; // COMMAND |
| 67 | sl030.write(writeBuffer, 0, 2); | |
| 68 | - | // sl030.r |
| 68 | + | |
| 69 | - | int readTotal = sl030Read.read(bytesTemp, 0, 4); |
| 69 | + | |
| 70 | - | System.out.println("Letti: " +readTotal);
|
| 70 | + | |
| 71 | - | // if (readTotal < 2) {
|
| 71 | + | // READ DEVICE FIRMWARE RESPONSE |
| 72 | - | // System.out.format("Error: %n bytes read/n", readTotal);
|
| 72 | + | |
| 73 | - | // return "EMPTY"; |
| 73 | + | // first just read in a single byte that represents the command+data payload |
| 74 | - | // } |
| 74 | + | int length = sl030.read(); |
| 75 | ||
| 76 | System.out.println("TOTAL BYTES AVAILABLE: " + length);
| |
| 77 | - | result = new String(bytesTemp); |
| 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 | } |