Advertisement
dhruvag2000

readAndVerifyRTOS202245.py

Apr 5th, 2022
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.20 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # Code tested and is working as of 21/01/2022 for BP
  3. # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
  4. # SPDX-License-Identifier: MIT
  5.  
  6. """
  7. This example shows connecting to the PN532 with I2C (requires clock
  8. stretching support), SPI, or UART. SPI is best, it uses the most pins but
  9. is the most reliable and universally supported.
  10. After initialization, try waving various 13.56MHz RFID cards over it!
  11. """
  12.  
  13. from pickle import FALSE
  14. import board
  15. import busio
  16. from digitalio import DigitalInOut
  17. import binascii
  18. from multiprocessing import Process    # for parallel processing
  19. import serial       # For serial communication to STM32
  20. import uuid     # For generating Transaction UID
  21. import time
  22.  
  23. #
  24. # NOTE: pick the import that matches the interface being used
  25. #
  26. from adafruit_pn532.i2c import PN532_I2C
  27.  
  28. # from adafruit_pn532.spi import PN532_SPI
  29. # from adafruit_pn532.uart import PN532_UART
  30.  
  31. # I2C connection:
  32. i2c = busio.I2C(board.SCL, board.SDA)
  33.  
  34. # Non-hardware
  35. # pn532 = PN532_I2C(i2c, debug=False)
  36.  
  37. # With I2C, we recommend connecting RSTPD_N (reset) to a digital pin for manual
  38. # harware reset
  39. reset_pin = DigitalInOut(board.D6)
  40. # On Raspberry Pi, you must also connect a pin to P32 "H_Request" for hardware
  41. # wakeup! this means we don't need to do the I2C clock-stretch thing
  42. req_pin = DigitalInOut(board.D12)
  43. pn532 = PN532_I2C(i2c, debug=False, reset=reset_pin, req=req_pin)
  44.  
  45.  
  46. ic, ver, rev, support = pn532.firmware_version
  47. print("Found PN532 with firmware version: {0}.{1}".format(ver, rev))
  48.  
  49. # Configure PN532 to communicate with MiFare cards
  50. pn532.SAM_configuration()
  51.  
  52. # The flag that goes high if a known rfid card is swiped
  53. rfid_flag = False
  54.  
  55. # Read and store the db in RAM
  56. byte_arr1 = []
  57. with open("rfidBinDatabase", "rb") as f:
  58.     print('Opening the file!')
  59.     byte = f.read(1)
  60.     while byte:
  61.         # Do stuff with byte.
  62.         byte = f.read(1)
  63.         byte_arr1.extend(bytes(byte))
  64.  
  65. byte_arr = byte_arr1
  66. print('Byte arr of db is:',byte_arr)
  67.  
  68. ser = serial.Serial(
  69.         port='/dev/ttyS0', #Replace ttyS0 with ttyAM0 for Pi1,Pi2,Pi0
  70.         baudrate = 9600,
  71.         parity=serial.PARITY_NONE,
  72.         stopbits=serial.STOPBITS_ONE,
  73.         bytesize=serial.EIGHTBITS,
  74.         timeout=1
  75. )
  76.  
  77. DOOR_COUNT = 8
  78. def processStmData(bString):
  79.     global imei, conn_status, door_status, charger_status, current, soc, soh, swap_state, station_state
  80.     print(len(bString))
  81.     if len(bString) == 10:
  82.         imei = int.from_bytes(bytes(bString[2:10]), "little")
  83.         print("imei : " + str(imei))
  84.     elif len(bString) == 19:
  85.         for i in range(DOOR_COUNT):
  86.             door_status[i] = (bString[1] >> i) & 1
  87.         print("door : " + str(door_status))
  88.         for i in range(DOOR_COUNT):
  89.             charger_status[i] = (bString[2] >> i) & 1
  90.         print("charger : " + str(charger_status))
  91.         conn_status = bString[3]
  92.         print("conn : " + str(conn_status))
  93.         swap_state = bString[4]
  94.         print("swap_state : "+str(swap_state))
  95.         station_state = bString[5]
  96.         print("station_state : "+str(station_state))
  97.         door_number = bString[6]
  98.         current[door_number] = struct.unpack('f', bString[7:11])[0]
  99.         print("current : " + str(current[door_number]))
  100.         soc[door_number] = struct.unpack('f', bString[11:15])[0]
  101.         print("soc : " + str(soc[door_number]))
  102.         #soh[door_number] = struct.unpack('f', bString[15:19])[0]
  103.         #print("soh : " + str(soh[door_number]))
  104.         soh[door_number] = int.from_bytes(bytes(bString[15:19]), "little")
  105.         print("soh : " + str(soh[door_number]))
  106.  
  107. def readSerial():
  108.     while 1:
  109.         inString = ser.readline()
  110.         if inString:
  111.             print (inString)
  112.             if inString[0] == 0xA0:
  113.                 # boot = False
  114.                 processStmData(inString)
  115.  
  116. def SendIdleBytes():
  117.     ser.write(b'\x0A')
  118.     for i in range(17):
  119.         ser.write(b'\x00')
  120.  
  121. def SendTIDBytes():
  122.     print ("The random id using uuid1() is : ",end="")
  123.     x = uuid.uuid1()
  124.     print (x)
  125.     print("And in bytes:")
  126.     in_bytes=x.bytes
  127.     print(in_bytes)
  128.     print("And the length:")
  129.     print(len(in_bytes))
  130.  
  131.     ser.write(b'\x0A')
  132.     ser.write(b'\x01')
  133.     ser.write(in_bytes)
  134.  
  135. def SendTapBytes():
  136.     ser.write(b'\x0A')
  137.     ser.write(b'\x03')
  138.     ser.write(b'\x01')
  139.     for i in range(15):
  140.         ser.write(b'\x00')
  141.  
  142. def SendBytes(invokedFlag = False):
  143.     print("Send bytes called\n")
  144.     if invokedFlag is True:
  145.         print("\nSending the RFID Tap bytes serially\n\n")
  146.         SendTapBytes()
  147.         SendTIDBytes()
  148.         time.sleep(10)
  149.     else:
  150.         while True:
  151.             SendIdleBytes()
  152.             time.sleep(1)
  153.  
  154.  
  155. def ScanCard():
  156.     global rfid_flag
  157.     print("Waiting for RFID/NFC card...")
  158.     prevUID = bytearray(b'3\x00\x00\x00')
  159.     while True:
  160.         # Check if a card is available to read
  161.         uid = pn532.read_passive_target(timeout=3)
  162.         print(".", end="")
  163.         # Try again if no card is available.
  164.         if uid is None:
  165.             continue
  166.         # print("Detected card with UID:", uid)
  167.         # print("Same ID?",prevUID, uid)
  168.         if prevUID == uid:
  169.             # print("Same ID",prevUID, uid)
  170.             continue
  171.         # Log the card that is detected
  172.         prevUID = uid
  173.         file = open("rfidLog.txt", "ab")
  174.         # Write bytes to file
  175.         immutable_bytes = bytes(uid)
  176.         file.write(immutable_bytes)
  177.         file.close()
  178.                 # if (i+1) % 3:
  179.                     # print(byte_arr)
  180.                     # byte_arr.clear()
  181.         print("Detected card with UID:", uid)
  182.         rfid_flag = set(immutable_bytes).issubset(byte_arr)
  183.         print(rfid_flag,": card found from local DB")
  184.         if rfid_flag:
  185.             SendBytes(True)
  186.  
  187. def HMIDisplay():
  188.     print("HMI Display STARTED\n")
  189.     while True:
  190.         print("HMI: Some info\n")
  191.         for i in range(10000000):
  192.             for i in range(5):
  193.                 pass
  194.  
  195.  
  196. if __name__ == '__main__':
  197.   p1 = Process(target=ScanCard)
  198.   p2 = Process(target=readSerial)
  199. #   p3 = Process(target=SendBytes)
  200.   p1.start()
  201. #   p3.start()
  202.   p2.start()
  203.   p1.join()
  204. #   p3.join()
  205.   p2.join()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement