Thursday, December 17, 2015

Preset Counter / Timer, Set Point, Parameter Setting using GSM SIM Card in the Allen-Bradley PLC




in this application about how to use read data from GSM SIM Card for preset counter/timer, set point /set value on temperature controller, parameter setting for servo and others.

Data stored on the GSM SIM Card up to 250 and the price is very cheap for GSM SIM card.

every employee who works on the same machine, the parameters of counter/timer can be changed without having to reprogram the PLC.

each replacement of production of the products on the same machine, the parameters can be changed without having to reprogram the PLC.

one GSM SIM Card for one product or one employee with different parameters.

See animation for preset timer with GSM SIM Card
Preset Counter Timer with GSM SIM Card

Video demonstration:

Preset Counter Timerusing GSM SIM Card in the Allen-Bradley PLC




Hardware, hardware connections and software

Same with article about USB Smart Card Reader and Allen-Bradley PLC


Project File for Preset Counter/Timer using GSM SIM Card in the Allen-Bradley PLC

  1. Arduino Project File, click here
  2. For Micrologix 1000 Allen-Bradley PLC, click here


Counter/Timer with Preset data from GSM SIM Card

In PLC Ladder programming, I use two counter with same value, there are C5:0 and C5:1, and then for Timer use T4:0.
Preset Counter from Phonebook index 1 and Preset Timer from Phonebook index 2.
See this image below:
Counter Timer In PLC Ladder programming


Preset Counter/Timer Change with SimEdit Software

A. GSM SIM Card 1 with Preset timer = 10

GSM SIM Card 1 with Preset timer 10


B. GSM SIM Card 2 with Preset timer = 20

GSM SIM Card 2 with Preset timer 20


Arduino Code for Read GSM SIM Card

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

#define DF1destination 1
#define DF1baud 9600
#define DF1format SERIAL_8N1
#define DF1timeout 1000


enum
{
  DF1PACKET1,
  DF1PACKET2,
  DF1TOTAL_NO_OF_PACKETS
};
DF1Packet DF1packets[DF1TOTAL_NO_OF_PACKETS];

DF1packetPointer DF1packet1 = &DF1packets[DF1PACKET1];
DF1packetPointer DF1packet2 = &DF1packets[DF1PACKET2];

unsigned int DF1writeRegs[28];
unsigned int DF1readRegs[2];


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;
uint8_t Inc_Order;
uint8_t PhoneBook_Index;


uint8_t Sparam1[10] =  {0x62,0x00,0x00,0x00,0x00,0x00,0x01,0x03,0x00,0x00};
uint8_t Rparam1[21] =  {0x80,0x0b,0x00,0x00,0x00,0x00,0x01,0x00,0x80,0x00,0x3b,0x18,0x11,0xc1,0x43,0x01,0x10,0x09,0x05,0x07,0x77};

uint8_t Sparam2[15] =  {0x61,0x05,0x00,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x11,0x00,0x00,0x0A,0x00};
uint8_t Rparam2[15] =  {0x82,0x05,0x00,0x00,0x00,0x00,0x02,0x00,0x80,0x00,0x11,0x00,0x00,0x0a,0x00};

uint8_t Sparam3[17] =  {0x6F,0x07,0x00,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0xA0,0xA4,0x00,0x00,0x02,0x7F,0x10};
uint8_t Rparam3[12] =  {0x80,0x02,0x00,0x00,0x00,0x00,0x03,0x00,0x80,0x00,0x9f,0x16};

uint8_t Sparam4[17] =  {0x6F,0x07,0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0xA0,0xA4,0x00,0x00,0x02,0x6F,0x3A};
uint8_t Rparam4[12] =  {0x80,0x02,0x00,0x00,0x00,0x00,0x04,0x00,0x80,0x00,0x9f,0x0f};

//index Sread[12]
uint8_t Sread[15] =  {0x6F,0x05,0x00,0x00,0x00,0x00,0x05,0x00,0x00,0x00,0xA0,0xB2,0x01,0x04,0x1E};
uint8_t Rread[42] =  {0x80,0x20,0x00,0x00,0x00,0x00,0x05,0x00,0x80,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x0B,0x81,0xF0,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x90,0x00};
//phone name Rread[10] ~ Rread[25]
//tel num Rread[28] ~ Rread[37]


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


  PLC_State=0;
  Inc_Order=0;

DF1_construct(DF1packet1, DF1destination, DF1_READ_N7, 0, 2, DF1readRegs);

DF1_construct(DF1packet2, DF1destination, DF1_WRITE_N7, 2, 28, DF1writeRegs);

DF1_configure(&Serial, DF1baud, DF1format, DF1timeout, DF1packets, DF1TOTAL_NO_OF_PACKETS);

PhoneBook_Index = 1;
}

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
                                PLC_State=1;
                                DF1writeRegs[1] = 0;
                            }
                            
                            if (rcode==0x03){ //smart card reader detecting card
                                next_timeout= millis() + 1500;                            
                                PLC_State=2;
                                DF1writeRegs[1] = 0;
                            }

                            break;

                      case 2 :
                            Sparam1[6]=Inc_Order;
                            rcode =  Smart_Card_Reader_PhoneBook_Read(sizeof(Sparam1),Sparam1,sizeof(Rparam1),Rparam1,Inc_Order);
                            if(rcode==0){
                              PLC_State=3;
                              Inc_Order++;                                                           
                            }
                          break;


                        case 3 :
                            Sparam2[6]=Inc_Order;
                            rcode =  Smart_Card_Reader_PhoneBook_Read(sizeof(Sparam2),Sparam2,sizeof(Rparam2),Rparam2,Inc_Order);
                            if(rcode==0){
                              PLC_State=4; 
                              Inc_Order++;                                                           
                            }
                          break;

                        case 4 :
                            Sparam3[6]=Inc_Order;
                            rcode =  Smart_Card_Reader_PhoneBook_Read(sizeof(Sparam3),Sparam3,sizeof(Rparam3),Rparam3,Inc_Order);
                            if(rcode==0){
                              PLC_State=5;
                              Inc_Order++;                                                            
                            }
                          break;

                        case 5 :
                            Sparam4[6]=Inc_Order;
                            rcode =  Smart_Card_Reader_PhoneBook_Read(sizeof(Sparam4),Sparam4,sizeof(Rparam4),Rparam4,Inc_Order);
                            if(rcode==0){
                              PLC_State=6;
                              Inc_Order++;                                                            
                            }
                          break;
                          
                        case 6 :                                                                                  
                                Sread[12] = PhoneBook_Index;
                                if(Sread[12]<1 || Sread[12]>250){
                                  PLC_State=1;  
                                  break;                         
                                }
                                Sread[6]=Inc_Order;
                                rcode =  Smart_Card_Reader_PhoneBook_Read(sizeof(Sread),Sread,sizeof(Rread),Rread,Inc_Order);
                                if(rcode==0){
                                  PLC_State=7;
                                  Inc_Order++;                                                            
                                }                                                        
                              break;
                          
                        case 7 :
                              next_timeout= millis() + 1000; 
                              rcode = GSM_SIM_Card_Reader ();
                              if (rcode==0x03){
                                if(1<=DF1readRegs[0] && DF1readRegs[0]<=250){
                                  if(PhoneBook_Index!=DF1readRegs[0]){
                                    PhoneBook_Index=DF1readRegs[0];
                                    PLC_State=6;  
                                  }
                                }
                              }
                              if (rcode==0x02){
                                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){
      DF1writeRegs[0] = buf[1];
      return buf[1];
    }
  }
    
return (1);
}



uint8_t Smart_Card_Reader_PhoneBook_Read(uint16_t nbytes, uint8_t* data,uint16_t ncbytes,uint8_t* cdata,uint16_t incbytes) {
 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, 50);
  if(rcode)
    return rcode;  
 
  if (rcvd==ncbytes){  
     cdata[6] = incbytes;
     if(data[0]== 0x6F && data[1]== 0x05){ //0

          if(buf[0]==0x80 && buf[1]==0x20 && buf[6]== cdata[6] && buf[8]==0x80 && buf[9]==0x00 && buf[27]==0x81 && buf[40]==0x90 && buf[41]==0x00){//1
              
              if(buf[26]==0xFF){//2 //No data  
                      
                DF1writeRegs[18]= -1;
                DF1writeRegs[1] = DF1readRegs[1];
                
              }else{ //2
                
                for(uint8_t i=2;i<18;i++){  
                  DF1writeRegs[i]= buf[i+8];    
                }
                
                uint8_t v1;
                uint8_t v0;
                unsigned int buffval[3] = {0,0,0};
                uint8_t index = 0;
                for(uint8_t i=38;i>=28;i--){//3
                  if(buf[i]!=0xFF){//4
                    
                    if((buf[i] & 0xF0)==0xF0){//5
                        v1 = 0;
                        v0 = buf[i] & 0x0F;
                        if((v0 & 0x0F)==0x0F)v0 = 0;    
                      }else{//5
                        v1 = (buf[i] & 0x0F)*10;
                        v0 = (buf[i]>>4)& 0x0F;
                      }//5
                        
                      if(index==0)buffval[index]= v0 + v1;
                      if(index==1)buffval[index]= (v0 + v1)*100;
                      if(index==2){//6
                        unsigned int buf3= (v0 + v1);
                        if(buf3>3)buf3=3;
                        buffval[index]= buf3*10000;
                      }//6
                      index++;
                      if(index==3)break;
                    }//4
                  }//3

                  unsigned int buf4= buffval[0] + buffval[1] + buffval[2];
                  if(buf4<0)buf4=0;
                  if(buf4>32767)buf4=32767;
                  DF1writeRegs[18] = buf4; 
                  DF1writeRegs[1] = DF1readRegs[1];
                  
                }//2

          }else{//1
            return (1);
          }//1
        
    }else{ //0
       for(uint8_t i=0;i<ncbytes;i++){          
          if(buf[i]!=cdata[i])return (1);
       }
    }//0
      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