Skip to content

Commit 8170b1b

Browse files
committed
Wrote rdm6300 rfid reader lib, tested on esp32, esp8266, avr!
1 parent 924b0b4 commit 8170b1b

File tree

6 files changed

+220
-0
lines changed

6 files changed

+220
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* A simple example to interface with rdm6300 rfid reader.
3+
*
4+
* Connect the rdm6300 to VCC=5V, GND=GND, TX=any GPIO (this case GPIO_2)
5+
* Note: for ESP* or other 3.3V based microcontroller,
6+
* you must divide the TX out of the rdm6300 to the 3.3V levels,
7+
* I used the following resistor divider:
8+
* GND--<2K resistor>--GPIO_2--<1K resistor>--TX(rdm6300).
9+
*
10+
* Arad Eizen (https://github.com/arduino12) 23/09/18.
11+
*/
12+
#include <rdm6300.h>
13+
14+
#define RDM6300_RX_PIN 2
15+
#define READ_LED_PIN 13
16+
17+
Rdm6300 rdm6300;
18+
19+
void setup()
20+
{
21+
Serial.begin(115200);
22+
23+
pinMode(READ_LED_PIN, OUTPUT);
24+
digitalWrite(READ_LED_PIN, LOW);
25+
26+
rdm6300.begin(RDM6300_RX_PIN);
27+
28+
Serial.println("Place RFID tag near the rdm6300...");
29+
}
30+
31+
void loop()
32+
{
33+
/* if non-zero tag_id, update() returns true- a new tag is near! */
34+
if (rdm6300.update())
35+
Serial.println(rdm6300.get_tag_id(), HEX);
36+
37+
digitalWrite(READ_LED_PIN, rdm6300.is_tag_near());
38+
39+
delay(10);
40+
}

keywords.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#######################################
2+
# Syntax Coloring Map Rdm6300
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
Rdm6300 KEYWORD1
10+
11+
#######################################
12+
# Methods and Functions (KEYWORD2)
13+
#######################################
14+
begin KEYWORD2
15+
update KEYWORD2
16+
get_tag_id KEYWORD2
17+
is_tag_near KEYWORD2
18+
19+
#######################################
20+
# Constants (LITERAL1)
21+
#######################################
22+
RDM6300_PACKET_SIZE LITERAL1
23+
RDM6300_PACKET_BEGIN LITERAL1
24+
RDM6300_PACKET_END LITERAL1
25+
RDM6300_NEXT_READ_MS LITERAL1
26+
RDM6300_BAUDRATE LITERAL1

library.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "rdm6300",
3+
"version": "1.0.0",
4+
"keywords": "rdm6300, rfid",
5+
"description": "A simple library to interface with rdm6300 rfid reader.",
6+
"repository":
7+
{
8+
"type": "git",
9+
"url": "https://github.com/arduino12/rdm6300.git"
10+
},
11+
"authors":
12+
[
13+
{
14+
"name": "Arad Eizen",
15+
"email": "https://github.com/arduino12",
16+
"url": "https://github.com/arduino12"
17+
"maintainer": true
18+
}
19+
],
20+
"frameworks": "arduino",
21+
"platforms": "*"
22+
}

library.properties

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=Rdm6300
2+
version=1.0.0
3+
author=Arad Eizen
4+
maintainer=Arad Eizen <https://github.com/arduino12>
5+
sentence=A simple library to interface with rdm6300 rfid reader.
6+
paragraph=A simple library to interface with rdm6300 rfid reader.
7+
category=Sensors
8+
url=https://github.com/arduino12/rdm6300
9+
architectures=*

rdm6300.cpp

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* A simple library to interface with rdm6300 rfid reader.
3+
* Arad Eizen (https://github.com/arduino12) 23/09/18.
4+
*/
5+
6+
#include "RDM6300.h"
7+
#include <Arduino.h>
8+
9+
10+
void Rdm6300::begin(int rx_pin)
11+
{
12+
/* init serial port to rdm6300 baud, without TX, and 20ms read timeout */
13+
#ifdef ARDUINO_ARCH_ESP32
14+
_serial = new HardwareSerial(1);
15+
_serial->begin(RDM6300_BAUDRATE, SERIAL_8N1, rx_pin, -1);
16+
#else
17+
_serial = new SoftwareSerial(rx_pin, -1);
18+
_serial->begin(RDM6300_BAUDRATE);
19+
#endif
20+
_serial->setTimeout(20);
21+
}
22+
23+
bool Rdm6300::update(void)
24+
{
25+
char buff[RDM6300_PACKET_SIZE];
26+
uint32_t tag_id;
27+
uint8_t checksum;
28+
29+
if (!_serial->available())
30+
return false;
31+
32+
/* if a packet doesn't begin with the right byte, remove that byte */
33+
if (_serial->peek() != RDM6300_PACKET_BEGIN && _serial->read())
34+
return false;
35+
36+
/* if read a packet with the wrong size, drop it */
37+
if (RDM6300_PACKET_SIZE != _serial->readBytes(buff, RDM6300_PACKET_SIZE))
38+
return false;
39+
40+
/* if a packet doesn't end with the right byte, drop it */
41+
if (buff[13] != RDM6300_PACKET_END)
42+
return false;
43+
44+
/* add null and parse checksum */
45+
buff[13] = 0;
46+
checksum = strtol(buff + 11, NULL, 16);
47+
/* add null and parse tag_id */
48+
buff[11] = 0;
49+
tag_id = strtol(buff + 3, NULL, 16);
50+
/* add null and parse version (needs to be xored with checksum) */
51+
buff[3] = 0;
52+
checksum ^= strtol(buff + 1, NULL, 16);
53+
54+
/* xore the tag_id and validate checksum */
55+
for (uint8_t i = 0; i < 32; i += 8)
56+
checksum ^= ((tag_id >> i) & 0xFF);
57+
if (checksum)
58+
return false;
59+
60+
/* if a new tag appears- return it */
61+
if (_last_tag_id != tag_id) {
62+
_last_tag_id = tag_id;
63+
_next_read_ms = 0;
64+
}
65+
/* if the old tag is still here set tag_id to zero */
66+
if (_next_read_ms > millis())
67+
tag_id = 0;
68+
_next_read_ms = millis() + RDM6300_NEXT_READ_MS;
69+
70+
_tag_id = tag_id;
71+
return tag_id;
72+
}
73+
74+
bool Rdm6300::is_tag_near(void)
75+
{
76+
return _next_read_ms > millis();
77+
}
78+
79+
uint32_t Rdm6300::get_tag_id(void)
80+
{
81+
uint32_t tag_id = _tag_id;
82+
_tag_id = 0;
83+
return tag_id;
84+
}

rdm6300.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* A simple library to interface with rdm6300 rfid reader.
3+
* Arad Eizen (https://github.com/arduino12) 23/09/18.
4+
*/
5+
6+
#ifndef RDM6300_h
7+
#define RDM6300_h
8+
9+
#ifdef ARDUINO_ARCH_ESP32
10+
#include <HardwareSerial.h>
11+
#else
12+
#include <SoftwareSerial.h>
13+
#endif
14+
15+
#define RDM6300_PACKET_SIZE 14
16+
#define RDM6300_PACKET_BEGIN 0x02
17+
#define RDM6300_PACKET_END 0x03
18+
#define RDM6300_NEXT_READ_MS 220
19+
#define RDM6300_BAUDRATE 9600
20+
21+
class Rdm6300
22+
{
23+
public:
24+
void begin(int rxPin);
25+
bool update(void);
26+
uint32_t get_tag_id(void);
27+
bool is_tag_near(void);
28+
private:
29+
#ifdef ARDUINO_ARCH_ESP32
30+
HardwareSerial * _serial;
31+
#else
32+
SoftwareSerial * _serial;
33+
#endif
34+
uint32_t _tag_id = 0;
35+
uint32_t _last_tag_id = 0;
36+
uint32_t _next_read_ms = 0;
37+
};
38+
39+
#endif

0 commit comments

Comments
 (0)