Last active
August 29, 2015 14:05
-
-
Save sdbakker/72ea70e02b06ac2cf69f to your computer and use it in GitHub Desktop.
WdKA RFID Workshop StrongLink SL030 RFID reader sample gist
This file contains 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
/* | |
* @title: WdKA RFID Workshop StrongLink SL030 RFID reader sample gist | |
* @author: Simon de Bakker <simon@simbits.nl> | |
* @see: http://www.stronglink.cn/english/sl030.htm | |
* | |
* Mapping from Arduino UNO to Stronglink SL030: | |
* | |
* Arduino UNO - SL030 | |
* 3V3 - VCC 1 | |
* A4 - SDA 3 | |
* A5 - SCL 4 | |
* 8 - OUT 5 | |
* GND - GND 6 | |
* | |
*/ | |
#include <Wire.h> | |
#define DEBUG 1 | |
#define CARD_PRESENCE_PIN 8 | |
#define READER_I2C_ADDR 0x50 | |
#define CMD_SELECT_MIFARE_CARD 0x01 | |
#define MAX_UID_BYTES 0x07 | |
#define SUCCESS 0x00 | |
#define NO_TAG 0x01 | |
#define ERR_NO_TAG -1 | |
#define ERR_WRONG_CMD -2 | |
#define ERR_READING_TAG -3 | |
#define ERR_LAST ERR_READING_TAG | |
char * ErrorStrings[] = { | |
"Unknown error", | |
"No TAG found", | |
"Wrong command", | |
"Error reading tag" | |
}; | |
char * CardTypes[] = { | |
"", | |
"Mifare 1k, 4 byte UID", | |
"Mifare 1k, 7 byte UID", | |
"Mifare UltraLight or NATG203, 7 byte UID", | |
"Mifare 4k, 4 byte UID", | |
"Mifare 4k, 7 byte UID", | |
"Mifare DesFire, 7 byte UID", | |
"", "", "", | |
"Other", | |
}; | |
boolean cardDetected = false; | |
char * error_to_string(int errno) | |
{ | |
if (errno >= 0 || errno < ERR_LAST) | |
return ErrorStrings[0]; | |
return ErrorStrings[errno * -1]; | |
} | |
void setup() | |
{ | |
Wire.begin(); | |
Serial.begin(9600); | |
pinMode(CARD_PRESENCE_PIN, INPUT); | |
pinMode(13, OUTPUT); | |
} | |
void send_select_mifare_cmd() | |
{ | |
Wire.beginTransmission(READER_I2C_ADDR); | |
Wire.write(1); | |
Wire.write(CMD_SELECT_MIFARE_CARD); | |
Wire.endTransmission(); | |
} | |
int read_mifare_uid(unsigned char *uid, int *n) | |
{ | |
unsigned char buf[11]; | |
int received = 0; | |
int len; | |
int cmd; | |
int type; | |
int uidlen; | |
Wire.requestFrom(READER_I2C_ADDR, 11, true); | |
while (Wire.available() && received < 11) { | |
unsigned char c = Wire.read(); | |
buf[received++] = c; | |
#if defined(DEBUG) | |
Serial.print(" 0x"); | |
Serial.print(c, HEX); | |
#endif | |
} | |
#if defined(DEBUG) | |
Serial.println(""); | |
#endif | |
len = buf[0]; | |
if (len > 2) { | |
cmd = buf[1]; | |
if (cmd == CMD_SELECT_MIFARE_CARD) { | |
if (buf[2] == SUCCESS) { | |
uidlen = len - 3; | |
for (int i=0; i<uidlen; i++) { | |
uid[i] = buf[3+i]; | |
} | |
type = buf[len]; | |
} else { | |
return ERR_NO_TAG; | |
} | |
} else { | |
return ERR_WRONG_CMD; | |
} | |
} else { | |
return ERR_READING_TAG; | |
} | |
*n = uidlen; | |
return type; | |
} | |
void loop() | |
{ | |
boolean cardPresent = !digitalRead(CARD_PRESENCE_PIN); | |
if (!cardDetected && cardPresent) { | |
unsigned char UID[MAX_UID_BYTES]; | |
int bytes; | |
int type; | |
digitalWrite(13, HIGH); | |
send_select_mifare_cmd(); | |
delay(100); | |
type = read_mifare_uid(UID, &bytes); | |
digitalWrite(13, LOW); | |
if (type > 0) { | |
#if defined(DEBUG) | |
Serial.print("card type: "); | |
Serial.println(CardTypes[type]); | |
Serial.print("UID: "); | |
for (int i=0; i<bytes; i++) { | |
Serial.print(UID[i], HEX); | |
} | |
Serial.println(""); | |
#else | |
for (int i=0; i<bytes; i++) { | |
Serial.print(UID[i], DEC); | |
} | |
Serial.print('\n'); | |
#endif | |
cardDetected = true; | |
} else { | |
#if defined(DEBUG) | |
Serial.println(error_to_string(type)); | |
#endif | |
} | |
} else if (!cardPresent) { | |
cardDetected = false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment