-
Notifications
You must be signed in to change notification settings - Fork 0
/
HandheldRead-Write1.0.ino
395 lines (353 loc) · 14 KB
/
HandheldRead-Write1.0.ino
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#define DisplaySerial Serial
#define printDelay 12
#define SIZE_OF_DATA 21
#include <Picaso_Const4D.h>
#include <Picaso_Serial_4DLib.h>
#include <SoftwareSerial.h>
SoftwareSerial rfid(7, 8);
Picaso_Serial_4DLib Display(&DisplaySerial);
//Prototypes
void halt(void);
void parse1(void);
void print_serial(void);
void read_serial(void);
void seek1(void);
void set_flag(void);
void read_Block(int block, int Data[21]);
void write_Block(int block,int writeData[21]);
void Authenticate(void);
void printData(void);
void parse2(void);
void parse3(void);
void printAuth(void);
void read_data(void);
void set_flag3(void);
//Global var
int flag = 0;
int flag2 = 0;
int flag3 = 0;
int Str1[10];
int writeData[21];
int Data[SIZE_OF_DATA];
char charData[SIZE_OF_DATA];
int auth[6];
int var[10];
int tagFound = 0;
String displayData;
int TIMEOUT_COUNTER_LIMIT = 500;
//INIT
void setup()
{
Serial.begin(9600); //Serial doesnt effact NFC
Serial.println("Start");
//----------------------------------4D Systems---------------------------------------------
//For handling errors
// Display.Callback4D = mycallback ;
//
//5 second timeout on all commands
Display.TimeLimit4D = 5000 ;
displayData.reserve(SIZE_OF_DATA);
//--------------------------------Optional reset routine-----------------------------------
//Reset the Display using D4 of the Arduino (if using the new 4D Arduino Adaptor - Rev 2)
//If using the old 4D Arduino Adaptor (Rev 1), change D4 to D2 below.
//If using jumper wires, reverse the logic states below.
//Refer to the accompanying application note for important information.
pinMode(4, OUTPUT); // Set D4 on Arduino to Output (4D Arduino Adaptor V2 - Display Reset)
digitalWrite(4, 1); // Reset the Display via D4
delay(100);
digitalWrite(4, 0); // unReset the Display via D4
//----------------------------------------------------------------------------------------------
delay (5000); //let the display start up
Display.gfx_ScreenMode(PORTRAIT);
//Display.gfx_BGcolour(WHITE) ; //change background color to white
Display.gfx_Cls(); //clear the screen
//-------------------------------------ENDof4DSetup-----------------------------------------------
// set the data rate for the SoftwareSerial ports
rfid.begin(19200);
delay(10);
for (int i = 0; i < 10; i++)
{
var[i] = 0x00;
}
halt();
seek1();
while (rfid.available() > 0) {
for (int i = 0; i < 10; i++) {
if (rfid.available() > 0) {
var[i] = rfid.read();
}
}
}
}
/***********MAIN LOOP***********************************************************************************************************/
//MAIN
void loop()
{
read_serial(); //read serial number and print it
delay(10);
// Authenticate(); //MAKES SURE THE DATA IS CORRECT/ AUTHORIZATION IS THERE (NEEDED FOR READ/WRITE BLOCK
delay(10);
// write_Block(5, writeData); //WRITES MESSAGE INTO TAG
// delay(10);
read_data(); //READS DATA FROM TAG
// delay(10);
}
/*****************************HALT******************************************************************************************/
void halt() //executes mifare halt command on selected tag
{
//Halt tag
rfid.write((uint8_t)0xff);
rfid.write((uint8_t)0x00);
rfid.write((uint8_t)0x01);
rfid.write((uint8_t)0x93);
rfid.write((uint8_t)0x94);
}
/************************************************************************************************************************/
void flushSoftSerial () { //clears buffer
while (rfid.available() > 0) {
rfid.read();
}
}
/************************lookForSeekResponse***********************************************************************************************/
void lookForSeekResponse() //looks for response from seek command. if there is a tag there, the "seeking" response comes back first, then it comes back with a "found" response
{
int firstTimeoutCounter = 0;
bool foundFirstResponse = false;
while (!foundFirstResponse && firstTimeoutCounter < TIMEOUT_COUNTER_LIMIT) //while command was sent out and time hasnt run out
{
firstTimeoutCounter++; //increment time
if (rfid.available() > 0) //if something is in the buffer
foundFirstResponse = (rfid.peek() == 0xFF); //first response is true if header is correct
}
if (foundFirstResponse) //if first response is true
{
for (int i = 0; i < 6; i++) //parse through data
{
Str1[i] = rfid.read();
}
int secondTimeoutCounter = 0;
bool foundSecondResponse = false;
while (!foundSecondResponse && secondTimeoutCounter < TIMEOUT_COUNTER_LIMIT) //while second response found and time not out
{
secondTimeoutCounter++; //increment second timer (this is to make sure we dont get stuck)
if (rfid.available() > 0) //if theres something in the buffer
foundSecondResponse = (rfid.peek() == 0xFF); //foundSecondResponse true if header is correct
}
if (!foundSecondResponse) { //if second response not true
tagFound = 0; //tag not found
}
if (foundSecondResponse) //if it was found
{
tagFound++; //increment tagfound
for (int i = 0; i < 10; i++) //parse through data, storing it into Str1
{
Str1[i] = rfid.read();
}
}
}
}
/***********SERIAL NUMBER**********************************************************************************************************/
void print_serial() //PRINTS SERIAL NUMBER BY PRINTING STR1
{
if (tagFound == 1) { //print to serial port
Serial.print("Serial number: ");
Serial.print(Str1[8], HEX);
Serial.print(Str1[7], HEX);
Serial.print(Str1[6], HEX);
Serial.print(Str1[5], HEX);
Serial.println();
delay(printDelay);
}
}
void read_serial() //reads serial number and then uses print_serial to print it
{
flushSoftSerial(); //gets rid of anything in the buffer
seek1(); //seeks tags. when tag found, selects it and returns the serial number
delay(10);
lookForSeekResponse(); //checks if there is a tag there
delay(10);
print_serial(); //prints serial number if tag present
}
/**********************READ AND PRINT DATA***********************************************************************************************/
void read_data() //reads data stored in a block
{
flushSoftSerial(); //if anything is in buffer, it gets rid of it
if (tagFound == 1) {
Authenticate(); //authenticates tag **** if using write function, COMMENT THIS OUT
read_Block (5, Data); //calls read function
if (rfid.available() > 0) { //if something is in buffer
for(int i=0; i<21;i++){
Data[i] = rfid.read(); //stores information from buffer into data
delay(1);
}
printData(); //prints the data from specified block
}
}
}
void printData() //prints data from blocks in tag
{
// //print to serial port
// Serial.print("Card Data: ");
// Serial.print(Data[0], HEX);
// Serial.print(Data[1], HEX);
// Serial.print(Data[2], HEX);
// Serial.print(Data[3], HEX);
// Serial.print(Data[4], HEX);
//
// for(int i=5;i<=21;i++){
// Serial.write(Data[i]);
// }
// Serial.print("\n");
Display.print("Card Data: ");
for(int i=5;i<=21;i++){
charData[i] = Data[i];
Display.print(charData[i]);
}
Display.println("");
}
/**************************SEEK********************************************************************************************/
void seek1() //looks for tags and selects them
{
//search for RFID tag
rfid.write((uint8_t)0xff); //header
rfid.write((uint8_t)0x00); //reserve
rfid.write((uint8_t)0x01); //length of command and response
rfid.write((uint8_t)0x82); //command
rfid.write((uint8_t)0x83); //check sum
delay(10);
}
/***********************AUTHENTICATE AND PRINT AUTH****************************************************************************************************/
void Authenticate() //authenticates the tag so information of tag can be changed
{
// Serial.println("in authenticate");
if (tagFound == 0) {
Serial.println("tag not found");
}
if (tagFound == 1) {
Serial.println("tag found");
rfid.write((uint8_t)0xff); //header
rfid.write((uint8_t)0x00); //reserve
rfid.write((uint8_t)0x03); //length of command and response
rfid.write((uint8_t)0x85); //command
rfid.write((uint8_t)0x05); //block number
rfid.write((uint8_t)0xff); //key type
rfid.write((uint8_t)0x18c); //check sum
delay(10);
if(rfid.available() >0) {
for (int i = 0; i < 6; i++) {
auth[i] = rfid.read();
}
printAuth();
}
}
}
void printAuth() //prints authenticate response
{
if (tagFound == 1) { //if a tag is found, prints to serial once
Serial.print("auth code: ");
Serial.print(auth[0], HEX );
Serial.print(auth[1], HEX );
Serial.print(auth[2], HEX );
Serial.print(auth[3], HEX );
Serial.print(auth[4], HEX );
Serial.print(auth[5], HEX);
Serial.println();
}
// else {
// Serial.println("flag3 wasnt 1");
// Serial.println(flag3);
}
// }
//}
/************READ AND WRITE INTO BLOCK 5*****************************************************************************************************************/
void read_Block(int block, int * Data) //command for reading a specified block
{
if (tagFound == 1) { //if a tag is found, it writes and prints this once
rfid.write((uint8_t)0xff); //header
rfid.write((uint8_t)0x00); //reserve
rfid.write((uint8_t)0x02); //length
rfid.write((uint8_t)0x86); //command
rfid.write((uint8_t)block); //block
rfid.write((uint8_t)0x8d); //checksum
delay(10);
}
}
void write_Block(int block, int * writeData) //command to write data into specified block
{
flushSoftSerial();
if (tagFound == 1)
{
Authenticate();
flushSoftSerial();
rfid.write((uint8_t)0xff); //header
rfid.write((uint8_t)0); //reserve
rfid.write((uint8_t)0x12); //length
rfid.write((uint8_t)0x89); //command (1)
rfid.write((uint8_t)0x05); // block 5(2)
rfid.write((uint8_t)'h'); //written (3)
rfid.write((uint8_t)'o'); //written (4)
rfid.write((uint8_t)'w'); //written (5)
rfid.write((uint8_t)'s'); //written (6)
rfid.write((uint8_t)' '); //written (7)
rfid.write((uint8_t)'i'); //written (8)
rfid.write((uint8_t)'t'); //written (9)
rfid.write((uint8_t)' '); //written (a)
rfid.write((uint8_t)'g'); //written (b)
rfid.write((uint8_t)'o'); //written (c)
rfid.write((uint8_t)'i'); //written (d)
rfid.write((uint8_t)'n'); //written (e)
rfid.write((uint8_t)'g'); //written (f)
rfid.write((uint8_t)'?'); //written (10)
rfid.write((uint8_t)':'); //written (11)
rfid.write((uint8_t)')'); //written (12)
rfid.write((uint8_t)1588); //checksum
delay(100);
// Serial.println ("Data written");
if(rfid.available()>0) { //if something is in the buffer
for(int i =0; i<21; i++){
writeData[i] = rfid.read(); //store each char in writeData
}
}
// printwriteData();
if(writeData[4] ==0x46) { //error code
Serial.println("Write Failed");
}
if(writeData[4] == 0x4E) { //error code
Serial.println("No tag Present");
}
if(writeData[4] == 0x58){ //error code
Serial.println("Unable to read after write");
}
if(writeData[4] == 0x55){ //error code
Serial.println("Read after write failed");
}
}
}
void printwriteData() //prints data from blocks in tag
{
//print to serial port
Serial.print("Card write Data: ");
// Serial.print(writeData[0], HEX);
// Serial.print(writeData[1], HEX);
// Serial.print(writeData[2], HEX);
// Serial.print(writeData[3], HEX);
// Serial.print(writeData[4], HEX);
Serial.write(writeData[5]);
Serial.write(writeData[6]);
Serial.write(writeData[7]);
Serial.write(writeData[8]);
Serial.write(writeData[9]);
Serial.write(writeData[10]);
Serial.write(writeData[11]);
Serial.write(writeData[12]);
Serial.write(writeData[13]);
Serial.write(writeData[14]);
Serial.write(writeData[15]);
Serial.write(writeData[16]);
Serial.write(writeData[17]);
Serial.write(writeData[18]);
Serial.write(writeData[19]);
Serial.write(writeData[20]);
Serial.write(writeData[21]);
Serial.println();
}