forked from LiangYuen/Pi4j-RC522
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReadRFID.java
211 lines (172 loc) · 6.67 KB
/
ReadRFID.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package com.liangyuen.idea.raspberryRFID;
/**
* Created by Liang on 2016/3/7.
*/
import com.liangyuen.util.Convert;
import com.liangyuen.util.*;
import com.pi4j.io.gpio.RaspiPin;
import com.pi4j.wiringpi.Gpio;
import com.pi4j.wiringpi.Spi;
import com.liangyuen.util.FMRC522;
public class ReadRFID
{
public static void main(String[] args) throws InterruptedException
{
RaspRC522 rc522=new RaspRC522();
int back_bits[]=new int[1];
String strUID;
byte tagid[]=new byte[5];
int i,status;
byte blockaddress=8; //读写块地址0-63
byte sector=15,block=2;
//读卡,得到序列号
// if(rc522.Request(RaspRC522.PICC_REQIDL, back_bits) == rc522.MI_OK)
// System.out.println("Detected:"+back_bits[0]);
// if(rc522.AntiColl(tagid) != RaspRC522.MI_OK)
// {
// System.out.println("anticoll error");
// return;
// }
//
// //Select the scanned tag,选中指定序列号的卡
// int size=rc522.Select_Tag(tagid);
// System.out.println("Size="+size);
rc522.Select_MirareOne(tagid);
strUID= Convert.bytesToHex(tagid);
//System.out.println(strUID);
//System.out.println("Card Read UID:" + tagid[0] + "," + tagid[1] + "," + tagid[2] + "," + tagid[3]);
System.out.println("Card Read UID:" + strUID.substring(0,2) + "," +
strUID.substring(2,4) + "," +
strUID.substring(4,6) + "," +
strUID.substring(6,8));
//default key
byte []keyA=new byte[]{(byte)0x03,(byte)0x03,(byte)0x00,(byte)0x01,(byte)0x02,(byte)0x03};
byte[] keyB=new byte[]{(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF};
//Authenticate,A密钥验证卡,可以读数据块2
byte data[]=new byte[16];
status = rc522.Auth_Card(RaspRC522.PICC_AUTHENT1A, sector,block, keyA, tagid);
if(status != RaspRC522.MI_OK)
{
System.out.println("Authenticate A error");
return;
}
status=rc522.Read(sector,block,data);
//rc522.Stop_Crypto();
System.out.println("Successfully authenticated,Read data="+Convert.bytesToHex(data));
status=rc522.Read(sector,(byte)3,data);
System.out.println("Read control block data="+Convert.bytesToHex(data));
for (i = 0; i < 16; i++)
{
data[i]=(byte)0x00;
}
//Authenticate,B密钥验证卡,可以写数据块2
status = rc522.Auth_Card(RaspRC522.PICC_AUTHENT1B, sector,block, keyB, tagid);
if(status != RaspRC522.MI_OK)
{
System.out.println("Authenticate B error");
return;
}
status=rc522.Write(sector,block,data);
if( status== RaspRC522.MI_OK)
System.out.println("Write data finished");
else
{
System.out.println("Write data error,status="+status);
return;
}
// byte buff[]=new byte[16];
//
// for (i = 0; i < 16; i++)
// {
// buff[i]=(byte)0;
// }
// status=rc522.Read(sector,block,buff);
// if(status == RaspRC522.MI_OK)
// System.out.println("Read Data finished");
// else
// {
// System.out.println("Read data error,status="+status);
// return;
// }
//
// System.out.print("sector"+sector+",block="+block+" :");
// String strData= Convert.bytesToHex(buff);
// for (i=0;i<16;i++)
// {
// System.out.print(strData.substring(i*2,i*2+2));
// if(i < 15) System.out.print(",");
// else System.out.println("");
// }
}
public static void rfidReaderLoop(int sleeptime) throws InterruptedException
{
int count=0;
while (count++ < 3)
{
int packetlength = 5;
byte packet[] = new byte[packetlength];
packet[0] = (byte) 0x80; // FIRST PACKET GETS IGNORED BUT HAS
// TO BE SET TO READ
packet[1] = (byte) 0x80; // ADDRESS 0 Gives data of Address 0
packet[2] = (byte) 0x82; // ADDRESS 1 Gives data of Address 1
packet[3] = (byte) 0x84; // ADDRESS 2 Gives data of Address 2
packet[4] = (byte) 0x86; // ADDRESS 3 Gives data of Address 3
System.out.println("-----------------------------------------------");
System.out.println("Data to be transmitted:");
System.out.println("[TX] " + bytesToHex(packet));
System.out.println("[TX1] " + packet[1]);
System.out.println("[TX2] " + packet[2]);
System.out.println("[TX3] " + packet[3]);
System.out.println("[TX4] " + packet[4]);
System.out.println("Transmitting data...");
// Send data to Reader and receive answerpacket.
packet = readFromRFID(0, packet, packetlength);
System.out.println("Data transmitted, packets received.");
System.out.println("Received Packets (First packet to be ignored!)");
System.out.println("[RX] " + bytesToHex(packet));
System.out.println("[RX1] " + packet[1]);
System.out.println("[RX2] " + packet[2]);
System.out.println("[RX3] " + packet[3]);
System.out.println("[RX4] " + packet[4]);
System.out.println("-----------------------------------------------");
if (packet.length == 0)
{
//Reset when no packet received
//ResetPin.high();
Thread.sleep(50);
//ResetPin.low();
}
// Wait 1/2 second before trying to read again
Thread.sleep(sleeptime);
}
}
public static byte[] readFromRFID(int channel, byte[] packet, int length)
{
Spi.wiringPiSPIDataRW(channel, packet, length);
return packet;
}
public static boolean writeToRFID(int channel, byte fullAddress, byte data)
{
byte[] packet = new byte[2];
packet[0] = fullAddress;
packet[1] = data;
if (Spi.wiringPiSPIDataRW(channel, packet, 1) >= 0)
return true;
else
return false;
}
public static String bytesToHex(byte[] bytes)
{
final char[] hexArray = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', 'A', 'B', 'C', 'D', 'E', 'F' };
char[] hexChars = new char[bytes.length * 2];
int v;
for (int j = 0; j < bytes.length; j++)
{
v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
}