Monday, February 2, 2015

Build a RFID System for PLC using Raspberry Pi





This application about build a RFID system for PLC. Use USB RFID Reader 125Khz and Raspberry Pi for this RFID system. And communication between PLC and RFID system use Modbus Protocol.
PLC with Modbus Protocol Support: I use Siemens PLC.

for Radio-Frequency IDentification (RFID) Reader 125Khz: I use USB Interface
for RFID Tag: I use RFID Credit Card, RFID Keychain, and RFID Wristband

This image about RFID System for PLC:
Build a RFID System for PLC

Example: User Access to Machine use RFID Tag
Example ID Number:
1. ID Number of RFID tag: 0011487710 for Machine ON/Access OK for User 1
2. ID Number of RFID tag: 0008997168 for Machine ON/ Access OK for User 2
3. ID Number of RFID tag: 0006216553 for Machine OFF/Access Not OK

RFID Credit Card, RFID Keychain, RFID Wristband

Result of User Access to Machine use RFID Tag:



Hardware:
1. USB RFID Reader 125Khz
2. 3 pcs RFID Tag
3. Raspberry Pi Model B
4. RS232 Module Male
5. PLC Cable
6. PLC with Modbus Support (Siemens PLC S7 200)

Wiring:

wiring of USB RFID Reader 125Khz to Raspberry Pi and PLC

Raspberry Setup:
Download project file, click here
How to use project file, click link:
https://www.youtube.com/watch?v=X5z_5kKZaTs
Change all id vendor and id product with idVendor=08ff, idProduct=0009

RFID Number Setting in PLC Ladder Programming
Download project file, click here
Example:
ID Number = 0011487710
Splits per 4 digits: 00-1148-7710
Register0 (VW800) = 7710 (Integer value = 7710)
Register1 (VW802) =1148 (Integer value = 1148)
Register2 (VW804) =00 (Integer value = 0)

RFID Tag Number Setting in PLC Ladder Programming

Python Code
import sys
import serial
import usb.core
import usb.util
import time
from random import randint

#Modbus
def CRCcal(msg):
CRC = 0xFFFF
CRCHi = 0xFF
CRCLo = 0xFF
CRCLSB = 0x00
for i in range(0, len(msg)-2,+1):
CRC = (CRC ^ msg[i])
for j in range(0, 8):
CRCLSB = (CRC & 0x0001);
CRC = ((CRC >> 1) & 0x7FFF)

if (CRCLSB == 1):
CRC = (CRC ^ 0xA001)
CRCHi = ((CRC >> 8) & 0xFF)
CRCLo = (CRC & 0xFF)
return (CRCLo,CRCHi)

def CRCvalid(resp):
CRC = CRCcal(resp)
if (CRC[0]==resp[len(resp)-2]) & (CRC[1]==resp[len(resp)-1]):return True
return False

def Func16Modbus(slave,start,values):
Slave_Address = slave
Function = 16
Starting_Address = start
NumberofRegisters = len(values)
Byte_Count = NumberofRegisters * 2
message = [0 for i in range(9 + 2 * NumberofRegisters)]
message[0] = (Slave_Address & 0xFF)
message[1] = (Function & 0xFF)
message[2] = ((Starting_Address >> 8) & 0xFF)
message[3] = (Starting_Address & 0xFF)
message[4] = ((NumberofRegisters >> 8) & 0xFF)
message[5] = (NumberofRegisters & 0xFF)
message[6] = (Byte_Count & 0xFF)

for i in range(0, NumberofRegisters):
message[7 + 2 * i] = ((values[i] >> 8) & 0xFF)
message[8 + 2 * i] = values[i] & 0xFF
CRC = CRCcal(message)

message[len(message) - 2] = CRC[0]
message[len(message) - 1] = CRC[1]

if ser.isOpen:
ser.write("".join(chr(h) for h in message))
reading = ser.read(8)
response = [0 for i in range(len(reading))]
for i in range(0, len(reading)):
response[i] = ord(reading[i])

if len(response)==8:
CRCok = CRCvalid(response)
if CRCok & (response[0]==slave) & (response[1]==Function):return True
return False

try:
ser = serial.Serial(
port = '/dev/ttyAMA0',
baudrate = 9600,
bytesize = serial.EIGHTBITS,
parity = serial.PARITY_EVEN,
stopbits = serial.STOPBITS_ONE,
timeout = 0.2
)
except Exception, e:
raise ValueError("ERROR! Serial Port: " + str(e))

#USB RFID Setting
# 08ff:0009 = Vendor_Id:Product_Id

Vendor_Id = 0x08ff
Product_Id = 0x0009

keyboard1= []
for i in range(130):
keyboard1.append("")

keyboard1[30]='1'
keyboard1[31]='2'
keyboard1[32]='3'
keyboard1[33]='4'
keyboard1[34]='5'
keyboard1[35]='6'
keyboard1[36]='7'
keyboard1[37]='8'
keyboard1[38]='9'
keyboard1[39]='0'

while True:
dev = None
dev = usb.core.find(idVendor=Vendor_Id, idProduct=Product_Id)
if dev is None:
print("Could not find Vendor_Id:Product_Id = "+ hex(Vendor_Id)+':'+hex(Product_Id))
else:
try:
dev.detach_kernel_driver(0)
except usb.core.USBError as e:
continue

try:
endpoint = dev[0][(0,0)][0]
except:
continue

dataid=[]
OrderIncrement=randint(0,255)
while True:
data = []
try:
data = dev.read(endpoint.bEndpointAddress,endpoint.wMaxPacketSize,timeout=10)
if len(data)==8 and len(dataid)<10: br=""> keycode=keyboard1[data[2]]
if keycode!="":
dataid.append(keycode)
except usb.core.USBError as e:
if len(dataid)==10:
datamodbus=[]
#Example RFID Number: 0006216553 = 00-0621-6553
#Modbus Register 0 = 6553
temp=dataid[6]+ dataid[7] + dataid[8] + dataid[9]
datamodbus.append(int(temp))

#Modbus Register 1 = 0621
temp=dataid[2] + dataid[3] + dataid[4] + dataid[5]
datamodbus.append(int(temp))

#Modbus Register 2 = 00
temp=dataid[1] + dataid[0]
datamodbus.append(int(temp))

#Modbus Register 3 = Order Value
OrderIncrement+=1
if OrderIncrement>32767:OrderIncrement=1
datamodbus.append(int(OrderIncrement))

print dataid
print datamodbus
Func16Modbus(1,0,datamodbus)

dataid=[]
if (e.message=='Operation timed out'):
continue
else:
print e.message
break

time.sleep(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