NAzT
5/12/2015 - 3:38 AM

read-rfid.py

read-rfid.py

#!/usr/bin/python
import serial
import requests

ser = serial.Serial(
   port='/dev/tty.SLAB_USBtoUART',
   baudrate=9600,
   parity=serial.PARITY_NONE,
   stopbits=serial.STOPBITS_ONE,
   bytesize=serial.EIGHTBITS
)


hexstrings = ["20"]   # big-endian 0xDEADBEEF

# bytes = bytearray(int(x, 16) for x in hexstrings)
# bytes = bytearray.fromhex("".join(hexstrings))     # Python 2.6 may need u""

# print(bytes)
# print(bytearray([0x20]))

# command = bytes

command = bytearray([0x02])
print(hex(ord(command)))
# c = 0x02
# c.to_bytes((c.bit_length() /8) +1, byteorder='big')

ser.write(command)

a = ""
r = 0
data = []
while 1:
    while ser.inWaiting():
        s = hex(ord(ser.read()))[2:]
        data.append(s)
        r = r+1
    r = r%4
    if r == 0 and len(data) > 0:
        card_id = "".join(data) 
        data = []
        try:
          print card_id
          print output.text
        except:
          print "exception"


ser.close()