Hello.
I am trying to implement an RFID RDM6300 to read 125KHz cards to an ESP32-EVB but I can't figure out why it doesn't work.
This is the reader:
https://www.optimusdigital.ro/ro/wireless-rfid/1500-cititor-de-carduri-rfid-rdm6300.html
This is the wiring:
(https://i.ibb.co/Kjnz3hn/20210202-150214.jpg) (https://ibb.co/zhdH0xd)
This is the code:
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;
long hexstr_to_value(char *str, unsigned int length) { // converts a hexadecimal value (encoded as ASCII string) to a numeric value
char* copy = (char*)malloc((sizeof(char) * length) + 1);
memcpy(copy, str, sizeof(char) * length);
copy[length] = '\0';
// the variable "copy" is a copy of the parameter "str". "copy" has an additional '\0' element to make sure that "str" is null-terminated.
long value = strtol(copy, NULL, 16); // strtol converts a null-terminated string to a long value
free(copy); // clean up
return value;
}
unsigned 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("--");
long tag = hexstr_to_value((char*)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((char*)msg_data + i, CHECKSUM_SIZE);
checksum ^= val;
}
Serial.print("Extracted Checksum (HEX): ");
Serial.print(checksum, HEX);
if (checksum == hexstr_to_value((char*)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;
}
void setup() {
delay(100);
Serial.begin(115200);
Serial1.begin(9600);
delay(100);
}
void loop() {
delay(2);
if (Serial1.available() > 0){
bool call_extract_tag = false;
int ssvalue = Serial1.read(); // read
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
}
if (buffer_index >= BUFFER_SIZE) { // checking for a buffer overflow (It's very unlikely that an buffer overflow comes up!)
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) {
unsigned tag = extract_tag();
} else { // something is wrong... start again looking for preamble (value: 2)
buffer_index = 0;
return;
}
}
}
}
I've used Serial1, which has RX1 - 36 and TX1 - 4, which can be fond in the UEXT connector.
What do you think ?
Hmmm are you selecting ESP32-EVB from the board selector? If you selected other board the pins for serial1 might be different. Maybe try to re-bind these. Maybe try software serial instead.
Do you have simple USB-serial adapter cable that can be used to test the UART? Can you test simple UART bridge code with some simple USB-serial cable (instead of the RFID):
#define SERIAL Serial1
void setup()
{
Serial.begin (115200);
SERIAL.begin (115200);
}
void loop()
{
if (Serial.available ()>0)
SERIAL.print((char)Serial.read());
if (SERIAL.available ()>0)
Serial.print((char)SERIAL.read());
}
Quote from: LubOlimex on February 03, 2021, 09:49:32 AMHmmm are you selecting ESP32-EVB from the board selector? If you selected other board the pins for serial1 might be different. Maybe try to re-bind these. Maybe try software serial instead.
Do you have simple USB-serial adapter cable that can be used to test the UART? Can you test simple UART bridge code with some simple USB-serial cable (instead of the RFID):
#define SERIAL Serial1
void setup()
{
Serial.begin (115200);
SERIAL.begin (115200);
}
void loop()
{
if (Serial.available ()>0)
SERIAL.print((char)Serial.read());
if (SERIAL.available ()>0)
Serial.print((char)SERIAL.read());
}
I fixed it.
The problem was the wiring. I "accidentally" linked the board's TX to the ESP's TX instead of the ESP's RX.
Now I only have to decode the signal that I'm receiving.