-
Notifications
You must be signed in to change notification settings - Fork 3
/
RFIDRdm630.cpp
160 lines (104 loc) · 2.5 KB
/
RFIDRdm630.cpp
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
/*
RFIDRdm630.h - Library for use RFID tags with RDM630 module in UART usage.
Created by Danilo Queiroz Barbosa, August 26, 2016.
Member of electronicdrops.com
Released into the public domain.
*/
#include <SoftwareSerial.h>
#include "RFIDRdm630.h"
#include "Arduino.h"
#include <stdio.h>
unsigned char charToHexDigit(char c)
{
if (c >= 'A')
return c - 'A' + 10;
else
return c - '0';
}
unsigned char stringToByte(char c[2])
{
return charToHexDigit(c[0]) * 16 + charToHexDigit(c[1]);
}
RFIDRdm630::RFIDRdm630(int rxPin, int txPin) {
_rfidSerial = new SoftwareSerial(rxPin, txPin);
_rfidSerial->begin(9600);
_isAvailable = false;
//_RFIDtag = new RFIDtag();
}
boolean RFIDRdm630::isAvailable() {
int bytesRead;
char buff[_tagLength + 1];
_isAvailable = false;
if (_rfidSerial->available() >= _totalLength) {
if (_rfidSerial->read() == _startTag) {
bytesRead = 0;
while (bytesRead < _tagLength) {
int val = _rfidSerial->read();
if ((val == _startTag) || (val == _endTag)) {
break;
}
buff[bytesRead] = val;
bytesRead += 1;
}
if (_rfidSerial->read() == _endTag) {
buff[bytesRead] = 0;
_isAvailable = true;
_RFIDtag.setTag(buff);
}
}
}
return _isAvailable;
}
RFIDtag RFIDRdm630::getTag() {
return _RFIDtag;
}
void RFIDRdm630::listen() {
_rfidSerial->listen();
}
/*
* Classe RFIDtag
*
*/
RFIDtag::RFIDtag(){
*_tag = NULL;
_isValid = true;
_cardNumber = 0;
}
char * RFIDtag::getTag(){
return _isValid? _tag:NULL;
}
boolean RFIDtag::validateTag(){
char checksum[2] = {'0', '0'};
char checksumValue = '\0';
char piece[2] = {'0', '0'};
char hexValue = '\0';
strncpy(checksum,_tag+10,2);
checksumValue = stringToByte(checksum);
for (int i = 0; i < _tagLength-2; i+=2){
strncpy(piece,_tag+i,2);
hexValue ^= stringToByte(piece);
if ( i > 0 and i < (_tagLength-2)){
_cardNumber = _cardNumber << 8;
_cardNumber += stringToByte(piece);
}
}
return _isValid = (checksumValue == hexValue);
}
boolean RFIDtag::isValid(){
return _isValid;
}
boolean RFIDtag::setTag(char tag[]){
strcpy(_tag,tag);
validateTag();
if(_isValid){
return true;
}
else{
*_tag = NULL;
_cardNumber = 0;
return false;
}
}
long RFIDtag::getCardNumber(){
return _isValid? _cardNumber:NULL;
}