Tuesday, December 22, 2015

Omron PLC USB - Raspberry Pi - RFID USB Reader




It's about the use of RFID application in Omron PLC USB with the help of Raspberry Pi.
Communication between Omron PLC and Raspberry Pi using USB communication,
And then communication between Raspberry Pi with RFID USB Reader using USB communication.
RFID tags read by the RFID USB reader and then sent to Omron PLC using python programming on Raspberry Pi.

Animation can be seen in the picture below:

Omron PLC RFID Application


Video Demonstration:

RFID USB Reader Connect to Omron PLC USB through Raspberry Pi 2




Hardware required

  1. Omron PLC with USB programming, I use CP1L-L10 Omron PLC
  2. Raspberry Pi 2
  3. Micro SD Card and Power Supply 5V for Raspberry Pi 2
  4. RFID USB Reader
  5. RFID tags


Hardware Connections



Hardware Connections Omron PLC Raspberry Pi RFID USB Reader


Project File

  1. PLC Project File (CP1L Omron PLC), click here
  2. Project File for Setup on Raspberry Pi, click here
  3. Follow the steps in the file : step by step for setup.txt


Setup on Raspberry Pi

how to setup on raspberry pi, check the link :
http://program-plc.blogspot.com/2015/12/omron-plc-usb-protocol-used-for.html


RFID setting on Omron PLC Ladder Programming

  1. Example RFID tag number : 0006216553 (10 digits)
  2. Split to : 00 - 0621 - 6553
  3. set to : D0 = 0 , D1 = 0621 , D2 = 6553

RFID setting on Omron PLC Ladder Programming


Python Programming on Raspberry Pi 2

RFID-USB-Reader.py

import usb.core
import usb.util
import sys


def RFID_Number(rfid):
    if rfid==30:return 1
    if rfid==31:return 2
    if rfid==32:return 3
    if rfid==33:return 4
    if rfid==34:return 5
    if rfid==35:return 6
    if rfid==36:return 7
    if rfid==37:return 8
    if rfid==38:return 9
    if rfid==39:return 0
    return rfid
    


def RFID_ID(rfid):
    rfid_data = []    
    for index in range(10):
       rfid_data.append(RFID_Number(rfid[index]))
    
    rfid_id_tag =[0,0,0]
    rfid_id_tag[2] = (rfid_data[6]*1000) + (rfid_data[7]*100) + (rfid_data[8]*10) + rfid_data[9]
    rfid_id_tag[1] = (rfid_data[2]*1000) + (rfid_data[3]*100) + (rfid_data[4]*10) + rfid_data[5]
    rfid_id_tag[0] = (rfid_data[0]*10) + rfid_data[1]

    return str(rfid_id_tag[0])+ " " + str(rfid_id_tag[1]) + " " + str(rfid_id_tag[2])



while True:    
    #RFID USB Reader
    dev2 = None
    endpoint = None
    dev2 = usb.core.find(idVendor=0x08ff, idProduct=0x0009)

    if dev2 is None:
        raise ValueError('RFID USB Reader Not Found')
    else:        
        try:
            dev2.set_configuration()
            print 'RFID USB Reader Ready'
        except usb.core.USBError as e:
            #print "ERROR 1 : ",e.strerror  
            if e.strerror.find("Device or resource busy")>=0:
                dev2.detach_kernel_driver(0)
                continue
            else:
                raise ValueError('RFID USB Reader Error 1')



        endpoint = dev2[0][(0,0)][0]
        #print endpoint
        rfid=[]
        while(1):
            data = []
            try:
                data = dev2.read(endpoint.bEndpointAddress, endpoint.wMaxPacketSize,timeout=10)
                if len(data)==8:
                    if (data[2]>=30) and (data[2]<=39):
                        rfid.append(data[2])
                    if data[2]==40:
                        #print rfid
                        if len(rfid)==10:
                            rfid_string = RFID_ID(rfid)
                            with open('RFID.txt','w') as f:
                                f.write("%s" % rfid_string)
                            print rfid_string
                            
                        rfid=[]
                                
            except usb.core.USBError as e:
                #print "ERROR 2 : ",e.strerror                    
                if e.strerror.find("Connection timed out")>=0:
                    continue
                else:
                    raise ValueError('RFID USB Reader Error 2')
                    #break
  

Omron-PLC-USB-RFID.py

import usb.core
import usb.util
import sys
from random import randint


#Omron PLC USB
dev1 = usb.core.find(idVendor=0x0590, idProduct=0x005B)
 
if dev1 is None:
    raise ValueError('PLC Not Detected')

                                                                                  
endpoint_in = dev1[0][(0,0)][0]
endpoint_out = dev1[0][(0,0)][1]



def Omron_PLC_USB_Write(msg,revlen):
    x=0
    cnt1=0
    cnt2=0
    data = []
    while(1):        
        try:
            if x==0:
                endpoint_out.write(msg)
                x=1
                
            data += dev1.read(endpoint_in.bEndpointAddress, endpoint_in.wMaxPacketSize, timeout = 20)
            if len(data)==revlen:             
                return data
            
        except usb.core.USBError as e:
            
            if e.strerror.find("error sending control message")>=0:
                cnt1+=1
                if cnt1>2:                    
                    raise ValueError('Over error sending control message')
                x=0
                continue
            
            elif e.strerror.find("Connection timed out")>=0:
                cnt2+=1
                if cnt2>2:
                    raise ValueError('Over Connection timed out')
                continue
            
            else:
                raise ValueError(e.strerror)


def Omron_PLC_Run_Mode():    
    write_array = [0xAB,0x00,0x11,0x80,0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x00]
    write_array.append(randint(1,0xFF))
    write_array +=[0x04,0x01,0xFF,0xFF,0x04]
    sumcheck = sum(write_array)
    sumHi = ((sumcheck >> 8) & 0xFF)
    sumLo = (sumcheck & 0xFF)
    write_array.append(sumHi)
    write_array.append(sumLo)
    res = Omron_PLC_USB_Write(write_array,19)
    if len(res)==19:
        sum1= res[len(res)-2]<<8 | res[len(res)-1]
        res.pop()
        res.pop()
        sum2 = sum(res)
        if sum1==sum2:
            revc = [171, 0, 16, 192, 0, 2, 0, 0, 251, 0, 0, 0, 4, 1, 0, 0]
            res.pop(12)
            if res==revc:
                print "Omron PLC is Run Mode"
            else:
                print "Omron PLC Run Mode Error"
        else:
            print "Omron PLC Run Mode Error"
    else:
        print "Omron PLC Run Mode Error"







def Omron_PLC_D_Write_4W(D_number,D_value):
    Omron_PLC_Write_Array = [0xAB,0x00,0x1C,0x80,0x00,0x2,0x00,0x00,0x00,0x00,0x00,0x00]
    rdm = randint(1,0xFF)
    Omron_PLC_Write_Array.append(rdm)
    Omron_PLC_Write_Array +=[0x01,0x02,0x82]
    Omron_PLC_Write_Array.append(D_number >> 8);
    Omron_PLC_Write_Array.append(D_number & 0xFF);
    Omron_PLC_Write_Array +=[0x00,0x00,0x04]
    for index in range(4):
        Omron_PLC_Write_Array.append(D_value[index] >> 8);
        Omron_PLC_Write_Array.append(D_value[index] & 0xFF);
    
    sumcheck = sum(Omron_PLC_Write_Array)
    sumHi = ((sumcheck >> 8) & 0xFF)
    sumLo = (sumcheck & 0xFF)
    Omron_PLC_Write_Array.append(sumHi)
    Omron_PLC_Write_Array.append(sumLo)
    res = Omron_PLC_USB_Write(Omron_PLC_Write_Array,19)
    if len(res)==19:
        val1=res.pop()
        val2=res.pop()
        sum1 = val2<<8 | val1
        sum2 = sum(res)
        if sum1==sum2:            
            rdm2= res.pop(12)
            if rdm==rdm2:
                revc = [171, 0, 16, 192, 0, 2, 0, 0, 251, 0, 0, 0, 1, 2, 0, 0]
                if res==revc:
                    return 1
                else:
                    return 0
            else:
                return 0
        else:
            return 0
    else:
        return 0

    
def RFID_ID_to_PLC(RFID_ID):    
    v = [0,0,0,0]
    for i in range(3):
        v[i]=RFID_ID[i]
            
    c = Omron_PLC_D_Write_4W(0,v)    
    if not c:
        return

    print v
    

Omron_PLC_Run_Mode()


while(1):
    try:
        data = []
        with open('RFID.txt','r') as myfile:
            data = myfile.read().split()

        #print data
        RFID_ID =[]
        for i in range(0,len(data)):
            RFID_ID.append(int(data[i]))
            
        #print  RFID_ID
        if len(RFID_ID)==3:
            RFID_ID_to_PLC(RFID_ID)                
            
    except usb.core.USBError as e:
        #print e.strerror
        continue 
  


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