Thursday, April 16, 2015

USB Credit/Debit Card Reader On PLC (Programmable Logic Controller)




This application about how to use USB Credit/Debit Card Reader on PLC.
I use USB Universal Magnetic Stripe Credit/Debit Card Reader or Track Swipe Credit/Debit Card Reader (I use Read Only Track 2) with model HCE-402U.
Track format of magnetic stripe card is %track1?;track2?;track3?
I use Read only track 2 with data ;track2?
Example of track 2 data ;1233123311223=41123341133?
Example this application in industrial: access to production machine with credit/debit Card, and others.

Access to production machine with credit debit Card


Hardware required for this application
1. HCE-402U USB Universal Magnetic Stripe Credit/Debit Card Reader
2. Arduino UNO
3. Arduino USB Host Shield
4. Serial Port TTL to RS232 male
5. RS232 PPI Multi Master PLC Programming Cable for Siemens S7 200
6. Siemens S7-200 PLC
7. Adaptor DC9V

Hardware of Credit Debit Card Reader On PLC


Hardware Connections

Hardware Connections of Credit Debit Card Reader On PLC


Download Project File for Credit/Debit Card Reader On PLC:
1. Arduino Project file, click here
for folder SimpleModbusMaster and USBHostShield2 in folder Arduino libraries copy-paste into folder C:\Program Files (x86)\Arduino\libraries
2. PLC Ladder Programming project file, click here

Credit/Debit Card Number Setting in PLC Ladder Programming:

Credit Debit Card Number Setting in PLC Ladder Programming


Video Test:




Arduino Code for Credit/Debit Card Reader on PLC via Modbus
#include <hid.h>
#include <hiduniversal.h>
#include <usbhub.h>
#include <avr/pgmspace.h>
#include <Usb.h>
#include <usbhub.h>
#include <avr/pgmspace.h>
#include <hidboot.h>

#include <SimpleModbusMaster.h> 
//Arduino Serial Port Connect to Port 0 of Siemens PLC S7 200
#define slaveAddr 1
#define baud 9600
#define timeout 1000
#define polling 200
#define retry_count 0
#define TxEnablePin 2 
enum
{
  PACKET1,
  TOTAL_NO_OF_PACKETS
};
Packet packets[TOTAL_NO_OF_PACKETS];
packetPointer packet1 = &packets[PACKET1];
#define Char_Max 40
unsigned int writeRegs[Char_Max];
static uint32_t update_time = 0;  
unsigned int order_inc=0;
byte byte0=0;
unsigned int data=0;

USB     Usb;
USBHub     Hub(&Usb);                                          
HIDUniversal      Hid(&Usb);                                  
HIDBoot<HID_PROTOCOL_KEYBOARD>    Keyboard(&Usb);
 
class KbdRptParser : public KeyboardReportParser
{
        void PrintKey(uint8_t mod, uint8_t key);
protected:
 virtual void OnKeyDown (uint8_t mod, uint8_t key);
 virtual void OnKeyPressed(uint8_t key);
};
 
void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key) 
{
    uint8_t c = OemToAscii(mod, key);
 
    if (c)
        OnKeyPressed(c);
}
 

void KbdRptParser::OnKeyPressed(uint8_t key) 
{
static uint32_t next_time = 0;    
static uint8_t index_array = 0;
static uint8_t count = 0;

    update_time = millis() + 200;
    
    if( millis() > next_time ) {
      memset(writeRegs,0,sizeof(writeRegs));
      index_array = 0;
      count = 0;
      order_inc++;
      if (order_inc>32767)order_inc=0;
      writeRegs[0] = (word) order_inc;
    }
    
    next_time = millis() + 200;   
    count++;
    if ((count%2)==0){
    data=byte0<<8;
    data +=key;  
    index_array++; 
    if (index_array<Char_Max)writeRegs[index_array] = (word) data;
    }else{
      byte0=key;
    }
};
 
KbdRptParser Prs;
 
void setup()
{
    modbus_construct(packet1, slaveAddr, PRESET_MULTIPLE_REGISTERS, 0, 40, writeRegs);
    modbus_configure(&Serial, baud, SERIAL_8E1, timeout, polling, retry_count, TxEnablePin, packets, TOTAL_NO_OF_PACKETS);  
    if (Usb.Init() == -1) {
        //Error
        while(1){}
    }
 
    delay( 200 );
 
    Hid.SetReportParser(0, (HIDReportParser*)&Prs);
    delay( 200 );
}
 
void loop()
{
  Usb.Task();
  if( millis() > update_time ) modbus_update(); 
}


Labels:






Newer Post Older Post Home

You may also like these ebook:

Get Free PLC eBook directly sent to your email,
and email subscription to program-plc.blogspot.com




We hate SPAM. Your information is never sold or shared with anyone.

Your Email Will Be 100% Secured !

Your email is stored safely on Google FeedBurner