Sunday, December 13, 2015

USB Smart Card Reader and Allen-Bradley PLC with bridged by Arduino UNO plus USB host Shield




How do I read a GSM SIM card CCID to Allen-Bradley PLC?
This application about USB Smart Card Reader and Allen-Bradley PLC with bridged by Arduino.
Communication between USB Smart Card Reader and Arduino using USB communication and then communication between Arduino and Allen-Bradley PLC using RS232 DF1 protocol communication.
I use MicroLogix 1000 for Allen-Bradley PLC and Arduino UNO plus USB host Shield.
Figure below describes this application:

USB Smart Card Reader and Allen-Bradley PLC


Video demonstration:

USB Smart Card Reader Connect to Allen-Bradley PLC through Arduino




Hardware for USB Smart Card Reader and Allen-Bradley PLC

  1. Allen-Bradley PLC , I use MicroLogix 1000
  2. USB Smart Card Reader, I use RTS5169 Realtek
  3. Arduino UNO
  4. Arduino USB host Shield
  5. TTL to RS232 Module
  6. RS232 PLC Cable for MicroLogix 1000
  7. Adapter Arduino / Power Supply for Arduino
  8. GSM SIM card

Hardware for USB Smart Card Reader and Allen-Bradley PLC



Hardware Connections for USB Smart Card Reader and Allen-Bradley PLC

A. Arduino to Allen-Bradley PLC


Hardware Connection Between Allen Bradley PLC and Arduino


B. Arduino to USB Smart Card Reader

USB Host Shield to Arduino UNO and USB Smart Card Reader to Port USB of USB Host Shield


Software for USB Smart Card Reader and Allen-Bradley PLC

  1. Arduino Software (IDE), I use Arduino 1.6.6 Hourly Build
  2. Arduino Libraries , click here


Project File for USB Smart Card Reader and Allen-Bradley PLC

  1. Arduino Project File, click here
  2. PLC Ladder Programming for MicroLogix 1000, click here

GSM SIM Card Reader Software

GSM SIM Card Reader Software Download, click here

GSM SIM Card Reader Software


Setting for GSM SIM card CCID Number on PLC Ladder Programming

  1. GSM CCID Number in N7:0 to N7:9
  2. Example:

  3. I use GSM CCID : 89628950000417377480

    separated per 2-digits : 89 - 62 - 89 - 50 - 00 - 04 - 17 - 37 - 74 - 80

    N7:0 = 89
    N7:1 = 62
    N7:2 = 89
    N7:3 = 50
    N7:4 = 00
    N7:5 = 04
    N7:6 = 17
    N7:7 = 37
    N7:8 = 74
    N7:9 = 80

  4. Set value to Ladder Programming:
Setting for GSM SIM card CCID Number on PLC Ladder Programming


Arduino Code for USB Smart Card Reader and Allen-Bradley PLC

#include <usbhub.h>
#include <DF1.h>

#define PLCdestination 1
#define PLCbaud 9600
#define PLCformat SERIAL_8N1
#define PLCtimeout 1000

USB Usb;
USB_DEVICE_DESCRIPTOR buf;
uint8_t addr;
uint8_t rcode;
uint8_t PLC_State;
unsigned long next_timeout;
bool Smart_Card_Reader_USB_Run = false;
bool Get_CCID_Number=false;
uint8_t CCID_Number[10];

uint8_t param1[10] =  {0x62,0x00,0x00,0x00,0x00,0x00,0xF1,0x03,0x00,0x00};
uint8_t param2[15] =  {0x61,0x05,0x00,0x00,0x00,0x00,0xF2,0x00,0x00,0x00,0x11,0x00,0x00,0x0A,0x00};
uint8_t param3[17] =  {0x6F,0x07,0x00,0x00,0x00,0x00,0xF3,0x00,0x00,0x00,0xA0,0xA4,0x00,0x00,0x02,0x2F,0xE2};
uint8_t param4[15] =  {0x6F,0x05,0x00,0x00,0x00,0x00,0xF4,0x00,0x00,0x00,0xA0,0xB0,0x00,0x00,0x0A};


enum
{
  PLCPACKET1,
  PLCPACKET2,
  PLCTOTAL_NO_OF_PACKETS
};
DF1Packet PLCpackets[PLCTOTAL_NO_OF_PACKETS];

DF1packetPointer PLCpacket1 = &PLCpackets[PLCPACKET1];
DF1packetPointer PLCpacket2 = &PLCpackets[PLCPACKET2];

unsigned int PLCwriteRegs[10];


                             
void setup() {
    if (Usb.Init() == -1){
      //OSC did not start
      //smart card reader not recognized
      while(1);   
    }

  randomSeed(analogRead(0));
  PLC_State=0;
  Get_CCID_Number=false;

DF1_construct(PLCpacket2, PLCdestination, DF1_WRITE_N7, 0, 10, PLCwriteRegs);

DF1_configure(&Serial, PLCbaud, PLCformat, PLCtimeout, PLCpackets, PLCTOTAL_NO_OF_PACKETS);
    
}

void loop() {
  DF1_update();
  Usb.Task();
  if( Usb.getUsbTaskState() == USB_STATE_RUNNING )
  {
  Smart_Card_Reader_USB_Run = true;
  if((millis() > next_timeout) && PLC_State>1)PLC_State=1;

            switch(PLC_State) {
                    case 0: // USB Smart Card Reader Find 
                            rcode = USB_Smart_Card_Reader_Configuration();
                            if(rcode){
                              //smart card reader not detected
                              while(1);
                            }else{
                              PLC_State=1;
                            }
                            break;

                            
                      case 1: // GSM SIM Card Reader Detect
                            rcode = GSM_SIM_Card_Reader ();
                            if (rcode==0x02){ //smart card reader not detecting card
                              Get_CCID_Number=false;
                               for(uint8_t i=0;i<10;i++){ 
                                CCID_Number[i] = 0;         
                                PLCwriteRegs[i]= CCID_Number[i];  
                               }
                            }
                            
                            if (rcode==0x03){ //smart card reader detecting card
                              if(Get_CCID_Number==false){
                                next_timeout= millis() + 1000;
                                PLC_State=2;
                              }
                            }

                            break;

                      case 2 :
                            rcode =  Smart_Card_Reader_CCID(sizeof(param1),param1,0x80,0x0B,false);
                            if(rcode==0){
                              PLC_State=3;                                                           
                            }
                          break;


                        case 3 :
                            rcode =  Smart_Card_Reader_CCID(sizeof(param2),param2,0x82,0x05,false);
                            if(rcode==0){
                              PLC_State=4;                                                           
                            }
                          break;

                        case 4 :
                            rcode =  Smart_Card_Reader_CCID(sizeof(param3),param3,0x80,0x02,false);
                            if(rcode==0){
                              PLC_State=5;                                                           
                            }
                          break;

                        case 5 :
                            rcode =  Smart_Card_Reader_CCID(sizeof(param4),param4,0x80,0x0C,true);
                            if(rcode==0){
                              PLC_State=6;                                                           
                            }
                          break;

                          
                    case 6 :
                           for(uint8_t i=0;i<10;i++){          
                              PLCwriteRegs[i]= CCID_Number[i];  
                           }
                            Get_CCID_Number=true;                            
                            PLC_State=1;                                                          
                          break;
                          
                             
            }
            
  }else{
    if(Smart_Card_Reader_USB_Run){
        //smart card reader error
        asm volatile ("  jmp 0"); 
    }
  }

}


void Smart_Card_Reader_GetAddresses(UsbDevice *pdev)
{
    UsbDeviceAddress adr;
    adr.devAddress = pdev->address.devAddress;
    addr = adr.devAddress;
}

uint8_t USB_Smart_Card_Reader_Configuration() {
  uint8_t rcode;
  Usb.ForEachUsbDevice(&Smart_Card_Reader_GetAddresses);
  rcode = Usb.getDevDescr(addr, 0, sizeof (USB_DEVICE_DESCRIPTOR), (uint8_t*) & buf);
  if (rcode) {
    return (rcode);
  }else{
    rcode = Usb.setConf(addr, 0, buf.bNumConfigurations);
    return (rcode);            
  }        
  return (USB_STATE_ERROR);
}

uint8_t GSM_SIM_Card_Reader (){
  uint8_t rcode;
  uint8_t  buf[64];
  uint16_t rcvd=0;
  rcode = BULK_IN(0x43,&rcvd, buf, 1);
  if(rcode)
    return rcode; 

  if (rcvd==2){
    if(buf[0]==0x50){
      return buf[1];
    }
  }
    
return (1);
}



uint8_t Smart_Card_Reader_CCID(uint16_t nbytes, uint8_t* data,uint8_t cdata0, uint8_t cdata1,bool get_ccid) {
 uint8_t rcode;
 rcode =  BULK_OUT(0x25,nbytes,data);
 if(rcode)
    return rcode;

  uint8_t  buf[64];
  uint16_t rcvd=0;
  rcode = BULK_IN(0x46,&rcvd, buf, 10);
  if(rcode)
    return rcode;  

  if (rcvd>10){    
    if(buf[0]==cdata0 && buf[1]==cdata1 && buf[6]==data[6] && buf[8]==0x80){
      if(get_ccid){
          for(uint8_t i=0;i<sizeof(CCID_Number);i++){  
            uint8_t v1 = (buf[i+10] & 0x0F)*10;
            uint8_t v0 = (buf[i+10]>>4)& 0x0F;  
            CCID_Number[i]= v0 +v1;
          }
      }
      return (0);
    }
  }


return (1);
}



uint8_t BULK_OUT(uint8_t vHXFR,uint16_t nbytes, uint8_t* data) {
    Usb.bytesWr(rSNDFIFO, nbytes, data);
    Usb.regWr(rSNDBC, nbytes);
    Usb.regWr(rHXFR, vHXFR);
    while(!(Usb.regRd(rHIRQ) & bmHXFRDNIRQ)); //wait for the completion IRQ
    Usb.regWr(rHIRQ, bmHXFRDNIRQ); //clear IRQ             
  return (0);
}


uint8_t BULK_IN(uint8_t vHXFR,uint16_t *pktsize, uint8_t* data, unsigned long timeout) {
  unsigned long timeout_start = millis() + timeout;
    
  while((long)(millis() - timeout_start) < 0L) {
    Usb.regWr(rHXFR, vHXFR);
      if((Usb.regRd(rHIRQ) & bmRCVDAVIRQ)==bmRCVDAVIRQ){
      uint16_t buff_pktsize = Usb.regRd(rRCVBC);
      *pktsize = buff_pktsize;
      data = Usb.bytesRd(rRCVFIFO, buff_pktsize, data);
      Usb.regWr(rHIRQ, bmRCVDAVIRQ); // Clear the IRQ & free the buffer     
      return (0);   
      }   
   }      
             
  return (1);
}


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