Another Magnetic Card Reader - Serial Output

This is my first useful project since my Arduino GPS a while back >>> http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1267027090

I saw some magnetic card readers around but nothing that had arduino code to go along with it. This reader is for track 2 and whatever the common standard for that track is. The current code prints the data on track 2, minus the start and end sentinel, serially so it can be viewed with the serial monitor. Here is an example of a credit card read: 1234123412341234=04121010000000123. The first field is the credit card number, the second field starts with the expiration in DD/MM format, so in this case, December 4. I don’t know what the data at the very end is however. If anyone has an idea, let me know!

Parts:

  1. Arduino
  2. Magtek 21045002 Card reader

The data pin is connected to pin 12
The strobe/clock pin is connected to pin 11
The card present pin is connected to pin 10

All these fields are easily modified with the "define"s at the beginning of the program along with the serial speed.

First it resets all values pertaining to the data containers so it can read more than one card without the need for a reset. Then it loops in the “waitForCard()” function until the CARDPRESENT pin goes low. It then reads the data while storing it in the “dataStr” string for later processing. Then the data is processed and converted to characters and prints them. Anytime a byte read doesn’t match one of the standard byte formats it declares an “*” in its place.

I think this program will be especially beneficial to those trying to interface a magnetic card reader and a computer program since data can be read through the serial port. I don’t really know where I’ll take it from here but I plan on making a card spoofer sometime in the near future.

It can also be easily modified to print to an LCD.

#define DATA 12
#define STROBE 11
#define CARDPRESENT 10
#define SERIALRATE 9600

boolean dataStr[400];
boolean check;
int dataNum;
boolean SSfound;
boolean ESfound;
char currentByte[6];
int i;


void setup() {
  Serial.begin(SERIALRATE);
  pinMode(DATA, INPUT);
  pinMode(STROBE, INPUT);
  pinMode(CARDPRESENT, INPUT);
}

void loop() {
  resetVals();
  waitForCard();
  readData();
  processData();
}

//*******************************************************

void resetVals() {
  dataNum = 0;
  check = 0;
  SSfound = 0;
  ESfound = 0;
}

//*******************************************************
 
void waitForCard() {
  while (true) {
    if (digitalRead(CARDPRESENT) == LOW) {
      break;
    }
  }
}

//*******************************************************

void readData() {
  while (true) {
    if (digitalRead(STROBE) == LOW && check == 1) {
      if (digitalRead(DATA) == HIGH) {
        dataStr[dataNum] = '0';
        dataNum++;
      }
      else {
        dataStr[dataNum] = '1';
        dataNum++;
      }
      check = 0;
    }
    else if (check == 0 && digitalRead(STROBE) == HIGH) {
      check = 1;
    }
    if (digitalRead(CARDPRESENT) == HIGH) {
      break;
    }
  }
}

//*******************************************************

void processData() {
  findSS();
  while (i < dataNum && ESfound == 0) {
    for (int x = 0; x < 5; x++) {
      currentByte[x] = dataStr[i];
      ++i;
    }
    convertByte();
  }
  Serial.println();
}  
  
void findSS() {
  while (SSfound == 0) {
    for (i = 0; i < dataNum; i++) {
      if (dataStr[i] == '1') {
        ++i;
        if (dataStr[i] == '1') {
          ++i;
          if (dataStr[i] == '0') {
            ++i;
            if (dataStr[i] == '1') {
              ++i;
              if (dataStr[i] == '0') {
                ++i;
                SSfound = 1;
                break;
              }
            }
          }
        }
      }
    }
  }
}

//*******************************************************

void convertByte() {
  if (strcmp(currentByte, "00001") == 0) {
    Serial.print("0");
  }
  else if (strcmp(currentByte, "10000") == 0) {
    Serial.print("1");
  }
  else if (strcmp(currentByte, "01000") == 0) {
    Serial.print("2");
  }
  else if (strcmp(currentByte, "11001") == 0) {
    Serial.print("3");
  }
  else if (strcmp(currentByte, "00100") == 0) {
    Serial.print("4");
  }
  else if (strcmp(currentByte, "10101") == 0) {
    Serial.print("5");
  }
  else if (strcmp(currentByte, "01101") == 0) {
    Serial.print("6");
  }
  else if (strcmp(currentByte, "11100") == 0) {
    Serial.print("7");
  }
  else if (strcmp(currentByte, "00010") == 0) {
    Serial.print("8");
  }
  else if (strcmp(currentByte, "10011") == 0) {
    Serial.print("9");
  }
  else if (strcmp(currentByte, "01011") == 0) {
    Serial.print(":");
  }
  else if (strcmp(currentByte, "00111") == 0) {
    Serial.print("<");
  }
  else if (strcmp(currentByte, "01110") == 0) {
    Serial.print(">");
  }
  else if (strcmp(currentByte, "10110") == 0) {
    Serial.print("=");
  }
  else if (strcmp(currentByte, "11111") == 0) {
    ESfound = 1;
  }
  else {
    Serial.print("*");
  }
}