Friday, February 4, 2011

Read from Magtek card swipe reader in HID mode using libusb-win

This post deals with reading card swipe information from a Magtek USB Swipe Reader when set in HID mode. I used Visual Studio C++ Express Edition for a windows development environment. I installed libusb-win32 from
Create a C++ project in Visual studio, include the usb.h file and link the project with libusb.lib. Both of these are included in the libusb package.

The following can be used to read the card information-

// magtekusbhidcardswipe.cpp : Defines the entry point for the console application.

#include "stdafx.h"
#include "usb.h"

int _tmain(int argc, _TCHAR* argv[])
    struct usb_bus *busses;
    struct usb_bus *bus;
    struct usb_device *dev;
    int c, i, res;
    char data[337];
    char idata[10];

    busses = usb_get_busses();
    for (bus = busses; bus; bus = bus->next) {

        for (dev = bus->devices; dev; dev = dev->next) {           
        if(dev->descriptor.idProduct == 2 && dev->descriptor.idVendor == 0x801) {
        printf("Product id: %04hx" ,dev->descriptor.idProduct);
        printf("Vendor id: %04hx \n" ,dev->descriptor.idVendor);
        usb_dev_handle *l_Handle = usb_open( dev);
        if( NULL == l_Handle ){
            printf( "usb_open(): no handle to device\n" );
        res = usb_claim_interface(l_Handle, 0);
        if(res  < -EBUSY) {
            printf("Device interface not available to be claimed! \n");
        if(res < -ENOMEM) {
            printf("Insufficient Memory! \n");
        printf( "\nPlease swipe your card\n",res );
        res = usb_interrupt_read(l_Handle, 0x81, data, 337, -1);
        printf( "\nusb_interrupt_read %d \n",res ); 
                //I am interested in only 10 characters in this range
        for(i=1;i<10;i++) {
            idata[++c] = data[i];
        printf( "\nMy data : %d\n",c);
        usb_release_interface(l_Handle, 0);            
    return 0;

Suggestions/improvements are welcome as always.


Anonymous said...

Like the look for your reader using Windows.
I have several rfid readers using hid.I want each reader to enter data into a separate position in MS Excel. Note tht all the readers have the same vid pid.Can the usb por and hub numbers be used?
Need some advice, totally new in coding. For a model railroad project.
Charles Harris
swchuck (at ) gmail .com

Вова said...

Hello, can you please help me? I've read your post about interfacing a USB card reader via libusb, but it doesn't work for me.
If you can, please contact me @ v s h a r g i n 9 5 (АТ) g m a i l (d0t) c o m

Headcase said...
This comment has been removed by the author.
Headcase said...
This comment has been removed by the author.
Spot Cash on Credit Card said...

It is a stunning post. Exceptionally valuable to me. I preferred it .Swipe Your Credit Card And have Immediate cash With Cheapest Costs. Provide Your ID Card Photo Copy As A Document Resistant.
Take a look to my site Credit Card Swipe For Cash in Chennai