Created
June 1, 2022 14:43
-
-
Save s-light/812bb44576f53fe592ad2ee8d13a97b4 to your computer and use it in GitHub Desktop.
example for 125kHz RFID reader RDM6300 → save both files as one sketch.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const int BUFFER_SIZE = 14; // RFID DATA FRAME FORMAT: 1byte head (value: 2), 10byte data (2byte version + 8byte tag), 2byte checksum, 1byte tail (value: 3) | |
const int DATA_SIZE = 10; // 10byte data (2byte version + 8byte tag) | |
const int DATA_VERSION_SIZE = 2; // 2byte version (actual meaning of these two bytes may vary) | |
const int DATA_TAG_SIZE = 8; // 8byte tag | |
const int CHECKSUM_SIZE = 2; // 2byte checksum | |
uint8_t buffer[BUFFER_SIZE]; // used to store an incoming data frame | |
int buffer_index = 0; | |
uint32_t tag = 0; | |
unsigned RFID_update() { | |
if (ssrfid.available() > 0){ | |
bool call_extract_tag = false; | |
int ssvalue = ssrfid.read(); // read | |
Serial.print("ssvalue : '"); | |
Serial.print(ssvalue); | |
Serial.println("'"); | |
if (ssvalue == -1) { // no data was read | |
return; | |
} | |
if (ssvalue == 2) { // RDM630/RDM6300 found a tag => tag incoming | |
buffer_index = 0; | |
} else if (ssvalue == 3) { // tag has been fully transmitted | |
call_extract_tag = true; // extract tag at the end of the function call | |
} | |
// checking for a buffer overflow (It's very unlikely that an buffer overflow comes up!) | |
if (buffer_index >= BUFFER_SIZE) { | |
Serial.println("Error: Buffer overflow detected! "); | |
return; | |
} | |
buffer[buffer_index++] = ssvalue; // everything is alright => copy current value to buffer | |
if (call_extract_tag == true) { | |
if (buffer_index == BUFFER_SIZE) { | |
tag = extract_tag(); | |
tag_found(tag); | |
} else { // something is wrong... start again looking for preamble (value: 2) | |
buffer_index = 0; | |
return; | |
} | |
} | |
} | |
return tag; | |
} | |
uint32_t extract_tag() { | |
uint8_t msg_head = buffer[0]; | |
uint8_t *msg_data = buffer + 1; // 10 byte => data contains 2byte version + 8byte tag | |
uint8_t *msg_data_version = msg_data; | |
uint8_t *msg_data_tag = msg_data + 2; | |
uint8_t *msg_checksum = buffer + 11; // 2 byte | |
uint8_t msg_tail = buffer[13]; | |
// print message that was sent from RDM630/RDM6300 | |
Serial.println("--------"); | |
Serial.print("Message-Head: "); | |
Serial.println(msg_head); | |
Serial.println("Message-Data (HEX): "); | |
for (int i = 0; i < DATA_VERSION_SIZE; ++i) { | |
Serial.print(char(msg_data_version[i])); | |
} | |
Serial.println(" (version)"); | |
for (int i = 0; i < DATA_TAG_SIZE; ++i) { | |
Serial.print(char(msg_data_tag[i])); | |
} | |
Serial.println(" (tag)"); | |
Serial.print("Message-Checksum (HEX): "); | |
for (int i = 0; i < CHECKSUM_SIZE; ++i) { | |
Serial.print(char(msg_checksum[i])); | |
} | |
Serial.println(""); | |
Serial.print("Message-Tail: "); | |
Serial.println(msg_tail); | |
Serial.println("--"); | |
uint32_t tag = hexstr_to_value(msg_data_tag, DATA_TAG_SIZE); | |
Serial.print("Extracted Tag: "); | |
Serial.println(tag); | |
long checksum = 0; | |
for (int i = 0; i < DATA_SIZE; i+= CHECKSUM_SIZE) { | |
long val = hexstr_to_value(msg_data + i, CHECKSUM_SIZE); | |
checksum ^= val; | |
} | |
Serial.print("Extracted Checksum (HEX): "); | |
Serial.print(checksum, HEX); | |
if (checksum == hexstr_to_value(msg_checksum, CHECKSUM_SIZE)) { // compare calculated checksum to retrieved checksum | |
Serial.print(" (OK)"); // calculated checksum corresponds to transmitted checksum! | |
} else { | |
Serial.print(" (NOT OK)"); // checksums do not match | |
} | |
Serial.println(""); | |
Serial.println("--------"); | |
return tag; | |
} | |
uint32_t hexstr_to_value(char *str, unsigned int length) { | |
// converts a hexadecimal value (encoded as ASCII string) to a numeric value | |
char* copy = malloc((sizeof(char) * length) + 1); | |
// the variable "copy" is a copy of the parameter "str". | |
memcpy(copy, str, sizeof(char) * length); | |
// "copy" has an additional '\0' element to make sure that "str" is null-terminated. | |
copy[length] = '\0'; | |
// strtol converts a null-terminated string to a long value | |
uint32_t value = strtol(copy, NULL, 16); | |
// clean up | |
free(copy); | |
return value; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
RDM6300-125KHz-RFID | |
based on www.mschoeffler.de Arduino Examples | |
modified on 20 Jan 2020 | |
by Amir Mohammad Shojaee @ Electropeak | |
https://electropeak.com/learn/interfacing-rdm6300-125khz-rfid-reader-module-with-arduino/ | |
20220531 s-light.eu stefan krüger | |
extracted RFID code into extra file. | |
*/ | |
#include <SoftwareSerial.h> | |
SoftwareSerial ssrfid = SoftwareSerial(2, 3); | |
void setup() { | |
Serial.begin(115200); | |
ssrfid.begin(9600); | |
ssrfid.listen(); | |
Serial.println(" INIT DONE"); | |
} | |
void loop() { | |
RFID_update(); | |
} | |
void tag_found(uint32_t tag) { | |
Serial.print("Found Tag: "); | |
Serial.print(tag); | |
Serial.println(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment