This PLC application about how to use USB Smart Card Reader on Omron PLC with USB Connection.
USB connection between USB Smart Card Reader and Omron PLC using Raspberry Pi, and both devices use USB Port on Raspberry Pi.
enter a smart card (chip card) into the USB Smart Card Reader, and the information contained in the smart card such as Integrated Circuit Card ID (ICCID) Number, data storage, and etc.
In case study:
I use GSM SIM card and in GSM SIM card can provide ICCID Number, Phonebook.
Reading SIM Card ICCID Number and Send to Omron PLC and save to memory from D0 to D4 (10 digits).
This application is suitable for accessing the machine with GSM SIM card.
Video Demonstration:
I use 4 Pieces GSM SIM card with CCID number:
In PLC Ladder Programming:
Copy paste all project file to folder /home/pi on Raspberry Pi
sudo groupadd usbsmartcard
sudo usermod -a -G usbsmartcard pi
sudo nano /etc/udev/rules.d/usbsmartcard.rules
add text:
SUBSYSTEMS=="usb", ACTION=="add", ATTRS{idVendor}=="0bda", ATTRS{idProduct}=="0165", GROUP="usbsmartcard, MODE="0660"
Press Ctrl+X to exit, Y to save followed by enter twice
sudo crontab -e
Scroll to bottom and add:
@reboot python /home/pi/Smart-Card-startup.py
@reboot python /home/pi/PLC-startup.py
Press Ctrl+X to exit, Y to save followed by enter twice
Omron-PLC-USB.py
USB connection between USB Smart Card Reader and Omron PLC using Raspberry Pi, and both devices use USB Port on Raspberry Pi.
enter a smart card (chip card) into the USB Smart Card Reader, and the information contained in the smart card such as Integrated Circuit Card ID (ICCID) Number, data storage, and etc.
In case study:
I use GSM SIM card and in GSM SIM card can provide ICCID Number, Phonebook.
Reading SIM Card ICCID Number and Send to Omron PLC and save to memory from D0 to D4 (10 digits).
This application is suitable for accessing the machine with GSM SIM card.
Video Demonstration:
USB Smart Card Reader on Omron PLC USB using Raspberry Pi
Hardware for USB Smart Card Reader on Omron PLC USB
- Omron PLC USB, I use CP1L Omron PLC
- USB Smart Card Reader, I use chip: RIT 5169 / RTS5169 Realtek
- Raspberry Pi, I use Raspberry Pi 2
- 4 Pieces GSM SIM card
- USB PLC Cable
Connections Hardware for USB Smart Card Reader on Omron PLC USB
PLC Ladder Programming for USB Smart Card Reader on Omron PLC USB
A. Download project file
Download project file for CP1L-L10 Omron PLC, click hereB. CCID Number Setting
Download SimEdit Software, click hereI use 4 Pieces GSM SIM card with CCID number:
- 89628950000465764805
- 89628930000935329703
- 89628950000911808352
- 89628950000417377480
In PLC Ladder Programming:
Raspberry Pi setup for USB Smart Card Reader on Omron PLC USB
A. Basic Setup
Same with Omron PLC and Raspberry PiB. Additional Setup
Step 1 :
Download project file, click hereCopy paste all project file to folder /home/pi on Raspberry Pi
Step 2:
cd ~sudo groupadd usbsmartcard
sudo usermod -a -G usbsmartcard pi
sudo nano /etc/udev/rules.d/usbsmartcard.rules
add text:
SUBSYSTEMS=="usb", ACTION=="add", ATTRS{idVendor}=="0bda", ATTRS{idProduct}=="0165", GROUP="usbsmartcard, MODE="0660"
Press Ctrl+X to exit, Y to save followed by enter twice
STEP 3:
cd ~sudo crontab -e
Scroll to bottom and add:
@reboot python /home/pi/Smart-Card-startup.py
@reboot python /home/pi/PLC-startup.py
Press Ctrl+X to exit, Y to save followed by enter twice
Python Code for USB Smart Card Reader on Omron PLC USB
USB-Smart-Card-Reader.py
import usb.core
import usb.util
import sys
from random import randint
#USB Smart Card Reader
dev2 = usb.core.find(idVendor=0x0BDA, idProduct=0x0165)
if dev2 is None:
raise ValueError('Smart Card Reader Not Detected')
endpoint_in1 = dev2[0][(0,0)][0]
endpoint_out1 = dev2[0][(0,0)][1]
endpoint_in2 = dev2[0][(0,0)][2]
msg_array =[[]]
msg = [0x62,0x00,0x00,0x00,0x00,0x00,0x01,0x03,0x00,0x00]
msg_array.append(msg)
msg = [0x61,0x05,0x00,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x11,0x00,0x00,0x0A,0x00]
msg_array.append(msg)
msg = [0x6F,0x07,0x00,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0xA0,0xA4,0x00,0x00,0x02,0x2F,0xE2]
msg_array.append(msg)
msg = [0x6F,0x05,0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0xA0,0xB0,0x00,0x00,0x0A]
msg_array.append(msg)
def Smart_Card_Reader_CCID():
CCID_SmartCard = []
data2 = []
out=True
cnt=1
while(1):
try:
if out:
endpoint_out1.write(msg_array[cnt])
out=False
data2 = []
data2 += dev2.read(endpoint_in2.bEndpointAddress, endpoint_in2.wMaxPacketSize, timeout = 1000)
if (cnt) == data2[6]:
#print ' '.join('{:02x}'.format(x) for x in data2)
cnt+=1
if cnt==len(msg_array):break
out=True
except usb.core.USBError as e:
return []
ln = len(data2)
if ln<10:
return []
check = []
for index in range(10):
check.append(data2[index])
if check != [0x80 ,0x0c ,0x00 ,0x00 ,0x00 ,0x00 ,0x04 ,0x00 ,0x80 ,0x00]:
return []
y=10
while(1):
b1= (data2[y] & 0x0f)<<4
b2 = (data2[y] & 0xf0)>>4
b = b1 | b2
CCID_SmartCard.append(b)
y+=1
if y==(ln-2) :break
return CCID_SmartCard
data1 = []
while(1):
try:
data1 += dev2.read(endpoint_in1.bEndpointAddress, endpoint_in1.wMaxPacketSize, timeout = 1000)
#print ' '.join('{:02x}'.format(x) for x in data1)
if data1==[0x50,0x03]:
data1 = []
print "Smart Card Reader Detecting Card"
CCID_Number = Smart_Card_Reader_CCID()
print "Smart Card Reader CCID :"
ccid_string = ' '.join('{:02x}'.format(x) for x in CCID_Number)
print ccid_string
with open('CCID.txt','w') as f:
f.write("%s" % ccid_string)
if data1==[0x50,0x02]:
with open('CCID.txt','w') as f:
f.write("00 00 00 00 00 00 00 00 00 00")
data1 = []
print "Smart Card Reader Not Detecting Card"
except usb.core.USBError as e:
#print e.strerror
if e.strerror.find("Operation not permitted")>=0:
print "Please use LX-Terminal : sudo python USB-Smart-Card-Reader.py"
break
elif e.strerror.find("The device does not recognize the command")>=0:
raise ValueError('Smart Card Reader Not Recognized')
elif e.strerror.find("No such device")>=0:
raise ValueError('Smart Card Reader Not Device')
elif e.strerror.find("Connection timed out")>=0:
#print "Smart Card Reader Timeout"
data1 = []
continue
else:
raise ValueError(e.strerror)
import usb.util
import sys
from random import randint
#USB Smart Card Reader
dev2 = usb.core.find(idVendor=0x0BDA, idProduct=0x0165)
if dev2 is None:
raise ValueError('Smart Card Reader Not Detected')
endpoint_in1 = dev2[0][(0,0)][0]
endpoint_out1 = dev2[0][(0,0)][1]
endpoint_in2 = dev2[0][(0,0)][2]
msg_array =[[]]
msg = [0x62,0x00,0x00,0x00,0x00,0x00,0x01,0x03,0x00,0x00]
msg_array.append(msg)
msg = [0x61,0x05,0x00,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x11,0x00,0x00,0x0A,0x00]
msg_array.append(msg)
msg = [0x6F,0x07,0x00,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0xA0,0xA4,0x00,0x00,0x02,0x2F,0xE2]
msg_array.append(msg)
msg = [0x6F,0x05,0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0xA0,0xB0,0x00,0x00,0x0A]
msg_array.append(msg)
def Smart_Card_Reader_CCID():
CCID_SmartCard = []
data2 = []
out=True
cnt=1
while(1):
try:
if out:
endpoint_out1.write(msg_array[cnt])
out=False
data2 = []
data2 += dev2.read(endpoint_in2.bEndpointAddress, endpoint_in2.wMaxPacketSize, timeout = 1000)
if (cnt) == data2[6]:
#print ' '.join('{:02x}'.format(x) for x in data2)
cnt+=1
if cnt==len(msg_array):break
out=True
except usb.core.USBError as e:
return []
ln = len(data2)
if ln<10:
return []
check = []
for index in range(10):
check.append(data2[index])
if check != [0x80 ,0x0c ,0x00 ,0x00 ,0x00 ,0x00 ,0x04 ,0x00 ,0x80 ,0x00]:
return []
y=10
while(1):
b1= (data2[y] & 0x0f)<<4
b2 = (data2[y] & 0xf0)>>4
b = b1 | b2
CCID_SmartCard.append(b)
y+=1
if y==(ln-2) :break
return CCID_SmartCard
data1 = []
while(1):
try:
data1 += dev2.read(endpoint_in1.bEndpointAddress, endpoint_in1.wMaxPacketSize, timeout = 1000)
#print ' '.join('{:02x}'.format(x) for x in data1)
if data1==[0x50,0x03]:
data1 = []
print "Smart Card Reader Detecting Card"
CCID_Number = Smart_Card_Reader_CCID()
print "Smart Card Reader CCID :"
ccid_string = ' '.join('{:02x}'.format(x) for x in CCID_Number)
print ccid_string
with open('CCID.txt','w') as f:
f.write("%s" % ccid_string)
if data1==[0x50,0x02]:
with open('CCID.txt','w') as f:
f.write("00 00 00 00 00 00 00 00 00 00")
data1 = []
print "Smart Card Reader Not Detecting Card"
except usb.core.USBError as e:
#print e.strerror
if e.strerror.find("Operation not permitted")>=0:
print "Please use LX-Terminal : sudo python USB-Smart-Card-Reader.py"
break
elif e.strerror.find("The device does not recognize the command")>=0:
raise ValueError('Smart Card Reader Not Recognized')
elif e.strerror.find("No such device")>=0:
raise ValueError('Smart Card Reader Not Device')
elif e.strerror.find("Connection timed out")>=0:
#print "Smart Card Reader Timeout"
data1 = []
continue
else:
raise ValueError(e.strerror)
Omron-PLC-USB.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 Smart_Card_Reader_Write_to_PLC(CCID_Number):
if len(CCID_Number)<10:return
v1 = [0,0,0,0]
t=0
for i in range(4):
v1[i]=(CCID_Number[t+0]<<8 | CCID_Number[t+1])
t+=2
c = Omron_PLC_D_Write_4W(0,v1)
if not c:
return
v1 = [0,0,0,0]
v1[0]=(CCID_Number[t+0]<<8 | CCID_Number[t+1])
c = Omron_PLC_D_Write_4W(4,v1)
if not c:
return
Omron_PLC_Run_Mode()
while(1):
try:
data = []
with open('CCID.txt','r') as myfile:
data = myfile.read().split()
#print data
CCID_Number =[]
for i in range(0,len(data)):
CCID_Number.append(int(data[i],16))
#print CCID_Number
Smart_Card_Reader_Write_to_PLC(CCID_Number)
except usb.core.USBError as e:
#print e.strerror
continue
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 Smart_Card_Reader_Write_to_PLC(CCID_Number):
if len(CCID_Number)<10:return
v1 = [0,0,0,0]
t=0
for i in range(4):
v1[i]=(CCID_Number[t+0]<<8 | CCID_Number[t+1])
t+=2
c = Omron_PLC_D_Write_4W(0,v1)
if not c:
return
v1 = [0,0,0,0]
v1[0]=(CCID_Number[t+0]<<8 | CCID_Number[t+1])
c = Omron_PLC_D_Write_4W(4,v1)
if not c:
return
Omron_PLC_Run_Mode()
while(1):
try:
data = []
with open('CCID.txt','r') as myfile:
data = myfile.read().split()
#print data
CCID_Number =[]
for i in range(0,len(data)):
CCID_Number.append(int(data[i],16))
#print CCID_Number
Smart_Card_Reader_Write_to_PLC(CCID_Number)
except usb.core.USBError as e:
#print e.strerror
continue
Labels:
Omron PLC
PLC
PLC Application
PLC USB Protocol
Raspberry Pi
Raspberry Pi and PLC
USB Smart Card Reader
Omron PLC
PLC
PLC Application
PLC USB Protocol
Raspberry Pi
Raspberry Pi and PLC
USB Smart Card Reader