Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # Code tested and is working as of 21/01/2022 for BP
- # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
- # SPDX-License-Identifier: MIT
- """
- This example shows connecting to the PN532 with I2C (requires clock
- stretching support), SPI, or UART. SPI is best, it uses the most pins but
- is the most reliable and universally supported.
- After initialization, try waving various 13.56MHz RFID cards over it!
- """
- from pickle import FALSE
- import board
- import busio
- from digitalio import DigitalInOut
- import binascii
- from multiprocessing import Process # for parallel processing
- import serial # For serial communication to STM32
- import uuid # For generating Transaction UID
- import time
- #
- # NOTE: pick the import that matches the interface being used
- #
- from adafruit_pn532.i2c import PN532_I2C
- # from adafruit_pn532.spi import PN532_SPI
- # from adafruit_pn532.uart import PN532_UART
- # I2C connection:
- i2c = busio.I2C(board.SCL, board.SDA)
- # Non-hardware
- # pn532 = PN532_I2C(i2c, debug=False)
- # With I2C, we recommend connecting RSTPD_N (reset) to a digital pin for manual
- # harware reset
- reset_pin = DigitalInOut(board.D6)
- # On Raspberry Pi, you must also connect a pin to P32 "H_Request" for hardware
- # wakeup! this means we don't need to do the I2C clock-stretch thing
- req_pin = DigitalInOut(board.D12)
- pn532 = PN532_I2C(i2c, debug=False, reset=reset_pin, req=req_pin)
- ic, ver, rev, support = pn532.firmware_version
- print("Found PN532 with firmware version: {0}.{1}".format(ver, rev))
- # Configure PN532 to communicate with MiFare cards
- pn532.SAM_configuration()
- # The flag that goes high if a known rfid card is swiped
- rfid_flag = False
- # Read and store the db in RAM
- byte_arr1 = []
- with open("rfidBinDatabase", "rb") as f:
- print('Opening the file!')
- byte = f.read(1)
- while byte:
- # Do stuff with byte.
- byte = f.read(1)
- byte_arr1.extend(bytes(byte))
- byte_arr = byte_arr1
- print('Byte arr of db is:',byte_arr)
- ser = serial.Serial(
- port='/dev/ttyS0', #Replace ttyS0 with ttyAM0 for Pi1,Pi2,Pi0
- baudrate = 9600,
- parity=serial.PARITY_NONE,
- stopbits=serial.STOPBITS_ONE,
- bytesize=serial.EIGHTBITS,
- timeout=1
- )
- DOOR_COUNT = 8
- def processStmData(bString):
- global imei, conn_status, door_status, charger_status, current, soc, soh, swap_state, station_state
- print(len(bString))
- if len(bString) == 10:
- imei = int.from_bytes(bytes(bString[2:10]), "little")
- print("imei : " + str(imei))
- elif len(bString) == 19:
- for i in range(DOOR_COUNT):
- door_status[i] = (bString[1] >> i) & 1
- print("door : " + str(door_status))
- for i in range(DOOR_COUNT):
- charger_status[i] = (bString[2] >> i) & 1
- print("charger : " + str(charger_status))
- conn_status = bString[3]
- print("conn : " + str(conn_status))
- swap_state = bString[4]
- print("swap_state : "+str(swap_state))
- station_state = bString[5]
- print("station_state : "+str(station_state))
- door_number = bString[6]
- current[door_number] = struct.unpack('f', bString[7:11])[0]
- print("current : " + str(current[door_number]))
- soc[door_number] = struct.unpack('f', bString[11:15])[0]
- print("soc : " + str(soc[door_number]))
- #soh[door_number] = struct.unpack('f', bString[15:19])[0]
- #print("soh : " + str(soh[door_number]))
- soh[door_number] = int.from_bytes(bytes(bString[15:19]), "little")
- print("soh : " + str(soh[door_number]))
- def readSerial():
- while 1:
- inString = ser.readline()
- if inString:
- print (inString)
- if inString[0] == 0xA0:
- # boot = False
- processStmData(inString)
- def SendIdleBytes():
- ser.write(b'\x0A')
- for i in range(17):
- ser.write(b'\x00')
- def SendTIDBytes():
- print ("The random id using uuid1() is : ",end="")
- x = uuid.uuid1()
- print (x)
- print("And in bytes:")
- in_bytes=x.bytes
- print(in_bytes)
- print("And the length:")
- print(len(in_bytes))
- ser.write(b'\x0A')
- ser.write(b'\x01')
- ser.write(in_bytes)
- def SendTapBytes():
- ser.write(b'\x0A')
- ser.write(b'\x03')
- ser.write(b'\x01')
- for i in range(15):
- ser.write(b'\x00')
- def SendBytes(invokedFlag = False):
- print("Send bytes called\n")
- if invokedFlag is True:
- print("\nSending the RFID Tap bytes serially\n\n")
- SendTapBytes()
- SendTIDBytes()
- time.sleep(10)
- else:
- while True:
- SendIdleBytes()
- time.sleep(1)
- def ScanCard():
- global rfid_flag
- print("Waiting for RFID/NFC card...")
- prevUID = bytearray(b'3\x00\x00\x00')
- while True:
- # Check if a card is available to read
- uid = pn532.read_passive_target(timeout=3)
- print(".", end="")
- # Try again if no card is available.
- if uid is None:
- continue
- # print("Detected card with UID:", uid)
- # print("Same ID?",prevUID, uid)
- if prevUID == uid:
- # print("Same ID",prevUID, uid)
- continue
- # Log the card that is detected
- prevUID = uid
- file = open("rfidLog.txt", "ab")
- # Write bytes to file
- immutable_bytes = bytes(uid)
- file.write(immutable_bytes)
- file.close()
- # if (i+1) % 3:
- # print(byte_arr)
- # byte_arr.clear()
- print("Detected card with UID:", uid)
- rfid_flag = set(immutable_bytes).issubset(byte_arr)
- print(rfid_flag,": card found from local DB")
- if rfid_flag:
- SendBytes(True)
- def HMIDisplay():
- print("HMI Display STARTED\n")
- while True:
- print("HMI: Some info\n")
- for i in range(10000000):
- for i in range(5):
- pass
- if __name__ == '__main__':
- p1 = Process(target=ScanCard)
- p2 = Process(target=readSerial)
- # p3 = Process(target=SendBytes)
- p1.start()
- # p3.start()
- p2.start()
- p1.join()
- # p3.join()
- p2.join()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement