현재 가지고 있는 리더기는 TTL 인터페이스를 가지고 있는 single track reader이다. 5핀 커넥터를 가지고 있고 Vcc, GND, Card present, Clock(Strobe), Data 신호를 제공해 준다.
신호 타이밍은 아래와 같다.
Arduino의 D2, D3, D5를 각각 리더기의 Data, Clock, Card present에 연결해 주면 된다.
코드는 다음과 같다.
/*
* Magnetic Stripe Reader
* by Stephan King http://www.kingsdesign.com
*
* Reads a magnetic stripe.
*
*/
int cld1Pin = 5; // Card status pin
int rdtPin = 2; // Data pin
int reading = 0; // Reading status
volatile int buffer[400]; // Buffer for data
volatile int i = 0; // Buffer counter
volatile int bit = 0; // global bita
char cardData[40]; // holds card info
int charCount = 0; // counter for info
int DEBUG = 0;
void setup() {
Serial.begin(38400);
// The interrupts are key to reliable
// reading of the clock and data feed
attachInterrupt(0, changeBit, CHANGE);
attachInterrupt(1, writeBit, FALLING);
}
void loop(){
// Active when card present
while(digitalRead(cld1Pin) == LOW){
reading = 1;
}
// Active when read is complete
// Reset the buffer
if(reading == 1) {
if (DEBUG == 1) {
printBuffer();
}
decode();
reading = 0;
i = 0;
int l;
for (l = 0; l < 40; l = l + 1) {
cardData[l] = '\n';
}
charCount = 0;
}
}
// Flips the global bit
void changeBit(){
if (bit == 0) {
bit = 1;
} else {
bit = 0;
}
}
// Writes the bit to the buffer
void writeBit(){
buffer[i] = bit;
i++;
}
// prints the buffer
void printBuffer(){
int j;
for (j = 0; j < 200; j = j + 1) {
Serial.println(buffer[j]);
}
}
int getStartSentinal(){
int j;
int queue[5];
int sentinal = 0;
Serial.print("Stripe_Data:");
for (k = 0; k < charCount; k = k + 1) {
Serial.print(cardData[k]);
}
Serial.println("");
}
void printMyByte(int thisByte[]) {
int i;
for (i = 0; i < 5; i = i + 1) {
if (DEBUG == 1) {
Serial.print(thisByte[i]);
}
}
if (DEBUG == 1) {
Serial.print("\t");
Serial.print(decodeByte(thisByte));
Serial.println("");
}
댓글을 달아 주세요