Advertisement
Agroz

rfid_USB

Jun 19th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import math
  3. import serial
  4. import binascii
  5. import subprocess
  6. import time
  7. from bitstring import BitArray
  8.  
  9. tags = {}
  10.  
  11. com_port = '/dev/ttyUSB0'
  12. baud_rate = 115200
  13. ser = serial.Serial(com_port, baud_rate, timeout=1)
  14.  
  15. def checksum(data):
  16. cSum = 0x00
  17. for byte in data:
  18. cSum += byte
  19. return (((~cSum) + 1) + (1 << 8)) % (1 << 8)
  20.  
  21. def send(cmd, data=None):
  22. msg = [0xA0, 0x00, 0x01, cmd]
  23. if data:
  24. msg.extend(data)
  25. msg[1] = len(msg) - 1
  26. msg.append(checksum(msg))
  27. ser.write(msg)
  28.  
  29. def ean_checksum(data):
  30. even = False
  31. cSum = 0
  32. for i in map(int,reversed(str(data))):
  33. if even:
  34. cSum += i
  35. even = False
  36. else:
  37. cSum += i * 3
  38. even = True
  39. return (int(math.ceil(cSum / 10.0)) * 10) - cSum
  40.  
  41. send(0x76,data=[0x14])
  42. time.sleep(1)
  43. ans = ser.read(ser.read(2)[1])
  44. send(0x7a, data=[0x00])
  45. ans = ser.read(ser.read(2)[1])
  46. send(0x89, data=[0xff])
  47.  
  48. timer = time.time()
  49. while True:
  50. try:
  51. ans = BitArray(ser.read(ser.read(2)[1])[5:])
  52. if ans.hex[:2] == '30':
  53. filt = BitArray(ans[8:11])
  54. part = BitArray(ans[11:14])
  55. ans = ans[14:]
  56. if part.uint == 0:
  57. company_len = 40
  58. indicator_len = 4
  59. elif part.uint == 1:
  60. company_len = 37
  61. indicator_len = 7
  62. elif part.uint == 2:
  63. company_len = 34
  64. indicator_len = 10
  65. elif part.uint == 3:
  66. company_len = 30
  67. indicator_len = 14
  68. elif part.uint == 4:
  69. company_len = 27
  70. indicator_len = 17
  71. elif part.uint == 5:
  72. company_len = 24
  73. indicator_len = 20
  74. elif part.uint == 6:
  75. company_len = 20
  76. indicator_len = 24
  77.  
  78. company = BitArray(ans[:company_len])
  79. indicator = BitArray(ans[company_len:company_len+indicator_len])
  80. #serNum = BitArray(ans[company_len+indicator_len:company_len+indicator_len+38])
  81.  
  82. barcode = (company.uint * 100000 + indicator.uint) * 10 + ean_checksum(company.uint * 100000 + indicator.uint)
  83. #if ans[-16:][:8].uint <= 55:
  84. # print(str(barcode), end=' - ')
  85. # print(ans[-16:][:8].uint)
  86.  
  87.  
  88. # if barcode not in tags:
  89. # tags[barcode] = [int(time.time()), 0]
  90. # else:
  91. # tags[barcode][1] += 1
  92.  
  93. # for tag in tags.keys():
  94. # if int(time.time()) - tags[tag][0] <= 3:
  95. # if tags[tag][1] >= 3:
  96. # subprocess.call(["xdotool", "type", str(barcode)])
  97. # subprocess.call(["xdotool", "key", "Return"])
  98. # print (tag, end=' - ')
  99. # print(int(time.time()))
  100. # del tags[tag]
  101. # else:
  102. # del tags[tag]
  103. subprocess.call(["xdotool", "type", str(barcode)])
  104. subprocess.call(["xdotool", "key", "Return"])
  105.  
  106. # print(str(barcode) + '*', end=' - ')
  107. # print(ans[-16:][:8].uint)
  108.  
  109. except Exception as e:
  110. #print(e)
  111. if time.time() - timer > 3600:
  112. send(0x70)
  113. timer = time.time()
  114. send(0x89, data=[0xff])
  115.  
  116. exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement