Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.

Thomas

Member Since 04 Aug 2010
Offline Last Active May 16 2012 10:29 AM
-----

Topics I've Started

MagStripe Reader with Netduino

08 January 2011 - 04:50 PM

Hi,

I have been playing around with a Mag Stripe reader (the one I use only reads track 2), but at the moment it only works when I swipe the card very slowly through the reader.

The outputs from the reader is:

- Card Present
- Clock
- Data

When a card is swiped, the Card Present will first go low, and the clock wil start switching high and low, and when the clock goes from high to low, you are suppose to sample the data line, which becomes stable a short while before and keeps stable as long as the clock is low.

The clock is not constant, but depends on how fast you swipe the card. Swiping the card slowly results in something like 500 Hz, going about normal speed you will get a clock speed of roughly 1 to 1.5 kHz and swiping it fast will probably be 2-4 kHz.

I wrote some code (included below) which seemd to work when swiping the card slowly, but when the speeds get to what most people would do, it won't read the values correctly.

Am I using the InterruptPort correctly?

How long does the InputPort.Read() method take to read the pin state?

In the code below, I just put a breakpoint at the end of cp_OnInterrupt and look at the temp variable to see what was read.

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;

namespace MagStripeReader
{
    public class Program
    {
        public static byte[] data = new byte[1024];

        public static int count = 0;

        public static InputPort dataPin = new InputPort(Pins.GPIO_PIN_D6,
                                     false,
                                     Port.ResistorMode.PullUp);

        public static void Main()
        {
            // write your code here
            InterruptPort clk = new InterruptPort(Pins.GPIO_PIN_D10,
                                     false,
                                     Port.ResistorMode.PullUp,
                                     Port.InterruptMode.InterruptEdgeLow);
            clk.OnInterrupt += new NativeEventHandler(clk_OnInterrupt);


            InterruptPort cp = new InterruptPort(Pins.GPIO_PIN_D5,
                                     false,
                                     Port.ResistorMode.PullUp,
                                     Port.InterruptMode.InterruptEdgeHigh);
            cp.OnInterrupt += new NativeEventHandler(cp_OnInterrupt);

            Thread.Sleep(Timeout.Infinite); 
        }

        static void cp_OnInterrupt(uint data1, uint data2, DateTime time)
        {
            bool inStartZone = true;
            bool inEndZone = false;

            byte[] temp = new byte[1024];
            int charCounter = 0;
            int bitCounter = 0;

            for (int i = 0; i < count; i++)
            {
                if (inStartZone && data[i] == 1)
                {

                }
                else if (inStartZone == false)
                {
                    bitCounter = 0;
                    if (data[i+0] == 0)
                    {
                        temp[charCounter] |= (byte)(1 << bitCounter);
                    }
                    bitCounter++;
                    if (data[i + 1] == 0)
                    {
                        temp[charCounter] |= (byte)(1 << bitCounter);
                    }
                    bitCounter++;
                    if (data[i + 2] == 0)
                    {
                        temp[charCounter] |= (byte)(1 << bitCounter);
                    }
                    bitCounter++;
                    if (data[i + 3] == 0)
                    {
                        temp[charCounter] |= (byte)(1 << bitCounter);
                    }
                    bitCounter++;

                    charCounter++;
                    i += 4;
                }
                else
                {
                    i--;
                    inStartZone = false;
                }
            }
            count = 0;
            return;
        }

        static void clk_OnInterrupt(uint data1, uint data2, DateTime time)
        {
            data[count] = 0;
            if (dataPin.Read())
            {
                data[count] = 1;
            }
            count++;
        }

    }
}

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.