public
Created — forked from maniacbug/rfid_decimal.pde

Complete example for reading the 125Khz RFID module RDM630

  • Download Gist
rfid_decimal.pde
Processing
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
#include <SoftwareSerial.h>
// Pin definitions
const int rfid_irq = 0;
const int rfid_tx_pin = 6;
const int rfid_rx_pin = 7;
// For communication with RFID module
SoftwareSerial rfid(rfid_tx_pin, rfid_rx_pin);
// Indicates that a reading is now ready for processing
volatile bool ready = false;
// Buffer to contain the reading from the module
uint8_t buffer[14];
uint8_t* buffer_at;
uint8_t* buffer_end = buffer + sizeof(buffer);
void rfid_read(void);
uint8_t rfid_get_next(void);
void setup(void)
{
// Open serial connection to host PC to view output
Serial.begin(57600);
Serial.println("rfid_decimal");
// Open software serial connection to RFID module
pinMode(rfid_tx_pin,INPUT);
rfid.begin(9600);
// Listen for interrupt from RFID module
attachInterrupt(rfid_irq,rfid_read,FALLING);
}
void loop(void)
{
if ( ready )
{
// Convert the buffer into a 32-bit value
uint32_t result = 0;
// Skip the preamble
++buffer_at;
// Accumulate the checksum, starting with the first value
uint8_t checksum = rfid_get_next();
// We are looking for 4 more values
int i = 4;
while(i--)
{
// Grab the next value
uint8_t value = rfid_get_next();
// Add it into the result
result <<= 8;
result |= value;
// Xor it into the checksum
checksum ^= value;
}
// Pull out the checksum from the data
uint8_t data_checksum = rfid_get_next();
// Print the result
Serial.print("Reading: ");
Serial.print(result);
if ( checksum == data_checksum )
Serial.println(" OK");
else
Serial.println(" CHECKSUM FAILED");
// We're done processing, so there is no current value
ready = false;
}
}
// Convert the next two chars in the stream into a byte and
// return that
uint8_t rfid_get_next(void)
{
// sscanf needs a 2-byte space to put the result but we
// only need one byte.
uint16_t result;
// Working space to assemble each byte
static char byte_chars[3];
// Pull out one byte from this position in the stream
snprintf(byte_chars,3,"%c%c",buffer_at[0],buffer_at[1]);
sscanf(byte_chars,"%x",&result);
buffer_at += 2;
return static_cast<uint8_t>(result);
}
void rfid_read(void)
{
// Only read in values if there is not already a value waiting to be
// processed
if ( ! ready )
{
// Read characters into the buffer until it is full
buffer_at = buffer;
while ( buffer_at < buffer_end )
*buffer_at++ = rfid.read();
// Reset buffer pointer so it's easy to read out
buffer_at = buffer;
// Signal that the buffer has data ready
ready = true;
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.