Thursday, January 21, 2016

RFID Application for Mitsubishi PLC FX using RFID USB Reader And Arduino




This article about how to use RFID application for Mitsubishi PLC FX.
RFID USB reader connect to Arduino USB Host and then connect to Mitsubishi PLC FX.
in this RFID application use RFID tags with number of ID are 10 digits.

RFID Application for Mitsubishi PLC FX


Video demonstration:

Mitsubishi PLC FX Series Communication with RFID USB Reader using Arduino USB Host



Hardware for RFID application

  1. RFID USB Reader
  2. Arduino USB Host Shield
  3. Arduino UNO
  4. Mitsubishi PLC FX1S
  5. Mitsubishi FX PLC Cable
  6. TTL to Male RS232 Module
  7. Adaptor for Arduino
  8. 3 pieces Light for testing
  9. 3 pieces RFID Tags for testing

Hardware of Mitsubishi FX PLC RFID Application


Hardware connections

1. Mitsubishi PLC FX ,  Arduino UNO and Arduino USB Host Shield

Mitsubishi PLC FX and Arduino USB Host


Arduino Software

  1. I use Arduino software version 1.6.6


Arduino Library

  1. Arduino Library for RFID Application : click here


Download Project File

  1. Arduino UNO project file : click here
  2. Mitsubishi PLC FX1S Project File, NO PROJECT FILE
  3. I use YouTube Video Tutorial : click How to Make Mitsubishi PLC Programming for RFID Application


How to Make Mitsubishi PLC Programming for RFID Application



Arduino Code for RFID Application

#include <usbhub.h>
#include <FX1S.h>
//by program-plc.blogspot.com
#define FX1Sbaud 19200
#define FX1Sformat SERIAL_8N1
#define FX1Stimeout 1000


enum
{
  FX1SPACKET1,
  FX1STOTAL_NO_OF_PACKETS
};
FX1SPacket FX1Spackets[FX1STOTAL_NO_OF_PACKETS];

FX1SpacketPointer FX1Spacket1 = &FX1Spackets[FX1SPACKET1];


unsigned int RFID_FX1SwriteD[3];

unsigned int RFID_USB_Reader_Number[10];
unsigned int FX1Ssuccessful;
unsigned long FX1Ssuccessful_timeout;

USB Usb;
USB_DEVICE_DESCRIPTOR buf;
uint8_t addr;
uint8_t rcode;
uint8_t PLC_State;
bool RFID_USB_Reader_RUN = false;
uint8_t rfid_inc;
  
void setup() {
    if (Usb.Init() == -1){
      //RFID USB Reader did not start
      while(1);   
    }
 
  FX1S_construct(FX1Spacket1, FX1S_WRITE_D, 0, 3, RFID_FX1SwriteD);

  FX1S_configure(&Serial, FX1Sbaud, FX1Sformat, FX1Stimeout, FX1Spackets, FX1STOTAL_NO_OF_PACKETS);

  PLC_State=0;
  rfid_inc=0;
}

void loop() {
  uint8_t  buf[64];
  uint16_t rcvd=0;
  Usb.Task();
  if( Usb.getUsbTaskState() == USB_STATE_RUNNING )
  {
  RFID_USB_Reader_RUN = true;

            switch(PLC_State) {
                    case 0:  // RFID USB Reader Configuration                  
                            rcode = RFID_USB_Reader_Configuration();
                            if(rcode){
                              asm volatile ("  jmp 0");
                            }else{
                              PLC_State=1;
                              rfid_inc=0;
                            }
                            break;

                            
                      case 1: // RFID Key Tags Detect                                      
                      rcode = RFID_USB_Reader_BULK_IN(0x01,&rcvd, buf, 1);
                      if(rcode ==0){//1
                        if(rcvd==8){//2
                          if(buf[2]!=0){//3
                            if(30<=buf[2] && buf[2]<=39){//4
                              if(rfid_inc<10){//5
                                RFID_USB_Reader_Number[rfid_inc] =  RFID_USB_Reader_Keycode_to_Number (buf[2]);
                                rfid_inc++;  
                              }else{//5
                                PLC_State=2;
                                FX1S_update();
                                FX1Ssuccessful = FX1Spacket1->FX1Ssuccessful_requests;                                
                                FX1Ssuccessful_timeout = millis() + 2000;
                              }//5
                            }//4
                            if(buf[2]==40){//6
                              PLC_State=2;
                              FX1S_update();
                              FX1Ssuccessful = FX1Spacket1->FX1Ssuccessful_requests;                              
                              FX1Ssuccessful_timeout = millis() + 2000;
                            }//6
                          }//3
                        }//2
                      }//1                  
                      break;

                    case 2: //RFID USB Reader Number and Send to Mitsubishi PLC FX
                      //Reduce from 10 memory to 3 memory
                      //0011487710 RFID Key Tag 1
                      // 00-1148-7710 = D0-D1-D2
                      //0008997168 RFID Key Tag 2
                      // 00-0899-7168 = D0-D1-D2
                      //0006216553 RFID Key Tag 3
                      // 00-0621-6553 = D0-D1-D2
                      RFID_FX1SwriteD[2] = (RFID_USB_Reader_Number[6]*1000 )+ (RFID_USB_Reader_Number[7]*100 ) + (RFID_USB_Reader_Number[8]*10 ) + RFID_USB_Reader_Number[9];
                      RFID_FX1SwriteD[1] = (RFID_USB_Reader_Number[2]*1000 )+ (RFID_USB_Reader_Number[3]*100 ) + (RFID_USB_Reader_Number[4]*10 ) + RFID_USB_Reader_Number[5];
                      RFID_FX1SwriteD[0] = (RFID_USB_Reader_Number[0]*10 ) + RFID_USB_Reader_Number[1];                    
                      FX1S_update();
                      if(FX1Spacket1->FX1Ssuccessful_requests!=FX1Ssuccessful){
                        PLC_State=1;
                        rfid_inc=0; 
                      }
                      if(millis() > FX1Ssuccessful_timeout){
                        PLC_State=1;
                        rfid_inc=0;   
                      }
                      break;
                                       
            }
            
  }else{
    if(RFID_USB_Reader_RUN){
      //RFID USB Reader Error
      asm volatile ("  jmp 0"); 
    }
  }


}


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

uint8_t RFID_USB_Reader_Configuration() {
  uint8_t rcode;
  Usb.ForEachUsbDevice(&RFID_USB_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 RFID_USB_Reader_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);
      return (0);   
      }   
   }      
             
  return (1);
}


byte RFID_USB_Reader_Keycode_to_Number (byte RFID_USB_Reader_Keycode){
  switch(RFID_USB_Reader_Keycode) {
     case 30:
        return 1;
     case 31:
        return 2;
     case 32:
        return 3;
     case 33:
        return 4;
     case 34:
        return 5;
     case 35:
        return 6;
     case 36:
        return 7;
     case 37:
        return 8;
     case 38:
        return 9;
     case 39:
        return 0;
     default:
        return RFID_USB_Reader_Keycode;
  }
}


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