Guest User

rfid_sl500.py

a guest
Oct 10th, 2013
1,646
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.71 KB | None | 0 0
  1. #-------------------------------------------------------------------------------
  2. # Name: RFID Reader/Writer API for the SL500 RFID USB module
  3. #
  4. # Authors: Arnan (Roger) Siptiakiat
  5. # Attaphan Chan-in
  6. #
  7. # Created: 04/10/2013
  8. # Copyright: (c) 2013Learning Inventions Lab, Chiang Mai University
  9. # Licence: GPL v.3
  10. #-------------------------------------------------------------------------------
  11.  
  12. import serial
  13. import time
  14. import sys
  15. import operator
  16.  
  17. ## create an command array template
  18.  
  19.  
  20.  
  21. class RFID_Reader():
  22. def __init__(self):
  23.  
  24. self.debugOn = True
  25. self.version = "1.0.1"
  26.  
  27. self.data_Cerate = 0
  28. self.block_return = 0
  29. self.result_read_string = 0
  30. self.length_CMD = 0
  31. self.ser = serial.Serial()
  32. self.ser.setBaudrate(19200)
  33. self.ser.port = 6 # comport start port0 : self.ser.port = 7 is comport 8
  34. self.ser.timeout = 0.2
  35.  
  36.  
  37. # Connects to the RFID using the given portName or number
  38. # - On Windows this sould be the COM port number -1.
  39. # Ex. COM1 -> portName=0, COM2 -> portName=1 ...
  40. # - On Linux (or the Raspberry Pi), the port name will be
  41. # /dev/ttyUSBx => x = port number
  42. # Ex. /dev/ttyUSB0
  43.  
  44. def connect(self, portName):
  45. self.ser.port = portName
  46. self.ser.open()
  47. time.sleep(0.001) # this is required on the Raspberry PI
  48. # the RPi seems to need time to open the port before
  49. # using it
  50.  
  51. def debug(self, message):
  52. if self.debugOn :
  53. print message
  54.  
  55.  
  56. # read one byte and add to the receive checksum
  57. def readByte(self):
  58. try:
  59. inByte = self.ser.read()
  60. return(ord(inByte))
  61. except:
  62. self.debug("Serial read error, timeout")
  63. ## sys.exit()
  64. raise
  65.  
  66.  
  67. # Wait for the reply header 0x55,0xFF from the GoGo
  68. # returns 0 if timeout (1 sec)
  69. # returns 1 if header is found
  70.  
  71. def waitForHeader(self):
  72.  
  73. timeOutCounter = 0
  74.  
  75. while (timeOutCounter < 10):
  76. while (self.ser.inWaiting > 1):
  77. if (self.readByte()==0xAA):
  78. if (self.readByte() == 0xBB):
  79. return(1)
  80.  
  81. time.sleep(0.001)
  82. timeOutCounter += 1
  83.  
  84. return(0)
  85.  
  86.  
  87. def createString(self, array):
  88. outStr = ""
  89. for item in array:
  90. outStr += item
  91.  
  92. return(outStr)
  93.  
  94. def writeCommand(self, cmd):
  95.  
  96. # ==================================================
  97. # Write command
  98. # ==================================================
  99.  
  100. self.ser.write(self.createString(cmd))
  101.  
  102.  
  103.  
  104. # ==================================================
  105. # Read reply
  106. # ==================================================
  107.  
  108. def readReply(self):
  109.  
  110.  
  111. if self.waitForHeader() == 0:
  112. self.debug("Reply not found")
  113. else:
  114. Length = self.readByte()
  115. Length += (self.readByte() << 8)
  116.  
  117. Deveice_ID = self.readByte()
  118. Deveice_ID += (self.readByte() << 8)
  119.  
  120. Command_code = self.readByte()
  121. Command_code += (self.readByte() << 8)
  122.  
  123. Status = self.readByte()
  124.  
  125. if Status != 0:
  126. self.data_Cerate = 0
  127.  
  128. else:
  129. if Command_code == 0x0104: #0x0401 Command_code of get model
  130. self.data_Cerate = 11
  131. elif Command_code == 0x0201: #0x0102 Command_code of request
  132. self.data_Cerate = 2
  133. elif Command_code == 0x0208: #0x802 Command_code of read
  134. self.data_Cerate = 16
  135. elif Command_code == 0x0202: #0x202 Command_code of anticoll
  136. self.data_Cerate = 4
  137. elif Command_code == 0x0203: #0x302 Command_code of select
  138. self.data_Cerate = 1
  139. else:
  140. self.data_Cerate = 0
  141.  
  142.  
  143. data = []
  144. j = self.data_Cerate
  145. for i in range(j):
  146. data.append(self.readByte())
  147.  
  148.  
  149. Verification = self.readByte()
  150.  
  151.  
  152. self.reply = {
  153.  
  154.  
  155. 'Length' :Length,
  156. 'Deveice_ID' :Deveice_ID,
  157. 'Command_code' :Command_code,
  158. 'Status' :Status,
  159. 'data' :data,
  160. 'Verification' :Verification
  161. }
  162.  
  163. return self.reply
  164.  
  165. def verification_CMD(self,length):
  166.  
  167.  
  168. verification_xor = 0
  169.  
  170. for i in range (length-4):
  171. verification_xor = operator.xor(verification_xor,ord(self.CMD[3+i]))
  172.  
  173. verification_xor = verification_xor & 0xff
  174. self.CMD[length-1] = chr(verification_xor)
  175.  
  176.  
  177.  
  178.  
  179. def length(self,length):
  180.  
  181. length_sum = length - 4
  182. length_sum = length_sum & 0xffff
  183. self.CMD[2] = chr(length_sum)
  184.  
  185.  
  186. def Create_CMD_Head(self,length):
  187.  
  188. self.CMD = []
  189. for i in range (length):
  190. self.CMD.append(chr(0))
  191.  
  192. self.CMD[0] = chr(0xAA)
  193. self.CMD[1] = chr(0xBB)
  194.  
  195.  
  196.  
  197. def CMD_KeyA(self):
  198.  
  199. self.CMD[10] = chr(0xFF)
  200. self.CMD[11] = chr(0xFF)
  201. self.CMD[12] = chr(0xFF)
  202. self.CMD[13] = chr(0xFF)
  203. self.CMD[14] = chr(0xFF)
  204. self.CMD[15] = chr(0xFF)
  205.  
  206.  
  207. def ping(self):
  208. self.debug("ping")
  209. length_CMD = 9
  210.  
  211. self.Create_CMD_Head(length_CMD)
  212. self.CMD[6] = chr(0x04)
  213. self.CMD[7] = chr(0x01)
  214.  
  215. self.verification_CMD(length_CMD)
  216. self.length(length_CMD)
  217.  
  218. self.writeCommand(self.CMD)
  219. self.result()
  220.  
  221.  
  222. def beep(self):
  223. self.debug("beep")
  224. length_CMD = 10
  225.  
  226. self.Create_CMD_Head(length_CMD)
  227. self.CMD[6] = chr(0x06)
  228. self.CMD[7] = chr(0x01)
  229. self.CMD[8] = chr(0x10)
  230.  
  231. self.verification_CMD(length_CMD)
  232. self.length(length_CMD)
  233.  
  234. self.writeCommand(self.CMD)
  235. self.result()
  236.  
  237. def request(self):
  238. ## self.debug("request")
  239. length_CMD = 10
  240.  
  241. self.Create_CMD_Head(length_CMD)
  242. self.CMD[6] = chr(0x01)
  243. self.CMD[7] = chr(0x02)
  244. self.CMD[8] = chr(0x26)
  245.  
  246. self.verification_CMD(length_CMD)
  247. self.length(length_CMD)
  248.  
  249.  
  250. self.writeCommand(self.CMD)
  251. ## self.result()
  252.  
  253. a = self.result()
  254. return(a)
  255.  
  256.  
  257.  
  258. def anticoll(self):
  259. ## self.debug ("anticoll")
  260. length_CMD = 9
  261.  
  262. self.Create_CMD_Head(length_CMD)
  263. self.CMD[6] = chr(0x02)
  264. self.CMD[7] = chr(0x02)
  265.  
  266. self.verification_CMD(length_CMD)
  267. self.length(length_CMD)
  268.  
  269. self.writeCommand(self.CMD)
  270. self.result()
  271.  
  272.  
  273.  
  274. def select(self):
  275. ## self.debug ("select")
  276. length_CMD = 13
  277.  
  278. self.Create_CMD_Head(length_CMD)
  279. self.CMD[6] = chr(0x03)
  280. self.CMD[7] = chr(0x02)
  281. self.CMD[8] = chr(self.Card_serial_Num[0])
  282. self.CMD[9] = chr(self.Card_serial_Num[1])
  283. self.CMD[10] = chr(self.Card_serial_Num[2])
  284. self.CMD[11] = chr(self.Card_serial_Num[3])
  285.  
  286. self.verification_CMD(length_CMD)
  287. self.length(length_CMD)
  288.  
  289. self.writeCommand(self.CMD)
  290. self.result()
  291.  
  292.  
  293. def init_type(self):
  294. ## debug "init type"
  295. length_CMD = 10
  296.  
  297. self.Create_CMD_Head(length_CMD)
  298. self.CMD[6] = chr(0x08)
  299. self.CMD[7] = chr(0x01)
  300. self.CMD[8] = chr(0x41)
  301. self.verification_CMD(length_CMD)
  302. self.length(length_CMD)
  303.  
  304. self.writeCommand(self.CMD)
  305. self.result()
  306.  
  307. def antenna_sta(self,Parameter):
  308. ## debug "antenna_sta"
  309. length_CMD = 10
  310.  
  311.  
  312. self.Create_CMD_Head(length_CMD)
  313. self.CMD[6] = chr(0x0C)
  314. self.CMD[7] = chr(0x01)
  315. self.CMD[8] = chr(Parameter)
  316. self.verification_CMD(length_CMD)
  317. self.length(length_CMD)
  318.  
  319. self.writeCommand(self.CMD)
  320. self.result()
  321.  
  322. def authen(self,block):
  323. ## self.debug ("authen")
  324. length_CMD = 17
  325.  
  326. self.Create_CMD_Head(length_CMD)
  327. self.CMD[6] = chr(0x07)
  328. self.CMD[7] = chr(0x02)
  329. self.CMD[8] = chr(0x60)
  330. self.CMD[9] = chr(block)
  331.  
  332. self.CMD_KeyA()
  333. self.verification_CMD(length_CMD)
  334. self.length(length_CMD)
  335.  
  336. self.writeCommand(self.CMD)
  337. self.result()
  338.  
  339.  
  340. # ==================================================
  341. # Read function
  342. # ==================================================
  343.  
  344.  
  345. def read_data(self):
  346. # Sector 0, Block 1
  347. self.result_read_string = 0
  348. self.request()
  349. self.anticoll()
  350. self.select()
  351. self.authen(1)
  352. self.debug("Read data")
  353. length_CMD = 10
  354.  
  355. self.Create_CMD_Head(length_CMD)
  356. self.CMD[6] = chr(0x08)
  357. self.CMD[7] = chr(0x02)
  358. self.CMD[8] = chr(0x01)
  359.  
  360. self.verification_CMD(length_CMD)
  361. self.length(length_CMD)
  362.  
  363. self.writeCommand(self.CMD)
  364. self.result()
  365.  
  366. return(self.data)
  367.  
  368. def read_String(self):
  369. # Sector 0, Block 1
  370.  
  371. self.result_read_string = 1
  372. if self.request() == 0:
  373. self.debug("Not Read : Tag not found")
  374. return("''")
  375. else:
  376. self.anticoll()
  377. self.select()
  378. self.authen(1)
  379. ## self.debug("Read String")
  380. length_CMD = 10
  381.  
  382. self.Create_CMD_Head(length_CMD)
  383. self.CMD[6] = chr(0x08)
  384. self.CMD[7] = chr(0x02)
  385. self.CMD[8] = chr(0x01)
  386.  
  387. self.verification_CMD(length_CMD)
  388. self.length(length_CMD)
  389.  
  390. self.writeCommand(self.CMD)
  391. self.result()
  392.  
  393. return(self.data_String)
  394.  
  395. def SectorToBlock(self,sector,block):
  396.  
  397. if sector <= 15 and block <= 3:
  398.  
  399. if sector == 0 and block == 0:
  400. self.debug("Manufacturer Data Block! is not read/write")
  401. sys.exit()
  402. if block == 3:
  403. self.debug("Key Block! is not read/write")
  404. sys.exit()
  405.  
  406. if sector == 1:
  407. block = block + 4
  408. elif sector == 2:
  409. block = block + 8
  410. elif sector == 3:
  411. block = block + 12
  412. elif sector == 4:
  413. block = block + 16
  414. elif sector == 5:
  415. block = block + 20
  416. elif sector == 6:
  417. block = block + 24
  418. elif sector == 7:
  419. block = block + 28
  420. elif sector == 8:
  421. block = block + 32
  422. elif sector == 9:
  423. block = block + 36
  424. elif sector == 10:
  425. block = block + 40
  426. elif sector == 11:
  427. block = block + 44
  428. elif sector == 12:
  429. block = block + 48
  430. elif sector == 13:
  431. block = block + 52
  432. elif sector == 14:
  433. block = block + 56
  434. elif sector == 15:
  435. block = block + 60
  436.  
  437. self.block_return = block
  438.  
  439. elif sector > 15:
  440. self.debug(">>RFID read error")
  441. self.debug(">>not Sector")
  442. sys.exit()
  443.  
  444. elif block > 3:
  445. self.debug(">>RFID read error")
  446. self.debug(">>not Block")
  447. sys.exit()
  448.  
  449.  
  450. def read(self,sector,block):
  451.  
  452. self.result_read_string = 0
  453.  
  454. self.debug("Sector %d Block %d" % (sector , block))
  455.  
  456. self.SectorToBlock(sector,block)
  457.  
  458. self.request()
  459. self.anticoll()
  460. self.select()
  461. self.authen(self.block_return)
  462. self.debug("read")
  463. length_CMD = 10
  464.  
  465. self.Create_CMD_Head(length_CMD)
  466. self.CMD[6] = chr(0x08)
  467. self.CMD[7] = chr(0x02)
  468. self.CMD[8] = chr(block)
  469.  
  470. self.verification_CMD(length_CMD)
  471. self.length(length_CMD)
  472.  
  473. self.writeCommand(self.CMD)
  474. self.result()
  475.  
  476.  
  477. # ==================================================
  478. # Write function
  479. # ==================================================
  480.  
  481.  
  482. def write_String(self,data):
  483. #Sector 0, Block 1
  484.  
  485. if self.request() == 0:
  486. self.debug("Not write : Tag not found")
  487. return("''")
  488. else:
  489.  
  490. self.anticoll()
  491. self.select()
  492. self.authen(1)
  493. self.debug("Write")
  494.  
  495. length_CMD = 26
  496. self.Create_CMD_Head(length_CMD)
  497.  
  498. if len(data) <= 16:
  499. for i in range(len(data)):
  500. self.CMD[9+i] = data[i]
  501.  
  502. self.CMD[6] = chr(0x09)
  503. self.CMD[7] = chr(0x02)
  504. self.CMD[8] = chr(0x01)
  505.  
  506. self.verification_CMD(length_CMD)
  507. self.length(length_CMD)
  508.  
  509. self.writeCommand(self.CMD)
  510. self.result()
  511.  
  512. else:
  513. self.debug("Write error: data more 16 characters")
  514.  
  515.  
  516. def write_data(self,data):
  517.  
  518. self.request()
  519. self.anticoll()
  520. self.select()
  521. self.authen(1)
  522. self.debug("Write")
  523.  
  524. length_CMD = 26
  525. self.Create_CMD_Head(length_CMD) #CMD[0] , CMD[1]
  526.  
  527.  
  528. self.CMD[6] = chr(0x09)
  529. self.CMD[7] = chr(0x02)
  530. self.CMD[8] = chr(0x01)
  531.  
  532. for i in range(16):
  533. self.CMD[9+i] = chr(data[i])
  534.  
  535.  
  536. self.verification_CMD(length_CMD)
  537. self.length(length_CMD)
  538.  
  539. self.writeCommand(self.CMD)
  540. self.result()
  541.  
  542.  
  543. def write(self,sector,block,data):
  544.  
  545.  
  546. self.debug("Sector %d Block %d" % (sector , block))
  547.  
  548. self.SectorToBlock(sector,block)
  549.  
  550. self.request()
  551. self.anticoll()
  552. self.select()
  553. self.authen(self.block_return)
  554.  
  555. self.debug("Write")
  556.  
  557.  
  558. length_CMD = 26
  559. self.Create_CMD_Head(length_CMD) #CMD[0] , CMD[1]
  560.  
  561.  
  562. self.CMD[6] = chr(0x09)
  563. self.CMD[7] = chr(0x02)
  564. self.CMD[8] = chr(0x01)
  565.  
  566. for i in range(16):
  567. self.CMD[9+i] = chr(data[i])
  568.  
  569.  
  570. self.verification_CMD(length_CMD)
  571. self.length(length_CMD)
  572.  
  573. self.writeCommand(self.CMD)
  574. self.result()
  575.  
  576.  
  577. # ==================================================
  578. # Result command
  579. # ==================================================
  580.  
  581.  
  582. def result(self):
  583.  
  584. ## RFIDTimeOut = 0
  585.  
  586. ## while (self.ser.inWaiting() == 0):
  587. ## time.sleep(0.01)
  588. ##
  589. ## #Loop RFIDTimeOut 15s to exit Program and Cancel for FP
  590. ## if RFIDTimeOut >= 1500:
  591. ## debug ">>RFID Time Out"
  592. ## RFIDTimeOut = 0
  593. ## sys.exit()
  594. ##
  595. ##
  596. ## RFIDTimeOut += 1
  597. ##
  598. ##
  599. ## RFIDTimeOut = 0
  600.  
  601. result = self.readReply()
  602.  
  603. #Result Code : 0 = Succeess and data code
  604. if result['Status'] == 0x00:
  605. ## self.debug (">>Success")
  606. #result Card serial Number 0x0202
  607. if result['Command_code'] == 0x0202:
  608. self.Card_serial_Num = [4]
  609. for i in range (4):
  610. self.Card_serial_Num.append(chr(0))
  611.  
  612. self.Card_serial_Num[0] = result['data'][0]
  613. self.Card_serial_Num[1] = result['data'][1]
  614. self.Card_serial_Num[2] = result['data'][2]
  615. self.Card_serial_Num[3] = result['data'][3]
  616.  
  617. ## debug result['data']
  618.  
  619. # result Read data 0x0802
  620. elif result['Command_code'] == 0x0208:
  621. # Read String data
  622. if self.result_read_string == 1:
  623.  
  624.  
  625. j = 0
  626.  
  627. for i in range (16):
  628. if result['data'][i] != 0x00 :
  629. j += 1
  630.  
  631. # Tag not Empty retrun data
  632. if j != 0:
  633. data_Array = [j]
  634. for i in range (j-1):
  635. data_Array .append(chr(0))
  636.  
  637. for i in range (j):
  638. data_Array [i] = chr(result['data'][i])
  639.  
  640. # function Array to String
  641. self.data_String = ''.join(data_Array)
  642.  
  643. ## self.debug(self.data_String)
  644. ## if data_Staring == "BANK":
  645. ## debug "OK"
  646. return(self.data_String)
  647.  
  648. # Tag is Empty retrun ""
  649. else :
  650. self.data_String = "''"
  651. self.debug("Tag is Empty")
  652. return(self.data_String)
  653.  
  654.  
  655. # Read data
  656. else:
  657.  
  658. self.data = result['data']
  659. self.debug(self.data)
  660. return(self.data)
  661.  
  662. self.result_read_string = 0
  663.  
  664. #Result Code : not 0 = fail
  665. elif result['Status'] != 0x00:
  666. if result['Command_code'] == 0x0201:
  667. ## self.debug(">>request fail")
  668. return(0)
  669.  
  670. ## if result['Command_code'] == 0x0208:
  671. ## self.debug(" ")
  672. ## return(0)
  673. else:
  674. return(0)
  675.  
  676.  
  677.  
  678.  
  679. if __name__ == '__main__':
  680.  
  681. # ==================================================
  682. # main
  683. # ==================================================
  684.  
  685.  
  686. rf = RFID_Reader()
  687. ## rf.connect("/dev/ttyUSB0")
  688. rf.connect(7)
  689.  
  690. ## rf.ping()
  691. ## rf.beep()
  692. ## time.sleep(0.5)
  693.  
  694. try:
  695. print "Read string = " + rf.read_String()
  696. rf.write_String("ABC")
  697. print "Re-Read = " + rf.read_String()
  698. except:
  699. print "Tag not found"
  700. ##---------------------------------------------------------------
  701.  
  702.  
  703. ##if RF.read_String() == "ATTAPAN":
  704. ## RF.beep()
  705. ##elif RF.read_String() == "BANK":
  706. ##
  707. ## RF.beep()
  708. ## time.sleep(0.2)
  709. ## RF.beep()
  710. ##elif RF.read_String() == "LIL@CMU":
  711. ##
  712. ## RF.beep()
  713. ## time.sleep(0.2)
  714. ## RF.beep()
  715. ## time.sleep(0.2)
  716. ## RF.beep()
  717.  
  718. ##-----------------------------------------------------
  719.  
  720. ## if RF.read_data() == [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2]:
  721. ## self.debug("OK")
  722. ## RF.beep()
  723. ##
  724. ## else:
  725. ## self.debug("fail")
  726.  
  727.  
  728.  
  729. ##-----------------------------------------------------
  730. ##try:
  731. ## rf.read_String()
  732. ##except:
  733. ## print "Tag not found"
Advertisement
Add Comment
Please, Sign In to add comment